i have a question and i tried to answer it but i still have a few errors..can someone please give me my code back with the correction plz ? :(.... and i need it in less then a day...i know am being pushy am so sorry but i really need it
the Q is
Consider a superclass PurchaseItem which models customer?s purchases. This class has:
- two private instance variables name (String) and unitPrice (double).
- One constructor to initialize the instance variables.
- A default constructor to initialize name to ?no item?, and unitPrice to 0. use this()
- A method getPrice that returns the unitPrice.
- Accessor and mutator methods.
- A toString method to return the name of the item followed by @ symbol, then the unitPrice.
Consider two subclasses WeighedItem and CountedItem. WeighedItem has an additional instance variable weight (double) in Kg while CountedItem has an additional variable quantity (int) both private.
- Write an appropriate constructor for each of the classes making use of the constructor of the superclass in defining those of the subclasses.
- 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
- Override also toString method for each class making use of the toString method of the superclass in defining those of the subclasses.
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)
Write an application class where you construct objects from the two subclasses and print them on the screen.
and i wrote this program
public class PurchaseItem {
private String name;
private double unitprice;
public PurchaseItem (String name , double unitprice) {
this.name = name;
this.uniteprice = unitprice;
}
public PurchaseItem() {
name = "no item" :
uniteprice = o;
}
public double getunitprice() {
return uniteprice;
}
public String getname() {
return name;
}
public void setName(String newName){
this.name = newName;
}
public void setuniteprice(double newuniteprice){
this.uniteprice = newuniteprice;
}
public String toString(){
return "\nitem name:" + name + "@" + "\nuniteprice:" + uniteprice;
}
}
public class WeighedItem extends PurchaseItem {
private double kg;
public WeighedItem(String name , double unitprice , double kg) {
super (name , uniteprice);
this.kg = kg;
}
public double getunitprice() {
return uniteprice + "" + kg;
}
public String toString() {
return "\nitem name:" + name + "@" +"\item weight:" + kg + "\nuniteprice:" + uniteprice;
}
}
public class counteditem extends PurchaseItem {
private int quantity;
public counteditem(String name , double unitprice , int quantity) {
super (name , uniteprice);
this.quantity = quantity;
}
public double getunitprice() {
return uniteprice + "" + quantity;
}
public String toString() {
return "\nitem name:" + name + "@" +"\quantity:" + quantity + "\nuniteprice:" + uniteprice;
}
}