// package class import java.util.Random; public class Package { private double
ID: 3569098 • Letter: #
Question
// package class
import java.util.Random;
public class Package
{
private double packageNum;
public double weight;
private double air;
private double ground;
private double expt;
private double cost;
public Package(double cost, double packageNum, double weight, double air, double ground, double expt)
{
this.cost = cost;
this.packageNum = packageNum;
this.weight = weight;
this.air = air;
this.ground = ground;
this.expt = expt;
}
public Package()
{
}
public void setPackageID(double packageNum)
{
this.packageNum = packageNum;
}
public double getPackageID()
{
Random rand = new Random();
int pick = rand.nextInt(40)+1;
return pick;
}
public void setAir(double air)
{
this.air = air;
}
public double getAir()
{
if (weight <= 1)
{
System.out.println("Please enter the correct amount of weight.");
}
else
{
if (weight <= 8)
{
cost = weight * 2;
}
else if ((weight >= 9) && (weight <= 16))
{
cost = weight * 3;
}
else if (weight >= 17)
{
cost = weight * 4.50;
}
}
return cost;
}
public void setExpt(double expt)
{
this.expt = expt;
}
public double getExpt()
{
if (weight <= 8)
{
System.out.println("The Shipment of package will cost $" + weight * 0.50);
}
else
{
if ((weight >= 9) && (weight <= 16))
{
System.out.println("The Shipment of package will cost $" + weight * 1.50);
}
else
{
if (weight >= 17)
{
System.out.println("The Shipment of package will cost $" + weight * 2.15);
}
}
}
return getCost();
}
public void setGround(double ground)
{
this.ground = ground;
}
public double getGround()
{
if (weight <= 8)
{
System.out.println("The Shipment of package will cost $" + weight * 1.50);
}
else
{
if ((weight >= 9) && (weight <= 16))
{
System.out.println("The Shipment of package will cost $" + weight * 2.35);
}
else
{
if (weight >= 17)
{
System.out.println("The Shipment of package will cost $" + weight * 3.25);
}
}
}
return getCost();
}
public double getCost()
{
return cost;
}
public void setCost(double cost)
{
this.cost = cost;
}
}
//Insured Class
public class Insured extends Package
{
private double range;
public Insured (double cost, double packageNum, double weight, double air, double expt, double ground)
{
super(cost, packageNum, weight, air, expt, ground);
}
public Insured()
{
super();
}
public void setRange (double range)
{
this.range = range;
}
public double getRange()
{
if (getCost() >= 10 && getCost() <= 100)
{
return getCost() + 15;
}
if (getCost() >= 100.01 && getCost() <= 300)
{
return getCost() + 25.50;
}
if (getCost() >= 300.01)
{
return getCost() + 34;
}
return range;
}
}
// Owner Class
import java.util.*;
public class Owner extends Package
{
private double ownerNum;
private String name;
private String address;
public void setOwnerID(double ownerNum)
{
this.ownerNum = ownerNum;
}
public void setName(String name)
{
this.name = name;
}
public void setAddress(String address)
{
this.address = address;
}
public double getOwnerID()
{
Random rand = new Random();
int ownerPick = rand.nextInt(40) + 1;
return ownerPick;
}
public String getName()
{
return name;
}
public String getAddress()
{
return address;
}
}
//Main Tester
import java.awt.*;
import java.util.Scanner;
import javax.swing.*;
public class ShippingTester
{
public static void main (String[] args)
{
Scanner keyboard = new Scanner(System.in);
Package pf = new Package();
Owner of = new Owner();
Insured ip = new Insured();
int userChoice;
boolean quit = false;
System.out.println("Hello! Welcome to the shipping calculator kiosk.");
System.out.println ("Please Enter Your Name: ");
Owner ownerName = new Owner();
String name = keyboard.nextLine();
System.out.println ("Please Enter Your Address: ");
Owner ownerAddress = new Owner();
String address = keyboard.nextLine();
System.out.println();
System.out.println("Owner Name is: " + name
+ " Owner Address is: " + address
+ " Unique User ID is: " + of.getOwnerID()
+ " Package number " + pf.getPackageID() + " belongs to you.");
System.out.println();
do {
System.out.println("Please choose a service level:");
System.out.println("1. Air");
System.out.println("2. Expedited");
System.out.println("3. Ground");
System.out.println("0. Quit");
System.out.print("Please enter your choice: ");
userChoice = keyboard.nextInt();
switch (userChoice)
{
case 1:
System.out.println();
System.out.println ("You have choosen to ship by Air.");
System.out.println ("Please enter the package weight (lbs): ");
double weight = keyboard.nextDouble();
pf.weight = weight;
System.out.println();
System.out.println("Your shipment without insurance will cost: $" + pf.getAir());
ip.setCost(pf.getCost());
System.out.println("With insurance will cost: $" + ip.getRange());
break;
case 2:
System.out.println ("You have choosen to ship Expedited.");
System.out.print("Please enter the package weight (lbs): ");
double weight2 = keyboard.nextDouble();
pf.weight = weight2;
System.out.println("Your shipment without insurance will cost: $" + pf.getExpt());
ip.setCost(pf.getCost());
System.out.println("With insurance will cost $" + ip.getRange());
break;
case 3:
System.out.println ("You have choosen to ship Ground.");
System.out.print("Please enter the package weight (lbs): ");
double weight1 = keyboard.nextDouble();
pf.weight = weight1;
System.out.println("Your shipment without insurance will cost: $" + pf.getGround());
ip.setCost(pf.getCost());
System.out.println("With insurance will cost: $" + ip.getRange());
break;
case 0:
quit = true;
break;
default:
System.out.println("Wrong choice. Please Try again.");
break;
}
System.out.println();
}
while (!quit);
System.out.println("Thank you! Happy Shipping, please see clerk to finalize shipment.");
}
}
Above is my code for java project. I am trying to create a shipping program. However, my outputs for Air, Expedited, and Ground seem to be incorrect. They are being calculated incorrectly and outputing as 0.0. Can someone please help me correct my mistakes.
Explanation / Answer
Here you go :)
//Please comment if you have any doubts
//Package class
import java.util.Random;
public class Package
{
private double packageNum;
public double weight;
private double air;
private double ground;
private double expt;
private double cost;
public Package(double cost, double packageNum, double weight, double air, double ground, double expt)
{
this.cost = cost;
this.packageNum = packageNum;
this.weight = weight;
this.air = air;
this.ground = ground;
this.expt = expt;
}
public Package()
{
}
public void setPackageID(double packageNum)
{
this.packageNum = packageNum;
}
public double getPackageID()
{
Random rand = new Random();
int pick = rand.nextInt(40)+1;
return pick;
}
public void setAir(double air)
{
this.air = air;
}
public double getAir()
{
if (weight <= 1)
{
System.out.println("Please enter the correct amount of weight.");
}
else
{
if (weight <= 8)
{
cost = weight * 2;
}
else if ((weight >= 9) && (weight <= 16))
{
cost = weight * 3;
}
else if (weight >= 17)
{
cost = weight * 4.50;
}
}
return cost;
}
public void setExpt(double expt)
{
this.expt = expt;
}
public double getExpt()
{
if (weight <= 8)
{
System.out.println("The Shipment of package will cost $" + weight * 0.50);
}
else
{
if ((weight >= 9) && (weight <= 16))
{
System.out.println("The Shipment of package will cost $" + weight * 1.50);
}
else
{
if (weight >= 17)
{
System.out.println("The Shipment of package will cost $" + weight * 2.15);
}
}
}
return getCost();
}
public void setGround(double ground)
{
this.ground = ground;
}
public double getGround()
{
if (weight <= 8)
{
System.out.println("The Shipment of package will cost $" + weight * 1.50);
}
else
{
if ((weight >= 9) && (weight <= 16))
{
System.out.println("The Shipment of package will cost $" + weight * 2.35);
}
else
{
if (weight >= 17)
{
System.out.println("The Shipment of package will cost $" + weight * 3.25);
}
}
}
return getCost();
}
public double getCost()
{
return cost;
}
public void setCost(double cost)
{
this.cost = cost;
}
}
//Insured class
public class Insured extends Package {
private double range;
public Insured(double cost, double packageNum, double weight, double air,
double expt, double ground) {
super(cost, packageNum, weight, air, expt, ground);
}
public Insured() {
super();
}
public void setRange(double range) {
this.range = range;
}
public double getRange() {
if (getCost() >= 10 && getCost() <= 100) {
return getCost() + 15;
}
if (getCost() >= 100.01 && getCost() <= 300) {
return getCost() + 25.50;
}
if (getCost() >= 300.01) {
return getCost() + 34;
}
return range;
}
}
//Owner class
import java.util.*;
public class Owner extends Package {
private double ownerNum;
private String name;
private String address;
public void setOwnerID(double ownerNum) {
this.ownerNum = ownerNum;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
public double getOwnerID() {
Random rand = new Random();
int ownerPick = rand.nextInt(40) + 1;
return ownerPick;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
}
//ShippingTester class
import java.awt.*;
import java.util.Scanner;
import javax.swing.*;
public class ShippingTester {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Package pf = new Package();
Owner of = new Owner();
Insured ip = new Insured();
int userChoice;
boolean quit = false;
System.out.println("Hello! Welcome to the shipping calculator kiosk.");
System.out.println("Please Enter Your Name: ");
Owner ownerName = new Owner();
String name = keyboard.nextLine();
System.out.println("Please Enter Your Address: ");
Owner ownerAddress = new Owner();
String address = keyboard.nextLine();
System.out.println();
System.out.println("Owner Name is: " + name + " Owner Address is: "
+ address + " Unique User ID is: " + of.getOwnerID()
+ " Package number " + pf.getPackageID() + " belongs to you.");
System.out.println();
do {
System.out.println("Please choose a service level:");
System.out.println("1. Air");
System.out.println("2. Expedited");
System.out.println("3. Ground");
System.out.println("0. Quit");
System.out.print("Please enter your choice: ");
userChoice = keyboard.nextInt();
switch (userChoice) {
case 1:
System.out.println();
System.out.println("You have choosen to ship by Air.");
System.out.println("Please enter the package weight (lbs): ");
double weight = keyboard.nextDouble();
pf.weight = weight;
System.out.println();
System.out
.println("Your shipment without insurance will cost: $"
+ pf.getAir());
ip.setCost(pf.getCost());
System.out.println("With insurance will cost: $"
+ ip.getRange());
break;
case 2:
System.out.println("You have choosen to ship Expedited.");
System.out.print("Please enter the package weight (lbs): ");
double weight2 = keyboard.nextDouble();
pf.weight = weight2;
System.out
.println("Your shipment without insurance will cost: $"
+ pf.getExpt());
ip.setCost(pf.getCost());
System.out
.println("With insurance will cost $" + ip.getRange());
break;
case 3:
System.out.println("You have choosen to ship Ground.");
System.out.print("Please enter the package weight (lbs): ");
double weight1 = keyboard.nextDouble();
pf.weight = weight1;
System.out
.println("Your shipment without insurance will cost: $"
+ pf.getGround());
ip.setCost(pf.getCost());
System.out.println("With insurance will cost: $"
+ ip.getRange());
break;
case 0:
quit = true;
break;
default:
System.out.println("Wrong choice. Please Try again.");
break;
}
System.out.println();
} while (!quit);
System.out
.println("Thank you! Happy Shipping, please see clerk to finalize shipment.");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.