Given the following blocks of Java code, add class headers, method headers and c
ID: 3805804 • Letter: G
Question
Given the following blocks of Java code, add class headers, method headers and comments that provide a brief description of what each does.
public class Package
{
private int weight;
private char method;
private double cost;
final char AIR = 'A';
final char TRUCK = 'T';
final int LOWWT = 9;
final double LOWAIR = 2.00;
final double LOWTRUCK = 1.50;
final double LOWMAIL = 0.50;
final int MEDWT = 17;
final double MEDAIR = 3.00;
final double MEDTRUCK = 2.35;
final double MEDMAIL = 1.50;
final double HIGHAIR = 4.50;
final double HIGHTRUCK = 3.25;
final double HIGHMAIL = 2.15;
public Package(int w, char m)
{
weight = w;
method = m;
cost = calculateCost(w, m);
}
private double calculateCost(int w, char m)
{
double c;
if(w < LOWWT)
{
if(m == AIR)
c = LOWAIR;
else
if(m == TRUCK)
c = LOWTRUCK;
else
c = LOWMAIL;
}
else
if(w < MEDWT)
{
if(m == AIR)
c = MEDAIR;
else
if(m == TRUCK)
c = MEDTRUCK;
else
c = MEDMAIL;
}
else
{
if(m == AIR)
c = HIGHAIR;
else
if(m == TRUCK)
c = HIGHTRUCK;
else
c = HIGHMAIL;
}
return c;
}
public void display()
//money format//
{
System.out.println("The package weighs " +
weight + " pounds. Ship method " + method +
"Cost $" + cost);
}
public double getCost()
//get cost method//
{
return cost;
}
public void increaseCost(double c)
{
cost = cost + c;
}
}
public class InsuredPackage extends Package
{
InsuredPackage(int w, char m)
{
super(w, m);
final double LOWCOST = 1.01;
final double MEDCOST = 3.01;
final double LOWINS = 2.45;
final double MEDINS = 3.95;
final double HIGHINS = 5.55;
double i;
if(getCost() < LOWCOST)
i = LOWINS;
else
if(getCost() < MEDCOST)
i = MEDINS;
else
i = HIGHINS;
increaseCost(i);
}
}
public class UsePackage
{
public static void main(String args[])
{
Package p1 = new Package(4,'A'),
p2 = new Package(10,'T'),
p3 = new Package(20,'M');
InsuredPackage p4 = new InsuredPackage(4,'A'),
p5 = new InsuredPackage(10,'T'),
p6 = new InsuredPackage(20,'M');
System.out.println("Packages:");
p1.display();
p2.display();
p3.display();
System.out.println("Insured packages:");
p4.display();
p5.display();
p6.display();
}
}
Explanation / Answer
// class to check weight and method of the delivering Package and calculate cost
public class Package
{
private int weight;
private char method;
private double cost;
final char AIR = 'A';
final char TRUCK = 'T';
final int LOWWT = 9;
final double LOWAIR = 2.00; //define lows for Air,Truck and mail packages
final double LOWTRUCK = 1.50;
final double LOWMAIL = 0.50;
final int MEDWT = 17; //define lows for Air,Truck and mail packages
final double MEDAIR = 3.00;
final double MEDTRUCK = 2.35;
final double MEDMAIL = 1.50;
final double HIGHAIR = 4.50; //define lows for Air,Truck and mail packages
final double HIGHTRUCK = 3.25;
final double HIGHMAIL = 2.15;
public Package(int w, char m) //Package constructor
{
weight = w;
method = m;
cost = calculateCost(w, m); //calculate the cost of Package based on weight and method used
}
private double calculateCost(int w, char m)
{
double c;
if(w < LOWWT)
{
if(m == AIR) //if method is AIR and weight is less than LOWWT cost = LOWAIR
c = LOWAIR;
else
if(m == TRUCK) //if method is TRUCK and weight is less than LOWWT cost = LOWTRUCK
c = LOWTRUCK;
else
c = LOWMAIL; //if method is Mail and weight is less than LOWWT cost = LOWMAIL
}
else
if(w < MEDWT) //for medium weight
{
if(m == AIR)
c = MEDAIR; //method = AIR cost = MEDAIR
else
if(m == TRUCK)
c = MEDTRUCK; //method = TRUCK cost = MEDTRUCK
else
c = MEDMAIL; //method = Mail cost = MEDMAIL
}
else //high cost
{
if(m == AIR)
c = HIGHAIR;
else
if(m == TRUCK)
c = HIGHTRUCK;
else
c = HIGHMAIL;
}
return c;
}
//display package weight ,shipping method and cost
public void display()
//money format//
{
System.out.println("The package weighs " +
weight + " pounds. Ship method " + method +
"Cost $" + cost);
}
//get method for cost
public double getCost()
//get cost method//
{
return cost;
}
//method to increase cost
public void increaseCost(double c)
{
cost = cost + c;
}
}
//derived class from Package
public class InsuredPackage extends Package
{
InsuredPackage(int w, char m)
{
//call base class constructor passing weight and method as arguments
super(w, m);
//define constants for low,medium and high costs and insurances
final double LOWCOST = 1.01;
final double MEDCOST = 3.01;
final double LOWINS = 2.45;
final double MEDINS = 3.95;
final double HIGHINS = 5.55;
double i;
if(getCost() < LOWCOST) // if cost less than LOWCOST ,low insurance
i = LOWINS;
else
if(getCost() < MEDCOST) // if cost less than MedCOST ,med insurance
i = MEDINS;
else
i = HIGHINS; // if cost less than highCOST ,high insurance
increaseCost(i); //add insurance to the cost of delivery
}
}
public class UsePackage
{
public static void main(String args[])
{
//create package instances with weight and method and calculate total cost for package delivery plus insurance
Package p1 = new Package(4,'A'),
p2 = new Package(10,'T'),
p3 = new Package(20,'M');
InsuredPackage p4 = new InsuredPackage(4,'A'),
p5 = new InsuredPackage(10,'T'),
p6 = new InsuredPackage(20,'M');
System.out.println("Packages:");
p1.display();
p2.display();
p3.display();
System.out.println("Insured packages:");
p4.display();
p5.display();
p6.display();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.