We are learning about parent and child classes and I think I am confuisng what n
ID: 3672307 • Letter: W
Question
We are learning about parent and child classes and I think I am confuisng what needs to go in the parent class and what needs to go in the child class it must also generate a random id for each order
// The package ID number is automatically assigned.
//In Package class, the base delivery price is determined using the following criteria:
// - Packages with weight of 32 ounces or less have a delivery fee of $5.00.
// - Packages with weight of more than 32 ounces incur an extra $0.12 fee per ounce.
//In InsuredPackage class, the insurance fee is calculated using the following criteria:
// - Packages with value of $20.00 or less have an insurance fee of $1.00.
// - Packages with a value of more than $20.00 have an insurance fee of $2.50.
Explanation / Answer
//Base class
/*Base class package that has methods to set ounces
and calculates the delivery charges */
//Package.java
public class Package
{
private int packageID;
private int ounces;
private double deliveryCharges;
//Constructor that sets the number of ounces
public Package(int ounces)
{
//generage package id a random number in a range of 1000
packageID=(int) (Math.random()*1000+1);
setOunces(ounces);
}
//Constructor to delivery charges
public Package(double deliveryCharges)
{
packageID=(int) (Math.random()*1000+1);
this.deliveryCharges=deliveryCharges;
}
public int getID()
{
return packageID;
}
//Private method that set ounces and call
//setDeliveryCharges
private void setOunces(int ounces)
{
this.ounces=ounces;
setDeliveryCharges(ounces);
}
//Set delivery charges of package of weight ounces
private void setDeliveryCharges(int ounces)
{
if(ounces<=32)
//set charge
deliveryCharges=5;
else
{
//set ounces grater than 32
int remainingOunces=(ounces-5);
deliveryCharges=5+remainingOunces*0.12;
}
}
//return ounces
public int getOunces()
{
return ounces;
}
//Return delivery charges
public double getDeliveryCharges()
{
return deliveryCharges;
}
}
---------------------------------------------------------------------------
//Child class
/**The class InsuredPackage that extends the Package
* class that pass the deliveryCharges to the
* base class package and get the deliverycharge
* fee from base class in getInsuranceFee class*/
//InsuredPackage.java
public class InsuredPackage extends Package
{
//constructor to set delivery charges for insured package
public InsuredPackage(double deliveryCharges)
{
//call super class Package constructor
super(deliveryCharges);
}
/**Returns the insurance fee based on the delivery charges
* fee*/
public double getInsuranceFee()
{
if(super.getDeliveryCharges()<20)
return 1;
else
return 2.50;
}
}
------------------------------------------------------------------------------------------------------
//Tester java program that tests the base class
//Package and InsuredPackage classes
//Driver.java
public class Driver
{
public static void main(String[] args)
{
//Create an instance of Package with ounces as input
Package packagee=new Package(32);
System.out.println("Packagee");
System.out.println("Package ID : "+packagee.getID());
System.out.println("ounces : "+packagee.getOunces());
System.out.println("Charges : "+packagee.getDeliveryCharges());
//Create an instance of InsuredPackage with ounces as charges
InsuredPackage insuredPackage=new InsuredPackage(25);
System.out.println("Packagee");
System.out.println("Package ID : "+insuredPackage.getID());
System.out.println("Insureance fee : "
+insuredPackage.getInsuranceFee());
System.out.println("Delivery Charges : "
+insuredPackage.getDeliveryCharges());
}
}
------------------------------------------------------------------------------------------------------------------------
Sample Ouptut:
Packagee
Package ID : 104
ounces : 32
Charges : 5.0
Packagee
Package ID : 504
Insureance fee : 2.5
Delivery Charges : 25.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.