Question: Create a new class called Laptop. Create a new interface called Computer. Rewrite your code from Part2 so that: Computer is an interface Laptop implements
Create a new class called Laptop. Create a new interface called Computer. Rewrite your code from Part2 so that: Computer is an interface Laptop implements Computer. All methods, constructors etc that were in the Laptop and Computer classes in part 2 must be included in part 4. You need to rewrite the Laptop and Computer classes from part 2 so that Computer is an interface and there are multiple ways to create a Laptop. Check your code works by creating a new Laptop in your main method.
laptop and computer class from part 2
Computer.java
class Computer
{
// variables
private String make, model;
// default constructor
public Computer()
{
make = "";
model = "";
}
// parameterized constructor
public Computer(String make, String model)
{
this.make = make;
this.model = model;
}
// accessor and mutator
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
// method to print details
public void printDetails()
{
System.out.println(" The computers details are: "
+ " \tMake : " + make
+ " \tModel : " + model);
}
}
Laptop.java
//subclass laptops
class Laptop extends Computer
{
// variables
private int year;
private double price;
// default constructor
public Laptop()
{
// calling parent constructor
super();
year = 0;
price = 0;
}
// parameterized constructor
public Laptop(String make, String model, int year, double price)
{
// calling parent constructor
super(make, model);
this.year = year;
this.price = price;
}
// accessor and mutator
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
// method to print details
public void printDetails()
{
//calling parent printDetails to print make and model details
super.printDetails();
System.out.println("\tYear : "+ year+" \tPrice : $"+price+" ");
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
