Assignments pg. 540 PE6 Create a Project named Chap10x. Follow the instructions
ID: 3841606 • Letter: A
Question
Assignments pg. 540 PE6 Create a Project named Chap10x. Follow the instructions on pg 540 PE6 Note you need to create only 1 package, 1 insured package Display method displays values in 3 fields rather 4 Use pounds not ounces After You finish PE 6, zip the folder chap10x and upload For reference see pages 522 in the text book Make sure your code hasthe following comments at the beginning of your program with the appropriate information: //Filename written by Written on output t Packages 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
import java.io.*;
class Package //class Package
{
double w; //data fields declaration
char sm;
double sc;
double tc;
Package(double w1,char sm1) //superclass constructor
{
w=w1;
sm=sm1;
calculateCost();
}
public void calculateCost() //calculating cost
{
if(sm=='A')
{
if((w>=1)&&(w<=8))
{
sc=w*2.00;
}
else if((w>=9)&&(w<=16))
{
sc=w*3.00;
}
else if(w>=17)
{
sc=w*4.50;
}
}
if(sm=='T')
{
if((w>=1)&&(w<=8))
{
sc=w*1.50;
}
else if((w>=9)&&(w<=16))
{
sc=w*2.35;
}
else if(w>=17)
{
sc=w*3.25;
}
}
if(sm=='M')
{
if((w>=1)&&(w<=8))
{
sc=w*0.50;
}
else if((w>=9)&&(w<=16))
{
sc=w*1.50;
}
else if(w>=17)
{
sc=w*2.15;
}
}
}
public void display() //displaying all fields
{
System.out.println("Weight:"+w+' '+"Shipping Method:"+sm+' '+"Shipping Cost before insurance:"+sc+' '+"Total Cost:"+tc+' ');
}
}
class InsuredPackage extends Package //subclass declaration
{
InsuredPackage(double x, char y) //subclass constructor which calls superclass constructor
{
super(x,y);
}
public void addCost() //calculating insurance cost
{
if((sc>=0)&&(sc<=1.00))
{
tc=sc+2.45;
}
if((sc>=1.01)&&(sc<=3.00))
{
tc=sc+3.95;
}
if(sc>=3.01)
{
tc=sc+5.55;
}
}
}
class UsePackage //main class declaration
{
public static void main(String args[])
{
InsuredPackage p1=new InsuredPackage(3,'A'); //creating instances and calling different methods
p1.calculateCost();
p1.addCost();
p1.display();
InsuredPackage p2=new InsuredPackage(10,'T');
p2.calculateCost();
p2.addCost();
p2.display();
InsuredPackage p3=new InsuredPackage(20,'M');
p3.calculateCost();
p3.addCost();
p3.display();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.