Question Consider the following problem description and write java code. A. Cons
ID: 3669815 • Letter: Q
Question
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
PurchaseItem.java
package com.chegg.test;
/**
* PurchaseItem class to initialize the name and price
*
* @author yourName
*
*/
public class PurchaseItem {
private String name;
private double unitPrice;
PurchaseItem() {
name = "no item";
unitPrice = 0;
}
PurchaseItem(String nam, double price) {
name = nam;
unitPrice = price;
}
/**
* @return the unitPrice
*/
public double getUnitPrice() {
return unitPrice;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param unitPrice
* the unitPrice to set
*/
public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
public String toString() {
return name + " " + "@" + String.format("%.2f", unitPrice);
}
}
WeighedItem.java
package com.chegg.test;
/**
* WeighedItem class to calculate the price for weighted items
*
* @author YourName
*
*/
public class WeighedItem extends PurchaseItem {
private double weight;
private double price;
// Constructor that invokes super class and initialize the weight and
// calculate the price
WeighedItem(String name, double price, double wei) {
super(name, price);
weight = wei;
price = getUnitPrice();
}
/**
* Method to to calculate the price
*/
@Override
public double getUnitPrice() {
double temp = super.getUnitPrice();
price = temp * weight;
return price;
}
/**
* Method to print the result
*/
@Override
public String toString() {
return super.toString() + " " + weight + " Kg " + price + " SR";
}
}
CountedItem.java
package com.chegg.test;
/**
* CountedItem class to calculate price for quantity items
*
* @author yourName
*
*/
public class CountedItem extends PurchaseItem {
private int quantity;
double price = 0;
// Constructor that invokes super class and initialize the quantity and
// calculate the price
CountedItem(String name, double price, int quan) {
super(name, price);
quantity = quan;
price = getUnitPrice();
}
/**
* Method to print the result
*/
@Override
public String toString() {
return super.toString() + " " + quantity + " units " + price + " SR";
}
/**
* Method to calculate the price
*/
@Override
public double getUnitPrice() {
double temp = super.getUnitPrice();
price = temp * quantity;
return price;
}
}
Application.java
package com.chegg.test;
/**
* Application class to calculate the price of given items
*
* @author yourName
*
*/
public class Application {
public static void main(String[] args) {
// construct objects for the two subclasses
CountedItem countedItem = new CountedItem("Pens", 4.5, 10);
WeighedItem weighedItem = new WeighedItem("Banana", 3.00, 1.37);
System.out.println("Item Price Weight\Count TotalPrice");
System.out.println(countedItem.toString());
System.out.println(weighedItem.toString());
}
}
Output:
Item Price WeightCount TotalPrice
Pens @4.50 10 units 45.0 SR
Banana @3.00 1.37 Kg 4.11 SR
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.