JAVA QUESTION 1. Write a hierarchy of Java classes (in Java) based on the super
ID: 671035 • Letter: J
Question
JAVA QUESTION
1. Write a hierarchy of Java classes (in Java) based on the super (parent) class ComputingDevice (as in for example a personal computer). The parent class should be abstract and should have at least three sub-classes. The parent class should include at least two instance variables. The parent class should include at least one abstract method and at least one non-abstract method. One of the sub-classes should over-ride one of the methods in the super class. Another sub-class should over-load one of the methods in the super class. Each of the sub-classes should include at least one instance variable and one method to differentiate it from the other subclasses. (2 points)
2. Design and write a Java Interface Type for your ComputingDevice class above. The interface type may be used by other classes in your application. The interface type must include at least two method definitions. (2 points)
3. Write a new version of one of the sub-classes from problem #1. This class should implement the interface you wrote in problem #2. (2 points)
4. Write toString methods for the ComputingDevice class and all of its sub-classes. The toString method should print the name of the class and the values of all of its instance variables. (2 points)
5. Write a Java class called User with an instance variable called currentComputingDevice and a method called setCurrentComputingDevice that takes a ComputingDevice object as a parameter and sets the currentComputingDevice instance variable to that value. (2 points)
Explanation / Answer
abstract class ComputingDevice {
String name;
String type;
ComputingDevice(String n, String t)
{
name=n;
type=t;
}
abstract void display();
public void read(String n, String t)
{
name=n;
type=t;
}
@Override
public String toString()
{
return("Class name:Computing Device, Name: "+name+" Type: "+type);
}
public String getName()
{
return name;
}
public String getType()
{
return type;
}
}
class p1 extends ComputingDevice
{
int year;
p1(String n, String t, int y)
{
super(n,t);
year=y;
}
@Override
void display() {
System.out.println("Year: "+year);
}
int getYear()
{
return year;
}
@Override
public String toString()
{
return("Class name:P1, year: "+year);
}
}
class p2 extends ComputingDevice
{
double price;
p2(String n, String t, double p)
{
super(n,t);
price=p;
}
void read(double p)
{
price=p;
}
double getPrice()
{
return price;
}
@Override
void display() {
System.out.println("Price: "+price);
}
void app();
void list();
}
@Override
public void app() {
System.out.println("It can download many applications");
}
@Override
public void list() {
System.out.println("It can list installed applications");
}
}
String currentComputingDevice;
void setcurrentComputingDevice(ComputingDevice cc)
{
currentComputingDevice=cc.getName();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.