(25 pts) Implement in Java a concrete class named Tablet that inherits from the
ID: 3731497 • Letter: #
Question
(25 pts) Implement in Java a concrete class named Tablet that inherits from the Computer class and implements the Comparable interface. Here are the other details of Tablet: C. A double field screenSize that describes the size of the tablet screen. An integer field on Time indicating the number of minutes since the last charge. A no-arg constructor that explicitly calls the Computer arg constructor such that the tablet has a manufacturer of Motorola, serial number of 304992B, and a speed of 2.0 Ghz. The screen size should be 10 inches and an onTime of O. method powerRemaining():integer that returns the number of minutes of power remaining. It is computed by taking 300 (onTime 3). A toString) method that outputs all the known fields of the Tablet separated by a"l". Tablets are compared using the screenSize property. A Note: You do not need to show code for the mutator and accessor methods.Explanation / Answer
package January;
import java.util.Arrays;
// Class Computer definition
class Computer
{
// Instance variable to store data
String manufacturer;
String serialNumber;
double speed;
// Default constructor
Computer()
{
manufacturer = serialNumber = "";
speed = 0;
}// End of default constructor
// Parameterized constructor
Computer(String manu, String slno, double sp)
{
manufacturer = manu;
serialNumber = slno;
speed = sp;
}// End of parameterized constructor
// Method to set the manufacturer
void setManufacturer(String manu)
{
manufacturer = manu;
}// End of method
// Method to set the serial number
void setSerialNumber(String slno)
{
serialNumber = slno;
}// End of method
// Method to set the speed
void setSpeed(double sp)
{
speed = sp;
}// End of method
// Method to return manufacturer
String getManufacturer()
{
return manufacturer;
}// End of method
// Method to return serial number
String getSerialNumber()
{
return serialNumber;
}// End of method
// Method to return speed
double getSpeed()
{
return speed;
}// End of method
// Overrides toString() method
public String toString()
{
return " " + getSerialNumber() + " | " + getManufacturer() + " | " + getSpeed();
}// End of method
}// End of class
// Class Tablet derived from Computer and implements Comparable interface
class Tablet extends Computer implements Comparable<Tablet>
{
// Instance variable to store data
double screenSize;
int onTime;
// Default constructor
Tablet()
{
// Calls the base class parameterized constructor with constant values
super("Motorola", "304992B", 2.0);
screenSize = 10;
onTime = 0;
}// End of default constructor
// Parameterized constructor
Tablet(String manu, String slno, double si, double screen, int ontime)
{
// Calls the base class parameterized constructor
super(manu, slno, si);
screenSize = screen;
onTime = ontime;
}// End of parameterized constructor
// Method to set the screen size
void setScreenSize(double screen)
{
screenSize = screen;
}// End of parameterized constructor
// Method to set the on time
void setOnTime(int time)
{
onTime = time;
}// End of parameterized constructor
// Method to return screen size
double getScreenSize()
{
return screenSize;
}// End of parameterized constructor
// Method to return on time
int getOnTime()
{
return onTime;
}// End of parameterized constructor
// Method to calculate and return power remaining
int powerRemaining()
{
return (300 - onTime * 3);
}// End of parameterized constructor
// Overrides toString() method
public String toString()
{
return super.toString() + " | " + getScreenSize() + " | " + getOnTime() + " | " + powerRemaining();
}// End of class
// Overrides compareTo() method
public int compareTo(Tablet tab)
{
// Sort the tablets by screen size
return (int)(this.screenSize - tab.screenSize);
}// End of method
}// End of class
// Driver class TabletDemo definition
class TabletDemo
{
// main method definition
public static void main(String[] args)
{
// Declares an array of Tablet class of size three
Tablet tab[] = new Tablet[3];
// Creates an tablet object using default constructor
tab[0] = new Tablet();
// Creates tablet objects using parameterized constructor
tab[1] = new Tablet("Nokia", "123113C", 3.2, 5, 2);
tab[2] = new Tablet("Samsung", "221113M", 4.2, 20, 4);
System.out.print(" Before Sorting: ");
// Loops till number of objects
for(int c = 0; c < tab.length; c++)
// Displays each tablet object information
System.out.print(tab[c]);
// Calls the method to sort
Arrays.sort(tab);
// Displays each tablet object information after sorting
System.out.print(" After Sorting: " + Arrays.toString(tab));
}// End of main method
}// End of class
Sample Output:
Before Sorting:
304992B | Motorola | 2.0 | 10.0 | 0 | 300
123113C | Nokia | 3.2 | 5.0 | 2 | 294
221113M | Samsung | 4.2 | 20.0 | 4 | 288
After Sorting: [
123113C | Nokia | 3.2 | 5.0 | 2 | 294,
304992B | Motorola | 2.0 | 10.0 | 0 | 300,
221113M | Samsung | 4.2 | 20.0 | 4 | 288]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.