Question
RobotSE (extension of becker.jar/becker robot)is a new kind of robot with extended capabilities such as turnAround, and turnRight. RobotSE also makes public a number of
RobotSE (extension of becker.jar/becker robot)is a new kind of robot with extended capabilities such as turnAround, and turnRight. RobotSE also makes public a number of protected methods in Robot.
Write a class called PrimeMover that extends RobotSE. Include a standard 4-parameter constructor, and then write a custom move (int n) method that makes the robot move forward n intersections, but only if n is a prime number, otherwise, the robot should not move. Recall that a prime number is a positive integer greater than 1 whose only positive integer factors are 1 and itself. The first few primes are 2, 3, 5, 7, 11, 13, 17, ... (An integer larger than 1 that is not prime is called composite. The integer 1 itself is a special case since it is neither prime nor composite it is called a unit.) Instead of putting the prime-testing code inside your custom move method, apply the principle of stepwise refinement, which says you should break larger tasks into smaller sub-tasks, and then write a method for each sub-task. For the current exercise, the sub-task is testing whether or not an integer is prime. Since a prime-testing method does not need to use any information about a robot, it is most appropriate to make it static (standalone), and this means it can easily be placed in another class. Write a class called ExtraMath, and in this class put a static method called isprime that takes an int argument and returns a boolean. The return value should, of course, be true if the input is a prime number, and false otherwise. Recall that in Java, a return statement looks like this: return ; (Terminology: A method that returns a boolean value is called a predicate.) How do you test whether or not a number is prime? The simplest way is to try to divide each integer in an appropriate range (what range?), using a loop. If any of these integers divide evenly, the number is not prime. If none of these integers divides evenly, the number is prime. To check divisibility by a specific integer, use the remainder %) operator (also called the mod operator). Back inside your custom move method in the PrimeMover class, call isPrime to determine whether or not the robot should move. (Remember the syntax for calling a static method in another class.) For actually moving, use the move method inherited from RobotSE that takes an integer argument. Now write a main method in a class called TestPrimeMover in which you construct a PrimeMover object and call your move method on it multiple times (with various arguments). It might help to make the robot turn after each call to move so that it will be obvious when one move command ends and another begins.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started