Java programming kolkata Knight Riders vs x C Mi 111/4 (43 ov x W Class Files In
ID: 3841635 • Letter: J
Question
Java programming kolkata Knight Riders vs x C Mi 111/4 (43 ov x W Class Files Intro to Log x MyGate ckboard nocccd.edu/webapps/blackboard/content/listcontentisp?course id 1453 1&content; id Assignments pg. 540 PE6. Create a Project named Chap10x. Follow the instructions on pg 540 PE6Note you need create only 1package, linsured package to Display method displays values in 3 felds rather 4 Use pounds notounces After You finish PE 6, zip the folder chap10x and upload For reference see pages 522 in the textbook Make sure your code has the following comments at the beginning of your program with the appropriate information: ZZEile name written by //written on output The package weighs 4 pounds ship method A Cost $2.00 Insured packages The package weighs 6 pounds. Ship method T cost $5.45Explanation / 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.