In: Computer Science
JAVA PROGRAM:
INCLUDE COMMENTS EXPLAINING THE CODE PLEASE
Given the following information:
Drive 3 classes; class Computer (a super class), class Apple and class Others (subclasses). Your classes should have whatever variables you think needed to reflect the given information, think carefully what is needed to be assigned to the super class and what should be assigned to the sub classes.
We have one superclass Computer and two subclasses, Apple and Others. If we look at the given specification of the laptops, we notice 4 attributes that are common in both Apple and non-Apple laptops, so we keep those 4 attributes in Computer class. The non-common attributes will be kept in their corresponding specific classes. Below is the rough model of all the items and their class types.
Computer
=============
modelName
screenSize
processorSpeed
ramSize
Apple
=============
flashStorageSize
graphicsCard
turboBoostLimit
numberOfCores
processorGeneration
Others
=============
processorModel
isHd
ramType
operatingSystem
color
The code is divided in three separate files Computer.java, Apple.java, and Others.java. Each of the files has inline comments to explain the thought process and logic. I am pasting the code right below followed by the IDE screenshot of the same code and execution outputs.
Computer.java
package myStore;
/*
* The parent(super) class Computer
*/
public class Computer {
/*
* Computer has below 4 parameters common in
* both Apple and non-Apple laptops
* Hence, we will keep them in this class
* and will re-use them in the subclasses
=============
modelName
screenSize
processorSpeed
ramSize
*/
protected String modelName; // MacBook Air or Dell
i3552
protected double screenSize; // inches can
be non-integer, so take it as double
protected double processorSpeed;// Again it's a double
value, 1.66GHz etc
protected int ramSize; // It's an integer always,
4/8/16 GB etc.
// Define a parameterized constructor, this will help
the
// object formation of subclasses Apple and
Others
public Computer(String model, double size, double
speed, int ram) {
this.modelName = model;
this.screenSize = size;
this.processorSpeed = speed;
this.ramSize = ram;
}
}
Apple.java
package myStore;
/*
* The subclass Apple which will inherit all
* the properties of superclass Computer,
* namely
* modelName
screenSize
processorSpeed
ramSize
* Apart from these, it has its own attributes
*/
public class Apple extends Computer {
/*
*
Apple laptop specific
attributes(which are not
present in Other laptops)
=============
numberOfCores
processorGeneration
graphicsCard
turboBoostLimit
flashStorageSize
*/
private int flashStorageSize;
//Flash size in int, like 128GB etc
private String graphicsCard;
//String like Intel HD Graphics 6k
private double turboBoostLimit;
// It's a double, like 2.7GHz
private int numberOfCores;
// if dual-core then 2,
quad-core = 4
private String processorGeneration; //
Core i5 ,i3 or i7
/*
* Apple constructor which takes the inherited
attributes as well as
* its own attributes to initialize an object of class
"Apple"
* First 4 parameters are coming from the superclass
"Computer",
* while rest are Apple specific parameters
*/
private Apple(String model, double size, double speed,
int ram,
int flash, String graphics, double turbo,
int cores, String generation) {
// invoke the superClass
constructor (Computer's)
super(model, size, speed,
ram);
// Assign rest of Apple specific
attributes
this.flashStorageSize =
flash;
this.graphicsCard = graphics;
this.turboBoostLimit = turbo;
this.numberOfCores = cores;
this.processorGeneration =
generation;
}
public static void main(String args[]) {
/*
* Create an Apple-laptop object
with all the necessary parameters, name it ap,
* use ap to print the
Apple-laptop's specifications in a formatted output
*
* 13-inch MacBook Air, 1.6GHz
dual-core Intel Core i5 processor,
* Turbo Boost up to 2.7GHz, Intel
HD Graphics 6000, 8GB memory,
* 128GB PCIe-based flash
storage
*/
Apple ap = new Apple("MacBook Air",
13, 1.66, 8, 128, "Intel HD Graphics 6000", 2.7, 2, "Intel Core
i5");
System.out.println("------- Details
of an Apple laptop --------\n"
+ "Model: " + ap.modelName
+ "\nScreen: " + ap.screenSize + " inches"
+ "\nProcessor Speed: " + ap.processorSpeed + "
GHz"
+ "\nProcessor Type: " +
ap.processorGeneration
+ "\nCores: " + ap.numberOfCores
+ "\nTurboBoost up to: " + ap.turboBoostLimit +
" GHz"
+ "\nGraphics: " + ap.graphicsCard
+ "\nRAM: " + ap.ramSize + " GB"
+ "\nFlash: " + ap.flashStorageSize + "
GB");
}
}
Others.java
package myStore;
/*
* The subclass Others which will inherit all
* the properties of superclass Computer,
* namely
* modelName
screenSize
processorSpeed
ramSize
* Apart from these, it has its own attributes
* different from Apple class' attributes
*/
public class Others extends Computer{
/*
*
Others laptop specific
attributes(which are not
present in Apple laptops)
=============
processorModel
isHd
ramType
opeatingSystem
color
*/
private String processorModel; // the
brand model name as string
private boolean isHd;
// boolean(if HD or not, true/false?)
private String ramType;
// DDR or SDR
private String operatingSystem; //
Windows, Linux, FreeDOS?
private String color;
// Color of the laptop
/*
* Others constructor which takes the inherited
attributes as well as
* its own attributes to initialize an object of class
"Others"
* First 4 parameters are coming from the superclass
"Computer",
* while rest are Others specific parameters
*/
private Others(String model, double size, double
speed, int ram,
String
processor, boolean hd, String ramtype,
String os,
String color) {
// invoke the superClass
constructor (Computer's)
super(model, size, speed,
ram);
// Assign rest of Others' specific
attributes
this.processorModel =
processor;
this.isHd = hd;
this.ramType = ramtype;
this.operatingSystem = os;
this.color = color;
}
public static void main(String[] args) {
/*
* Create an Other-laptop object
with all the necessary parameters,
* name it ot, use ot to print the
Other-laptop's specifications
* in a formatted output
*
* 15.6-inch Dell i3552, 1.6GHz
Processor, Intel Pentium N3700,
* HD Laptop, 4 GB memory, DDR3L
SDRAM, Windows 10, Black
*/
Others ot = new Others("Dell
i3552", 15.6, 1.66, 4,
"Pentium N3700", true,
"DDR3L", "Windows 10", "Black");
System.out.println("------- Details
of a non-Apple laptop --------\n"
+ "Model: " + ot.modelName
+ "\nScreen: " + ot.screenSize + " inches"
+ "\nProcessor Speed: " + ot.processorSpeed + "
GHz"
+ "\nProcessor Type: " + ot.processorModel
+ "\nHD(?): " + ot.isHd
+ "\nRAM : " + ot.ramSize + "GB " +
ot.ramType
+ "\nOS: " + ot.operatingSystem
+ "\nColor: " + ot.color);
}
}
Computer.java
Apple.java
Others.java
Execution screenshots:
Let me know if you have any further queries and I will be glad to assist. Thanks for asking :)