Question

In: Computer Science

ITP 100 Programming Assignment 1 This program uses the Little-Crab-3 scenario complete the following steps. Start...

ITP 100 Programming Assignment 1

This program uses the Little-Crab-3 scenario complete the following steps. Start with a fresh un-edited copy of Little-Crab-3. When finished paste each of your class source code files into word and submit through Canvas.

1.   Change the code to make the crabs turn left when the l (lowercase L) key is pressed. Make the crabs turn right when the r key is pressed.

public void act()

   {

       if(Greenfoot.isKeyDown("l"))

       {

           turn(-15);

       }

       if (Greenfoot.isKeyDown("r"))

       {

           turn(15);

       }

           

   }

2.   Make changes to the code so that the worms will randomly move between 1 and 10 each time act is pressed

public void act()

{

   if( Greenfoot.getRandomNumber(1));

   {

      move(4);

   }

   if( Greenfoot.getRandomNumber(10));

}

Move(4);

}

3.     Make changes to the code so that the worm will randomly eat lobsters 15% of the time (you may need to check if you are touching a Lobster first)

public void act()

{

   if ( isAtEdge())

   {

       turn(17);

   }

   if( Greenfoot.getRandomNumber(100)<15 )

   {

       turn(4);

   }

   move(5);

}

4.   Create a method named mover that has an integer parameter named distance, and no return type. This method causes the crab to move the value of the distance parameter.

public void act()

{ randomMove();

}

void randomMove()

{

move(Greenfoot.getRandomNumber(15);

}

5. Add code that will cause the crab to turn 17 if the q button is pressed

public void act()

   {

       turnCrab();

   }

   void turnCrab()

   {

       if(Greenfoot.isKeyDown("q"))

       {

           turn(17);

       }

   }

6.   Add code that will cause the crab to turn -17 if they w button is pressed

public void act()

   {

       turnCrab();

   }

   void turnCrab()

   {

       if(Greenfoot.isKeyDown("w"))

       {

           turn(-17);

       }

   }

I am working on this assignment - Is there anyone who can help me check my answer - I think I am making some mistakes - correct me with explanation please

Solutions

Expert Solution

Answering the first Question i.e. 1:

Modified Code:

public void act()

   {

       if(Greenfoot.isKeyDown("l"))

       {

           turn(-15);

       }

       if (Greenfoot.isKeyDown("r"))

       {

           turn(15);

       }

           

   }

Reason:

1. This code is correct.

2. Here, when the l (lowercase L) key is pressed, a left turn is made using turn(-15).

3. Here, when the r  key is pressed, a right turn is made using turn(15).

4. To judge the key pressing action, code use Greenfoot.isKeyDown() function


Related Solutions

DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1....
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods you...
Complete the following assignment in C programming language. 1. Declare the following float variables: -Maximum exam...
Complete the following assignment in C programming language. 1. Declare the following float variables: -Maximum exam score, user's exam score, and percentage. 2. Ask the user to input data into your variables, such as: -"What is the max score of your exam:" -"What was your score:" 3. Use if statements to validate user inputs. For example, score received should not be more than maximum possible score. -Display an error message when the user enters invalid data. -You can restart the...
Programming assignment 4 : C++ Write a program to do the following: 1.Define a structure to...
Programming assignment 4 : C++ Write a program to do the following: 1.Define a structure to store a date, which includes day(int), month(int), and year(int). 2.Define a structure to store an address, which includes address(house number and street)(string), city(string), state(string), zip code (string). 3.Define a class to store the following information about a student. It should include private member variables: name(string), ID (int), date of birth (the first structure), address (the second structure), total credit earned (int), and GPA (double)....
Part 1: Complete the assignment by executing the following steps. Generate a dataset of 300 random...
Part 1: Complete the assignment by executing the following steps. Generate a dataset of 300 random numbers that follows are normal distribution with a mean of 200 and a standard deviation of 10. Use appropriate R command(s) to validate that an approximate mean = 200 and an approximate standard deviation = 10 was obtained. Create a histogram depicting the data distribution for the 300 random numbers generated in step 1. Create a histogram that superimposes a normal curve over the...
Complete the following steps: Step 1: State your topic and dependent variable. Start with a simple...
Complete the following steps: Step 1: State your topic and dependent variable. Start with a simple question that you want to know the answer to: “What do (your selected population) think about _____________?” Fill in the blank, and you have your topic, as well as your dependent variable: “opinion about ________________.” Step 2: Define and describe your target population – the people you want to participate in your survey. Step 3: Describe your sampling approach. Will it be probability or...
Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going...
Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going to implement a simple C version of the UNIX cat program called lolcat. The cat program allows you to display the contents of one or more text files. The lolcat program will only display one file. The correct usage of your program should be to execute it on the command line with a single command line argument consisting of the name you want to...
This assignment uses a combination of classes and arrays. Instructions: 1) download the 3 program files...
This assignment uses a combination of classes and arrays. Instructions: 1) download the 3 program files into a new C++ project.    NOTE: if your IDE does not allow you to create projects - or you are not sure how to do it - then you may combine the two cpp files into a single file. 2) Complete the program by adding code the the class methods. You may want to study the existing code first to get a feel...
1. INTRODUCTION The goal of this programming assignment is for students to write a Python program...
1. INTRODUCTION The goal of this programming assignment is for students to write a Python program that uses repetition (i.e. “loops”) and decision structures to solve a problem. 2. PROBLEM DEFINITION  Write a Python program that performs simple math operations. It will present the user with a menu and prompt the user for an option to be selected, such as: (1) addition (2) subtraction (3) multiplication (4) division (5) quit Please select an option (1 – 5) from the...
Complete the 3 programming problems in this assignment by using Microsoft Visual Studio Suite. Compile your...
Complete the 3 programming problems in this assignment by using Microsoft Visual Studio Suite. Compile your solutions for each problem solved in a Word or Google document which includes (a) the pseudocode, (b) the C# codes and (c) the execution result (screen capture just the answer part using Snipping Tool, avoiding non-related or blank spaces). Notice for readability, that the (a) & (b) are in text mode and the (c) is in image mode. Use proper titles and wording in...
Complete the 3 programming problems in this assignment by using Microsoft Visual Studio Suite. Compile your...
Complete the 3 programming problems in this assignment by using Microsoft Visual Studio Suite. Compile your solutions for each problem solved in a Word or Google document which includes (a) the pseudocode, (b) the C# codes and (c) the execution result (screen capture just the answer part using Snipping Tool, avoiding non-related or blank spaces). Notice for readability, that the (a) & (b) are in text mode and the (c) is in image mode. Use proper titles and wording in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT