In: Computer Science
i.e. The base price for a package with volume <=1 is $3, for every unit increase in volume, the cost increases by $1
e.g. 1: a parcel with volume 4, the cost is $3+ $1 +$1 +$1 = $6
e.g. 2: a parcel with volume 2.5, the cost is $3 + $1.5 = $4.5
Code for your program is provided below. The code is explained in code comments. The output screenshot is provided in the last. If you need further clarification please ask in comments.
#################################################################################
CODE-->>
import java.util.Scanner;
//class Package
class Package
{
//private data members
private double length;
private double width;
private double height;
//Scanner object as private member
private Scanner input=new Scanner(System.in);
//default constructor
public Package()
{
length=1.0;
width=1.0;
height=1.0;
}
//parameterized constructor
public Package(double length, double width, double height) {
super();
this.length = length;
this.width = width;
this.height = height;
}
//copy constructor
public Package(Package p)
{
length=p.length;
width=p.width;
height=p.height;
}
//function to enter length
public void inputLength()
{
System.out.print("Enter length: ");
length=input.nextDouble();
}
//function to enter width
public void inputWidth()
{
System.out.print("Enter Width: ");
width=input.nextDouble();
}
//function to enter height
public void inputHeight()
{
System.out.print("Enter Height: ");
height=input.nextDouble();
}
//function to display dimesnsions
public void displayDimensions()
{
System.out.println("Dimensions are: "+length+"X"+width+"X"+height);
}
//calculate volume
public double calcVolume()
{
return length*width*height;
}
}
public class Shipment
{ //main
public static void main(String[] args)
{
Scanner s=new Scanner(System.in); //scanner object to read input
//variables to store value
double length=0,width=0,height=0;
double vol1=0,vol2=0,cost1=0,cost2=0;
System.out.println("Welcome to Jade Millers's shipping Calculator");
//enter diffrent dimesnsions of Package
System.out.print("Enter the length of first Package: ");
length=s.nextDouble(); //length
System.out.print("Enter the width of first Package: ");
width=s.nextDouble(); //width
System.out.print("Enter the height of first Package: ");
height=s.nextDouble(); //height
//creating package with dimesnsions
Package p1=new Package(length, width, height);
//enter dimensions of second
System.out.println("Enter dimesnsions of second Package:");
Package p2=new Package(); //creating object using default constructor
p2.inputHeight();
p2.inputLength();
p2.inputWidth();
//find volumes
vol1=p1.calcVolume();
vol2=p2.calcVolume();
//find cost
cost1=3+vol1-1;
cost2=3+vol2-1;
//if equal cost
if(cost1==cost2)
System.out.println("Cost for Both Packages is same: $"+cost1);
else //if cost is diff
{
if(cost1>cost2) //if first package is costly
{
if(cost1<2*cost2)
System.out.println("Package 1 Cost is slightly more than Package 2");
else if(cost1<3*cost2)
System.out.println("Package 1 is twice costly than Package 2");
else if(cost1<4*cost2)
System.out.println("Package 1 is tripple costly than Package 2");
else if(cost1<5*cost2)
System.out.println("Package 1 is quadruple costly than Package 2");
else //findig how many times it is costly
System.out.println("Package 1 is "+(int)(cost1/cost2)+"times costly than Package 2");
//display the diff
System.out.println("Package 1 is more costly than Package 2 with price difference of: $"+(cost1-cost2));
}
if(cost2>cost1) //package 2 costly
{
if(cost2<2*cost1) //slight difference
System.out.println("Package 2 Cost is slightly more than Package 1");
else if(cost2<3*cost1)
System.out.println("Package 2 is twice costly than Package 1");
else if(cost2<4*cost1) //tripple costly
System.out.println("Package 2 is tripple costly than Package 1");
else if(cost2<5*cost1)
System.out.println("Package 2 is quadruple costly than Package 1");
else //finding how many time costly and displaying
System.out.println("Package 2 is "+(int)(cost2/cost1)+"times costly than Package 1");
//display the diffrence
System.out.println("Package 2 is more costly than Package 1 with price difference of: $"+(cost2-cost1));
}
}
s.close(); //close scanner
}
}
######################################################################
OUTPUT