In: Computer Science
Part A: (Chapter 8) Make the following updates to the original code below:
Part B : (Chapter 10) Make the following updates to your Assignment 1
----
Original Code:
public class Coffee { private int Number; private double price; private String title; public Coffee() { } public Coffee (int Number, double price, String title) { this.Number = Number; this.price = price; this.title = title; } public void display() { System.out.println("Coffee [price=" + this.price + ", title=" + this.title + ", Number=" + this.Number + "]"); } public String getTitle() { return this.title; } public void setTitle(String title) { this.title = title; } public int getNumber() { return this.Number; } public void setNumber(int Number) { this.Number = Number; } public double getPrice() { return this.price; } public void setPrice(double price) { this.price = price; } }
-----
Driver (demo) code:
public class Driver { public static void main(String[] args) { Coffee C1 = new Coffee(); C1.setPrice(2.50); C1.setTitle("Black"); C1.setNumber(1); System.out.println("C1:"); C1.display(); Coffee C2 = new Coffee (2, 6.00, "Latte"); System.out.println("\nC2:"); C2.display(); } }
---------------
Result:
C1: Coffee [price=2.5, title=Black, Number=1]
C2: Coffee [price=6.0, title=Latte, Number=2]
Process finished with exit code 0
Find below the java code that meets all the Assignment conditions and all points are properly described using comments.
Java Code:
class Coffee {
private int Number;
private double price;
private String title;
private static int counter;
// increment every time when object is created
{
counter += 1;
}
// static method that displays the counter value
public static void displayCounter() {
System.out.println("\nCounter: " + counter);
}
public Coffee() {
this(0, 0, null); // "this" reference to call the parameterized constructor
}
public Coffee(int Number, double price, String title) {
this.Number = Number;
this.price = price;
this.title = title;
}
// copy constructor
public Coffee(Coffee coffee) {
this.Number = coffee.Number;
this.price = coffee.price;
this.title = coffee.title;
}
public void display() {
System.out.println("Coffee [price=" + this.price + ", title=" + this.title + ", Number=" + this.Number + "]");
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public int getNumber() {
return this.Number;
}
public void setNumber(int Number) {
this.Number = Number;
}
public double getPrice() {
return this.price;
}
public void setPrice(double price) {
this.price = price;
}
// override equals methods of Coffee class
@Override
public boolean equals(Object o) {
// If the object is compared with itself then return true
if (o == this) {
return true;
}
/*
* Check if o is an instance of Complex or not "null instanceof [type]" also
* returns false
*/
if (!(o instanceof Coffee)) {
return false;
}
// typecast o to Coffee so that we can compare data members
Coffee c = (Coffee) o;
// Compare the data members and return accordingly
return Double.compare(price, c.price) == 0 && Integer.compare(Number, c.Number) == 0 && title.equals(c.title);
}
// override toString methods of Coffee class
@Override
public String toString() {
return "Coffee [Number=" + Number + ", price=" + price + ", title=" + title + "]";
}
}
// subclass that extends Coffee class
class PremiumCoffee extends Coffee {
private boolean premium; // one additional attribute of subClass
// default constructor
public PremiumCoffee() {
super(); // call base class default constructor
premium = false;
}
public PremiumCoffee(boolean premium, int Number, double price, String title) {
super(Number, price, title); // call base class parameterized constructor
this.premium = premium;
}
/*
* Override the display() method to print out all the instance variable
* values from the base class, and also from the sub class
*/
@Override
public void display() {
System.out.println("PremiumCoffee [price=" + getPrice() + ", title=" + getTitle() + ", Number=" + getNumber() + ", Premium=" + this.premium + "]");
}
public boolean isPremium() {
return premium;
}
public void setPremium(boolean premium ) {
this.premium = premium;
}
}
public class Driver {
public static void main(String[] args) {
// using the no-arg (default) constructor
Coffee C1 = new Coffee();
C1.setPrice(2.50);
C1.setTitle("Black");
C1.setNumber(1);
System.out.println("C1:");
C1.display();
// creating object using copy Constructor
Coffee C2 = new Coffee(C1);
System.out.println("\nCopy Constructor(C2):");
C2.display();
// check overriden methods toString() and equals()
System.out.println("\ntoString() : "+C2);
System.out.println("Is C1 is equals to C2? "+C1.equals(C2));
//using the parameterized constructor
Coffee C3 = new Coffee(2, 6.00, "Latte");
System.out.println("\nC3:");
C3.display();
System.out.println("Is C1 is equals to C3? "+C1.equals(C3));
// using the no-arg (default) constructor
PremiumCoffee PC1 = new PremiumCoffee();
PC1.setPrice(5.50);
PC1.setTitle("AMERICANO");
PC1.setNumber(3);
PC1.setPremium(true);
System.out.println("\nPC1:");
PC1.display();
//using the parameterized constructor
PremiumCoffee PC2 = new PremiumCoffee(false, 4, 8.00, "CAPPUCCINO");
System.out.println("\nPC2:");
PC2.display();
// call displayCounter() to get the total number of objects created
Coffee.displayCounter();
}
}