Package Delivery Program A package-delivery company provides shipping service fo
ID: 3761196 • Letter: P
Question
Package Delivery Program
A package-delivery company provides shipping service for customers. Write a program to simulate the shipping service.
Define a class Package (Package.java)
Private instance data members: package id, weight (in ounces) and cost per ounce to ship the package.
Two overloaded constructors. Constructor should initialize instance data members (not static data members). Ensure that the weight and cost per ounce contain positive values.
Public getters and setters for instance data members. Ensure that the numeric values are greater than zero.
A public method calculateDeliveryCost that calculates and returns a double indicating the cost associated with shipping the package.
toString method.
Package
- packageID : int
- weightInOunce : double
- costPerOunce : double
+ Package ()
+ Package (int, double, double)
+ setPackageID(int) : void
+ getPackageID() : int
+ setWeightInOunce(double) : void
+ getWeightInOunce() : double
+ setCostPerOunce(double) : void
+ getCostPerOunce() : double
+ calculateDeliveryCost(): double
+ toString(): String
Write a client program that uses the Package class to simulate the delivery service.
Package
- packageID : int
- weightInOunce : double
- costPerOunce : double
+ Package ()
+ Package (int, double, double)
+ setPackageID(int) : void
+ getPackageID() : int
+ setWeightInOunce(double) : void
+ getWeightInOunce() : double
+ setCostPerOunce(double) : void
+ getCostPerOunce() : double
+ calculateDeliveryCost(): double
+ toString(): String
Explanation / Answer
public class client
{
public static void main(String[] args)
{
Package p=new Package(123,12.9,10);
System.out.println(p.toString());
p.setPackageID(120);
p.setWeightOunce(20);
System.out.println(p.toString());
}
}
package mani;
public class Package
{
private int packageID;
private double weightInOunce;
private double costPerOunce;
public Package(){
}
public Package(int pid,double weight,double costp){
if(pid>0||weight>0||costp>0){
packageID=pid;
weightInOunce=weight;
costPerOunce=costp;
}else{
System.out.println("Only postive values details not stored");
}
}
public void setPackageID(int i){
if(i>0){
packageID=i;
}else{
System.out.println("Only postive values details not stored");
}
}
public int getPackageID(){
return packageID;
}
public void setWeightOunce(double i){
if(i>0){
weightInOunce=i;
}else{
System.out.println("Only postive values details not stored");
}
}
public double getWeightOunce(){
return weightInOunce;
}
public void setCostPerOunce(double i){
if(i>0){
costPerOunce=i;
}else{
System.out.println("Only postive values details not stored");
}
}
public double getCostPerOunce(){
return costPerOunce;
}
public double calculateDeliveryCost(){
return weightInOunce*costPerOunce;
}
public String toString(){
return "Package ID: "+getPackageID()+" Weight of the Package: "+getWeightOunce()+" Cost of the Package: $"+calculateDeliveryCost();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.