Create a class named Package with data fields for weight in ounces, shipping met
ID: 3695812 • Letter: C
Question
Create a class named Package with data fields for weight 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 6
2.00
1.50
.75
7 to 14
3.00
2.35
1.50
15 and over
4.50
3.25
2.25
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
2.65
1.01 to 3.00
3.95
3.01 and over
5.55
Write an application named PackageTest 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.
Weight (oz.)
Air ($)
Truck ($)
Mail ($)
1 to 6
2.00
1.50
.75
7 to 14
3.00
2.35
1.50
15 and over
4.50
3.25
2.25
Explanation / Answer
Package.java
package org.students;
public class Package {
private int weight;
private char shippingMethod;
public Package(int weight, char shippingMethod) {
this.weight = weight;
this.shippingMethod = shippingMethod;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public char getShippingMethod() {
return shippingMethod;
}
public void setShippingMethod(char shippingMethod) {
this.shippingMethod = shippingMethod;
}
public double calculateCost()
{
double cost=0;
char sm=this.shippingMethod;
//char convertedChar;
//String tempString;
//tempString = String.valueOf(sm);
//tempString = tempString.toUpperCase();
//convertedChar = tempString.charAt(0);
switch(sm)
{
case 'A':
if(weight>=1 && weight<=8)
{
cost=weight*2;
}
else if(weight>=9 && weight<=14)
{
cost=weight *3;
}
else if(weight>=15 )
{cost=weight * 4.5; }
break;
case 'T':
if(weight>=1 && weight<=8)
{
cost=weight*1.50;
}
else if(weight>=9 && weight<=14)
{
cost=weight *2.35;
}
else if(weight>=15 )
{cost= weight * 3.25;}break;
case 'M':
if(weight>=1 && weight<=8)
{
cost= weight*0.5;
}
else if(weight>=9 && weight<=14)
{
cost=weight *1.50;
}
else if(weight>=15 )
{cost= weight * 2.25;}
break;
}
return cost;
}
public void display()
{
System.out.println("Weight(OZ) Air($) Truck($) Mail($)");
System.out.println("1 to 8 2.00 1.50 0.75");
System.out.println("9 to 14 3.00 2.35 1.50");
System.out.println("15 and over 2.00 1.50 0.75");
}
}
______________________________________________________________________________________
InsuredPackage.java
package org.students;
public class InsuredPackage extends Package {
double shippingCharges;
public InsuredPackage(int weight, char shippingMethod) {
super(weight, shippingMethod);
}
public double calInsuredCost()
{
double totcost=0;
shippingCharges=calculateCost();
if(shippingCharges>=0 && shippingCharges<=1.0)
{
totcost = shippingCharges * 2.45;
}
else if(shippingCharges>=1.01 && shippingCharges<=3.0)
{
totcost=shippingCharges * 3.95;
}
else if(shippingCharges>=3.01 )
{
totcost=shippingCharges * 5.55;
}
return totcost;
}
}
________________________________________________________________________________________
Test.java
package org.students;
import java.text.DecimalFormat;
public class Test {
public static void main(String[] args) {
DecimalFormat df=new DecimalFormat("#.##");
Package pack=new Package(9,'A');
pack.display();
double costbefins=pack.calculateCost();
System.out.println("Total Cost of the delivery charges before Insurance through 'Air' ::"+costbefins+" $");
Package pack1=new Package(21,'T');
double costbefins1=pack1.calculateCost();
System.out.println("Total Cost of the delivery charges before Insurance through 'Truck'::"+costbefins1+" $");
InsuredPackage ip=new InsuredPackage(3,'M');
double costafterins=ip.calInsuredCost();
System.out.println("Total Cost of the delivery charges after Insurance ::"+df.format(costafterins)+" $");
}
}
__________________________________________________________________________________
output:
Weight(OZ) Air($) Truck($) Mail($)
1 to 8 2.00 1.50 0.75
9 to 14 3.00 2.35 1.50
15 and over 2.00 1.50 0.75
Total Cost of the delivery charges before Insurance through 'Air' ::27.0 $
Total Cost of the delivery charges before Insurance through 'Truck'::68.25 $
Total Cost of the delivery charges after Insurance ::5.93 $
___________________________________________________________________________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.