Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following problem description and write java code. A. Consider a su

ID: 3669345 • Letter: C

Question

Consider the following problem description and write java code.

A. Consider a superclass PurchaseItem which models customer’s purchases. This class has:

1. Two private instance variables name and unit price.

2. One constructor to initialize the instance variables.

3. A default constructor to initialize name to “no item”, and unit price to 0.

4. A method that returns the price.

5. A toString method to return the name of the item followed by @ symbol, then the unit price.  


B. Consider two subclasses WeighedItem and CountedItem. WeighedItem has an additional instance variable weight while CountedItem has an additional variable quantity.

1. Write an appropriate constructor for each of the classes making use of the constructor of the superclass in defining those of the subclasses.

2. Override getPrice method that returns the price of the purchasedItem based on its unit price and weight (WeighedItem), or quantity (CountedItem). Make use of getPrice of the superclass

3. Override also toString method for each class making use of the toString method of the superclass in defining those of the subclasses.

4. toString should return something that can be printed on the receipt. For example    Banana @ 3.00   1.37Kg   4.11 SR (in case of WeighedItem class) Pens @ 4.5      10 units   45 SR (in case of CountedItem class)  

C. Write an application class where you construct objects from the two subclasses and print them on the screen.

Explanation / Answer

main.java


public class main {

   public static void main(String[] args) {
       PurchaseItem pi = new PurchaseItem("Phone", 10123.45);
       System.out.println(pi);
      
       WeightedItem wi = new WeightedItem("Oranges", 25.5, 3.2);
       System.out.println(wi);
      
       CountedItem ci = new CountedItem("Candy", 5, 35);
       System.out.println(ci);
   }

}

CountedItem.java

public class CountedItem extends PurchaseItem {
   private int count;
  
   public CountedItem(String name, double unitPrice, int count) {
       super(name, unitPrice);
       this.count = count;
   }
   //@Override
   public double getPrice() {
       return super.getPrice() * count;
   }
   //@Override
   public String toString() {
       return super.toString() + count + " Units " + getPrice() + " SR";
   }
}

PurchaseItem.java

public class PurchaseItem {
   private String name;
   private double unitPrice;
  
   public PurchaseItem(String name, double unitPrice) {
       this.name = name;
       this.unitPrice = unitPrice;
   }
  
   public PurchaseItem() {
       name = "No Name";
       unitPrice = 0;
   }
  
   public double getPrice() {
       return unitPrice;
   }
  
   public String getName() {
       return name;
   }
  
   public void setName(String name) {
       this.name = name;
   }
  
   public void setPrice(double unitPrice) {
       this.unitPrice = unitPrice;
   }
  
   @Override
   public String toString() {
       return name + "@" + unitPrice;
   }
}

WeightedItem.java

public class WeightedItem extends PurchaseItem {
   private double weight;
  
   public WeightedItem(String name, double unitPrice, double weight) {
       super(name, unitPrice);
       this.weight = weight;
   }
   @Override
   public double getPrice() {
       return super.getPrice() * weight;
   }
   @Override
   public String toString() {
       return super.toString() + " Weight: " + weight + "kg " + getPrice() + " SR";
   }
}

output
                                                                                                                   
Phone@10123.45                                                                                                                                              
Oranges@25.5 Weight: 3.2kg 81.60000000000001 SR                                                                                                             
Candy@5.035 Units 175.0 SR