Question: Part 4 Interfaces Create a new Java project called firstName_lastName_Part4 in Netbeans. Select the option to create a main method. Create a new class called
Part 4 Interfaces Create a new Java project called firstName_lastName_Part4 in Netbeans. Select the option to create a main method. 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
below is the answers from part 2: I need the answer for part 4... please don't fill with wrong answer.please
//Computer class public class Computer { //variables private String make, model; private int year; private double price; //default constructor public Computer() { make = ""; model = ""; year = 0; price = 0; }
//parameterized constructor public Computer(String make, String model, int year, double price) { this.make = make; this.model = model; this.year = year; this.price = price; }
//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; }
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() { System.out.println(" The computers details are: " + " \tMake : " + make + " \tModel : " + model + " \tYear : " + year + " \tPrice : " + price + "$" + " " ); } //main method public static void main(String[] args) { //create a Computer with arguments Computer c1 = new Computer("Dell", "Optiplex", 2018, 1500); //create a computer without arguments Computer c2 = new Computer(); //set properties c2.setMake("HP"); c2.setModel("Slim Tower"); c2.setYear(2019); c2.setPrice(2000); //test mutator System.out.println("Setting computer 2 price via mutator"); c2.setPrice(3000); //test accessor System.out.println("Getting computer 2 price via accessor: " + c2.getPrice()); //test print method c2.printDetails(); }
}
part2
Computer.java
//Computer class public 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
import java.util.ArrayList; import java.util.Iterator;
public class Laptop extends Computer { private int year; private double price;
// default constructor public Laptop() { this.year = 0; this.price = 0; }
/** * parameterized constructor * * @param make * @param model * @param year * @param price */ public Laptop(String make, String model, int year, double price) { super(make, model); this.year = year; this.price = price; }
/** * @return the year */ public int getYear() { return year; }
/** * @param year the year to set */ public void setYear(int year) { this.year = year; }
/** * @return the price */ public double getPrice() { return price; }
/** * @param price the price to set */ public void setPrice(double price) { this.price = price; }
// method to print details @Override public void printDetails() { super.printDetails(); System.out.println("\tYear :" + year + " \tPrice: " + price + "$" + " ");
}
// main method public static void main(String[] args) { // Declare an ArrayList with a type parameter of Laptop ArrayList
// Get a laptop from the ArrayList Laptop getLap = list.get(0); // Remove a laptop from the ArrayList list.remove(1); // Print the size of the ArrayList System.out.println("Arraylist size: " + list.size()); // Clear the ArrayList list.clear();
// Create an object of type Laptop and an object of type Computer Laptop lapNew = new Laptop("Dell", "Optiplex", 2010, 30000); Computer computerNew = new Computer("Dell", "Optiplex"); lapNew.printDetails(); System.out.println(); computerNew.printDetails();
}
public static void print(Computer computer) { // o print out the computers make and model using the Computer accessor methods. System.out.println("Make: " + computer.getMake()); System.out.println("Model: " + computer.getModel()); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
