Question: Java I. Introduction To motivate this lab, assume that a robot factory assembly line builds futuristic robots, all with the first part of their name
Java







I. Introduction To motivate this lab, assume that a robot factory assembly line builds futuristic robots, all with the first part of their name being "ArmyRobot" and the second part of their name being a random integer value. The combined first and second part of the name for each robot is used to construct a robot's ID number (serialID). For example the factory might produce robots with IDs "ArmyRobot45678" or "ArmyRobot4549008". A robot is made up of a head and torso. Your task is to write a program that simulates a robot garage, which is an array of three Robot objects. The Robot Torso and Robot Head classes are small. The Robot class is a bit more complex, because it contains an equals() methods, a copy () method, as well as a few getter and setter methods. The third robot in your array must have a serialID that is identical to the serialID of one of the other robots (you'll use the copy() method to create the third robot). The Robot class has a static field, numRobots that keeps count of how many robots there are. The figure below is a schematic representation of your robot garage: robotGarage address Robot serialID Robot serialID Robot serialid Robot Head RobotHead Robot Torso eyeColor | numArms Robot Head RobotTorso eyeColor || numArms RobotTorso numArms eyeColor II. Robot Torso Class RobotTorso - numArms : int + toString(): String + RobotTorso) Write the class Robot Torso according to the UML diagram below. The numArms field must be private. The constructor should set the value of numArms instance field to a random integer value between 0 and 10 (inclusive). As was discussed in lecture, it is a good practice to include a toString() method in each class, which should output a String that contains the information (or state of) the instance Robot Torso. In this case, for example if the Robot Torso field numArms is 9, the toString method should output: Number of arms : 9 Write this class and make sure it compiles before moving on to the next one. The entire class can be written 20 lines or fewer. III. Robot Head Class Robot Head - eyeColor : String + toString(): String + RobotHead() The Robot Head class is also straight-forward. It is made up of a single instance field, a constructor and a toString method. The constructor should generate a random number between 0 and 5 (inclusive), and based on that number, assign a unique value to the instance field eyeColor. You select the eye colors. You could use an if- else or a switch on the randomly generator number to assign the color. For example if the number is 0, then the color could be red, if the number is 1 the color could be green and so on. The toString method is very similar to the one in the class Robot Torso, it will print out the value in the instance field. If the eyeColor is "brown", then the toString method should print out: Eye color : brown IV. Robot Class The Robot class is an aggregate class, that is made up of four instance fields (one of which is static), a constructor, and 6 utility methods (one of which is static), as shown in the UML below. Remember that by convention static fields and methods are underlined in an UML. All of the instance fields need to be private. Robot numRobots : int robotHead : Robot Head robotTorso : Robot Torso serialID : String + Robot () + setSerialID(serial : String): void + getSerialIDO): String + getRobotCount(): int + toString(): String + equals(aRobot : Robot): boolean + copy0: Robot Robot constructor: Increment the static field numRobots by 1. Generate a random number between 0 and 100,000. Append the number to a string "ArmyRobot" and assign the string to the field serialID. For example if the number is 9865345, then the serialID should be "ArmyRobot9865345". Instantiate the fields robotHead and robot Torso. For example, to create a new Robot Head instance field, you'd write: robotHead = new RobotHead();|| This creates a new instance of Robot Head object, and assigns the reference of the new object to the instance filed robot Head. copy0 method: Create a new Robot object. Set the serialID of the newly created object to be the serialID of the Robot object from which the copy method is called. The full code for this method can be seen below. public Robot copy(){ Robot robotCopy = new Robot(); robotcopy.setSerialID(serialID); return robotCopy; equals() method: Checks if the serialID in the instance from where the method is being invoked is the same as the serialID of the instance that is being received as the argument. Return true if the serialIDs are the same. Refer to the lecture slides if you need some help. toString() method: Creates and returns a string which contains the serialID and also appends the returning values of the robot Torso and robot Head toString methods. The code for this method can be seen below. public String toString({ String str = "Serial ID: "+serialID+", "+robotHead.toString()+ ", "+robot Torso.toString(); return str; setSerialIDO and getSerialIDO methods: Set and return the field serialID. getRobot Count() method: Returns the field numRobots. V. RobotGarage Class Now that you've written the Robot, Robot Torso, and Robot Head classes, you will use them in the class RobotGarage. This will be the driver class for this programming assignment, it will have the main method. Figure 1 contains the pseudocode for the main method of this class. Declare an array of 3 Robot objects Instantiate new Robot objects into indices 0 and 1 of the array of Robots Use the copy() method from one of the robots in indices 0 or 1 to create a new robot object, and place the copy of the robot into index 2 of the // array of Robots Use the toString method in each robot object to print to the screen details about that robot Retrieve the value of the static variable numRobots and print it to the // screen Use a series of if-then statements to check if robots at indices 0 and 1, 0 and 2, or i and 2 are identical using the equals method in the Robot class, and print to the screen which robots are identical Figure 1: Pseudocode for main method of RobotGarage
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
