2. Create a Computer class (G4 - 14 points) Write the definition of a class name
ID: 3573436 • Letter: 2
Question
2. Create a Computer class (G4 - 14 points) Write the definition of a class named Computer containing: a. An instance variable maker of type String, initialized to empty String. b. An instance variable numMegaBytesMemory of type int initialized to 0. c. An instance variable cpuSpeed of type double initialized to 0. d. An instance variable dualCore of type boolean initialized to false. e. In addition, your Computer class definition should provide an appropriately named "get" method and "set" method for each of these. f. A constructor with no-arguments that initializes the value of numMegaBytesMemory to 500 g. A constructor that accepts one String parameter and one double. The value of the String is used to initialize the value of maker. The double used to initialize the value of cpuSpeed. Note: you will not lose points for coding standards on this question, but try to keep it easy to read. 3. Write ComputerTester class (G4 - 7 points) ComputerTester should have a main method in it. Inside the main method: a. Create a Computer object named myLaptop using “HewlettPackard” and 3.8. b. Use the setter method to change the instance variable dualCore to true; c. Display the maker, numMegaBytesMemory, cpuSpeed and dualCore for myLaptop. d. Create a Computer object named myTablet using “MicroSoft Surface3” and 2.6 e. Set the sizeMemory of myTablet to 350. f. Display the maker, numMegaBytesMemory, cpuSpeed and dualCore for myTablet.
Explanation / Answer
Please follow the code and commeents for description :
a)
CODE :
public class Computer { // class
private static String maker = ""; // required instance variables
private static int numMegaBytesMemory = 0;
private static double cpuSpeed = 0;
private static boolean dualCore = false;
public Computer() { // no arg constructor
numMegaBytesMemory = 500; // set the value
}
public Computer(String makerName) { // maker name setter
maker = makerName; // set the data
}
public Computer(double speed) { // speed value setter
cpuSpeed = speed; // set teh value
}
// getters and setters
public static String getMaker() {
return maker;
}
public static void setMaker(String maker) {
Computer.maker = maker;
}
public static int getNumMegaBytesMemory() {
return numMegaBytesMemory;
}
public static void setNumMegaBytesMemory(int numMegaBytesMemory) {
Computer.numMegaBytesMemory = numMegaBytesMemory;
}
public static double getCpuSpeed() {
return cpuSpeed;
}
public static void setCpuSpeed(double cpuSpeed) {
Computer.cpuSpeed = cpuSpeed;
}
public static boolean isDualCore() {
return dualCore;
}
public static void setDualCore(boolean dualCore) {
Computer.dualCore = dualCore;
}
}
b)
CODE :
public class ComputerTester { // class
public static void main(String[] args) { // driver method
Computer myLaptop = new Computer("HewlettPackard"); // class object
myLaptop.setDualCore(true); // set the value
System.out.println("The Maker is " + myLaptop.getMaker()); // print the data to console
System.out.println("The Memory is " + myLaptop.getNumMegaBytesMemory());
System.out.println("The CPU Speed is " + myLaptop.getCpuSpeed());
System.out.println("The Computer is DualCore : " + myLaptop.isDualCore());
}
}
OUTPUT :
The Maker is HewlettPackard
The Memory is 0
The CPU Speed is 0.0
The Computer is DualCore : true.
CODE :
public class ComputerTester { // class
public static void main(String[] args) { // driver method
Computer myTablet = new Computer("MicroSoft Surface3"); // class object
myTablet.setNumMegaBytesMemory(350); // set the value
System.out.println("The Maker is " + myTablet.getMaker()); // print the data to console
System.out.println("The Memory is " + myTablet.getNumMegaBytesMemory());
System.out.println("The CPU Speed is " + myTablet.getCpuSpeed());
System.out.println("The Computer is DualCore : " + myTablet.isDualCore());
}
}
OUTPUT :
The Maker is MicroSoft Surface3
The Memory is 350
The CPU Speed is 0.0
The Computer is DualCore : false
Hope this is helpful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.