Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

AlienBot.java TestAlienBot.java Problem 1 Write a new class called AlienBot (a subclass of RobotSE) with a now-familiar 5-parameter constructor (the last parameter represents the number

AlienBot.java TestAlienBot.java

Problem 1 Write a new class called AlienBot (a subclass of RobotSE) with a now-familiar 5-parameter constructor (the last parameter represents the number of Things the robot begins with). This class will have some instance (non-static) methods as described below. You will also write a TestAlienBot class containing a main method.

Step 1 (Squares) Add a method to AlienBot called makeSquare. This method should take an int (lets assume the parameter is called num) and return void.

First, makeSquare should always cause the AlienBot to face east. Then if num 0, dont do anything else (just return from the method). Otherwise, make the AlienBot travel over a square region with num rows and num columns, such that the AlienBots starting position is the top-left corner of this square region. The AlienBot should leave a Thing in each intersection of the square region. Use stepwise refinement: Write a method called oneRow that makes the AlienBot travel along a single row from left to right, leaving a Thing in each intersection, and then also makes the AlienBot move back to the beginning of the next row, again facing east. The oneRow method will need to take an input of type int indicating how long a row is. Now your makeSquare method can be very simple, since it only needs to call oneRow the appropriate number of times. Inside main, before you construct a City or an AlienBot, get an integer from the user via a Scanner object associated with the keyboard, as you have done before (print a meaningful prompt first). See the last page of this handout for a reminder about how to use Scanner to get user input. Now inside main, construct a City and place an AlienBot in the city. The AlienBot should initially have n*n Things in its backpack, where n is the users input. Call the makeSquare method on the AlienBot, passing it the integer entered by the user. Does the AlienBot do what it is supposed to do? Run your program multiple times, creating squares of different sizes at different starting locations.

Step 2 (Circles) Consider the situation in which the user enters an odd integer n. This means that a square region of size n n has an intersection that lies in the very center of the square (on the other hand, if n is even, no intersection is in the exact center). The distance from this center intersection to the right or left edge of the square is (n-1)/2, and the distance from this center intersection to the top or bottom edge of the square is also (n-1)/2. (Convince yourself of this.) If we let r = (n-1)/2, and if we only put a Thing in an intersection of the square region if the distance between that intersection and the center is r, then the result will be a circular shape made of Things that fits snugly inside the square. (As you may have guessed, we used the variable r because it represents the radius of the circle.)

Add a method to AlienBot called makeCircle. This method should take in an int (again, lets assume the parameter is called num) and return void. First, makeCircle should cause the AlienBot to face east. Then if num 0 or if num is even, dont do anything else (just return from the method). Otherwise, think of the AlienBot as sitting at the top-left corner of a square region of size num num, and calculate the street and avenue of the center intersection (store these in local variables). Then have makeCircle call a second (overloaded) version of oneRow num times. This enhanced oneRow method (make sure you keep the old one from Step 1) should take in three int values corresponding to: the length of a row, the street of the center intersection, and the avenue of the center intersection. This oneRow method should also cause the AlienBot to travel along a single row from left to right and then return to the beginning of the next row facing east, but at each intersection as it travels from left to right, it should only place a Thing if the distance between the current intersection and the center intersection is r. Note that you can calculate r from the length of the row, and you can calculate the desired distance using the well-known formula for the distance between two points and the static Math.sqrt method (of course you need to know the street and avenue of the intersection where the AlienBot currently is, but remember that there are methods inherited from RobotSE for this, which in turn are inherited from Robot). Inside main, prompt the user for a second integer, but this time present a message that asks for an odd number. Then construct a second AlienBot with n*n Things in its backpack, where n is the users input. (Place this second AlienBot reasonably far away from the first one in the city so that the shapes they create dont overlap each other.) Call makeCircle on the second AlienBot to see if it works as expected. Run your program multiple times with different inputs.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Reliability Engineering Designing And Operating Resilient Database Systems

Authors: Laine Campbell, Charity Majors

1st Edition

978-1491925942

More Books

Students also viewed these Databases questions