The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 313 Weeks Ago, 5 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
I want to know how to write the four .java files code.
To motivate this lab, assume that a robot factory assembly line builds futuristic robots, all with the first name Johnny and a last name that is an integer value. The combined first and last name for each robot is used to construct a robot’s ID number (serialID). For example the factory might produce robots with IDs Johnny36723 or Johnny23232. 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 RobotTorsoand RobotHeadclasses 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:

Create a RobotTorso.java class per the UML diagram shown below.
|
RobotTorso |
|
- intnumArms |
|
+ RobotTorso() + toString() : String |
The numArmsfield must be private. The constructor should set the value of the numArmsinstance field to a random integer between 0 and 9 (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 RobotTorso. In this case, for example if the RobotTorso field numArmsis 7, the toStringmethod might output:
Make sure that your RobotTorsoclass compiles before you proceed to the next section.
The RobotHeadclass is also straight-forward. It is made up of a single instance field, a constructor, and a toStringmethod, as shown in the below UML diagram:
|
RobotHead |
|
- String eyeColor |
|
+ RobotHead() + toString() : String |
The constructor should generate a random number between 0 and 4 (inclusive), and based on that number, assign a unique value to the instance field eyeColor. You select the eye colors. In the example code shown in the last section of this lab, the eye colors in use are blue, green, evil red, creepy yellow, or black. The toString() method, as in the RobotTorsoclass, should output a String that gives details about the RobotHeadinstance. For example, if the eyeColorfield of the instance is creepy yellow, then the toString() method might output:
Make sure that your RobotHeadclass compiles before you proceed to the next step.
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 show in the below UML diagram (by convention in a UML diagram a static field or method is underlined). Make sure that all of the instance fields are private.
|
Robot |
|
- numRobots: int = 0 - robotHead : RobotHead - robotTorso : RobotTorso - serialID : String |
|
+ Robot() + getRobotCount() : int + copy() : Robot + setSerialID(serial : String) : void + equals(aRobot : Robot) : Boolean + getSerialID() : String + toString() : String |
The constructor should:
• Increment by 1 the static field numRobots
• Generate a random integer between 0 and 99999 as the robot’s “last” name, and create a SerialIDString value that is “Johnny” appended with the random number. For example, Johnny36278
• Instantiate the fields robotHead and robotTorso. For example, to create a new RobotHead instance field, you’d write:
Be sure you understand that this would be create a new instance of a RobotHeadobject, and assign the reference variable of the new object to the instance field robotHead.
The copy() method should:
• Create a new Robot object
• Set the serialIDof the newly created object to be the serialIDof the Robot object from which the copy method is called. The full code for the copy method is provide for you below. Make sure you understand what this code is doing.

The setSerialID(), getSerialID(), and getRobotCounts() methods are all
standard setter or getter methods. They should set and retrieve, respectively, the value of the serialIDinstance field.
The equals() method should check if the serialIDin the instance from where the method is being invoked is the same as the serialIDof the instance that is being received as the argument. Refer to the lecture slides to see how it is done. You’ll need to include a conditional (if), and return either a true or a false. Because the serialIDfield is a String, you must use the equals method of the String class to see if the serialIDsare the same.
The toString() method in the Robot class is slightly more advanced than the toStringmethods of the RobotHeadand RobotTorsoclasses. This is because the Robot class’s toString() method should output a String that is the robots ID, and invoke the toStringmethods of the robotTorsoand robotHeadinstance fields. The entirety of the toStringmethod of the Robot class is given to you below. Make sure you understand how it invokes the toStringmethods of robotTorsoand robotHeadinstance fields.
Make sure that your Robot class compiles before you proceed to the next step.
Now that you’ve written the Robot, RobotTorso, and RobotHeadclasses, create an array of Robot objects, and use the copy and/or toStringmethods, as well as the equal method in the Robot class, to manipulate those objects.
Create a RobotGarage.java file with a main routine for which the pseudocode is shown below.
// 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 1 and 2 are identical using the equals method in the Robot
// class, and print to the screen which robots are identical
Two sample invocations for the program RobotGarageare shown below.

----------- ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly