Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program #10: Create a class named Package with data fields for wright in ounces,

ID: 3735750 • Letter: P

Question

Program #10: Create a class named Package with data fields for wright in ounces, shipping method, and shipping cost. The shipping method is a character: A for air, T for truck, or M for mail. The Package class contains a constructor that requires arguments for weight and shipping method. The constructor calls a calculateCost() method that determines the shipping cost, based on the following table: Weight (oz.) Air ($) Truck ($) Mail ($) 1 to 8 9 to 16 17 and over 2.00 3.00 4.50 1.50 2.35 3.25 .50 1.50 2.15 The package class also contains a display() method that displays the values in all four fields. Create a subclass named InsuredPackage that adds an insurance cost to the shipping cost, based on the following table: Shipping Cost Before Insurance ($) Additional Cost ($) 0 to 1.00 1.01 to 3.00 3.01 and over 2.45 3.95 5.55 Write an application named UsePackage that instantiates at least three objects of each type (Package and InsuredPackage) using a variety of weights and shipping method codes. Display the results for each Package and InsuredPackage. Save the files as Package.java, InsuredPackage.java, and UsePackage.java.

Explanation / Answer

Please find my implementation:

/****************        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

Please rate my answer if it helped you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote