Create an application named PackageDemo that declares and demonstrates objects o
ID: 3795524 • Letter: C
Question
Create an application named PackageDemo that declares and demonstrates objects of the package class and its descendants. The package class includes auto-implemented properties for an id number, recipients name, and weight in ounces. The class also comntins a delivery price feild that is set when the weight is set as $5 for the first 32 ounces and 12 cents per ounce for every ounce over 32. b. Create a child class named InsuredPackage, which includes a field for the package's value. Override the method that sets a Package's delivery price to include insurance, which is $1 for packages valued up to $20 and $2.50 for packages valued $20 or more.
Explanation / Answer
//************************************** Parent PackageDemo **************************************
package Solution;
// This program will create a PackageDemo class, contain data elements package id , package name, package //weight and delivery price
public class PackageDemo {
int packageId;
String packageName;
double packageWeight;
double deliveryPrice;
// get and set method of each data element
public int getPackageId() {
return packageId;
}
public void setPackageId(int packageId) {
this.packageId = (int) Math.random();
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public double getPackageWeight() {
return packageWeight;
}
public void setPackageWeight(double packageWeight) {
this.packageWeight = packageWeight;
}
public double getDeliveryPrice() {
return deliveryPrice;
}
public void setDeliveryPrice(double deliveryPrice) {
this.deliveryPrice = deliveryPrice;
}
//constructor
PackageDemo(String packageName, double packageWeight)
{
this.packageName=packageName;
this.packageWeight = packageWeight;
}
/*This method will compute the delivery price of the package
* this method assume the weight > 32 ounces
*/
public double computeDeliveryPrice(double packageWeight)
{
int price=5;
if (packageWeight > 32)
{
//weight is set as $5 for the first 32 ounces
price=5;
int cents=0;
for (int i=1;i<(packageWeight-32);i++)
{
// 12 cents for each every ounce over 32
cents +=12;
}
//add to the price
price +=(cents/100);
} // end of if loop
return price;
} //end of method
} //end of class
//************************************** Child class InsuredPackage **************************************
/* This program is the child class of the PackageDemo class.*/
package Solution;
public class InsuredPackage extends PackageDemo {
//overall delivery price
double packageValue;
// This method will override the super class method and add insurance price based on the delivery price
@Override
public double computeDeliveryPrice(double packageWeight)
{
//call super class method
double price= super.computeDeliveryPrice(packageWeight);
if (price <20)
{
// $1 for packages valued up to $20
this.packageValue=price + 1;
}
else
{
// $2.50 for packages valued $20 or more
this.packageValue=price + 2.50;
}
return this.packageValue;
}
// test method
public static void main(String[] args)
{
System.out.println("Create insurance package");
InsuredPackage insuredPackage = new InsuredPackage("Test Package ", 50);
System.out.println("the total delivery price is " + insuredPackage.computeDeliveryPrice(insuredPackage.getPackageWeight()));
}
public InsuredPackage(String packageName, double packageWeight )
{
super(packageName,packageWeight);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.