Create a class named Package with data fields for wrghtin ounces, shipping metho
ID: 3733854 • Letter: C
Question
Create a class named Package with data fields for wrghtin ounces, shipping method, and shipping cost The shipping method is a character: A for air, T for truck, or Mfor mail. The Package class contains a constructor that requires arguments for weight a shipping method. The constructor calls a calculateCostl method chat determines the shipping cost based an the follow ng table: Weight loz) Air i5) Truck (S Mai to 8 2.00 50 to 15 3.00 ? and over 450 The package c ass also contains a disp method that displays the values in all four fields. Create a subclass armed InsuredPackage t atac an n surance cost to the sh co toas onchefo o bl tional Cost Shipping Cost Before Insurance (S) 0 tO 1.00 ddi 13 45 01 to 3.00 01 and over write an application amed Us Package that instantiates at least three o ects of each type Package and insure Package sin a variety of ei tsa s sp ng m es so ri er ts re Pack ge 3 ns red u s er eme ackage.java, InsuredPackage.java, and UePackagejavaExplanation / Answer
Package.java
public class Package {
//Declaring instance variables
private double weight;
private char shippingMethod;
private double shippingCost;
//Parameterized constructor
public Package(double weight, char shippingMethod) {
super();
this.weight = weight;
this.shippingMethod = shippingMethod;
calculateCost();
}
public double getShippingCost() {
return shippingCost;
}
public void setShippingCost(double shippingCost) {
this.shippingCost = shippingCost;
}
//This method will calculate the shipping cost
private void calculateCost() {
switch (shippingMethod) {
case 'A': {
if (weight >= 1 && weight <= 8) {
shippingCost = 2.00;
} else if (weight >= 9 && weight <= 16) {
shippingCost = 3.00;
} else if (weight >= 17) {
shippingCost = 4.50;
}
}
case 'T': {
if (weight >= 1 && weight <= 8) {
shippingCost = 1.50;
} else if (weight >= 9 && weight <= 16) {
shippingCost = 2.35;
} else if (weight >= 17) {
shippingCost = 3.25;
}
}
case 'M': {
if (weight >= 1 && weight <= 8) {
shippingCost = 0.50;
} else if (weight >= 9 && weight <= 16) {
shippingCost = 1.50;
} else if (weight >= 17) {
shippingCost = 2.15;
}
}
}
}
//This method will display the value of the variables
public void display()
{
System.out.println("The Package weight :"+weight*0.0625+" pounds Ship Method :"+shippingMethod+" Cost :$"+shippingCost);
}
}
__________________________
InsuredPackage.java
public class InsuredPackage extends Package {
//parameterized constructor
public InsuredPackage(double weight, char shippingMethod) {
super(weight, shippingMethod);
if (getShippingCost() >= 0 && getShippingCost() <= 1.00)
{
setShippingCost(getShippingCost() + 2.45);
}
else if (getShippingCost() >= 1.01 && getShippingCost() <= 3.00)
{
setShippingCost(getShippingCost() + 3.95);
}
else if (getShippingCost() >= 3.01)
{
setShippingCost(getShippingCost() + 5.55);
}
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
System.out.println("Insured Packages :");
super.display();
return " ";
}
}
________________________
UsePackage.java
public class UsePackage {
public static void main(String[] args) {
Package p1,p2,p3;
InsuredPackage ip1,ip2,ip3;
p1=new Package(9.5, 'A');
System.out.println("Packages :");
p1.display();
p2=new Package(7.5, 'M');
System.out.println("Packages :");
p2.display();
p3=new Package(18.5, 'T');
System.out.println("Packages :");
p3.display();
ip1=new InsuredPackage(6.5, 'A');
ip1.toString();
ip2=new InsuredPackage(15.5, 'T');
ip2.toString();
ip3=new InsuredPackage(22.4, 'M');
ip3.toString();
}
}
____________________
Output:
Packages :
The Package weight :0.59375 pounds Ship Method :A Cost :$1.5
Packages :
The Package weight :0.46875 pounds Ship Method :M Cost :$0.5
Packages :
The Package weight :1.15625 pounds Ship Method :T Cost :$2.15
Insured Packages :
The Package weight :0.40625 pounds Ship Method :A Cost :$2.95
Insured Packages :
The Package weight :0.96875 pounds Ship Method :T Cost :$5.45
Insured Packages :
The Package weight :1.4 pounds Ship Method :M Cost :$6.1
____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.