Question

In: Computer Science

Create a Java class named Package that contains the following: Package should have three private instance...

  1. Create a Java class named Package that contains the following:
  1. Package should have three private instance variables of type double named length, width, and height.
  1. Package should have one private instance variable of the type Scanner named input, initialized to System.in.
  1. No-args (explicit default) public constructor, which initializes all three double instance variables to 1.0.  
  1. Initial (parameterized) public constructor, which defines three parameters of type double, named length, width, and height, which are used to initialize the instance variables of same name.
  1. Public copy constructor, with a parameter of type Package, used to duplicate an existing Package object.
  1. Three public void methods named inputLength, inputWidth, and inputHeight. Each method will prompt the user for the appropriate property, and input a double value using the Scanner object input to initialize the instance variables
  1. A public void method named displayDimensions which prints the dimensions as length X width X height (each value separated by a “ X “).
  1. A public method of type double named calcVolume that calculates the volume and returns the result as a double value.
  1. Create a class named Shipment
  1. The program must ask for two separate packages to ship
  1. The program must calculate the cost difference using the difference in volume:

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

  1. Give the following (in order of priority):  
  1. If there is no difference, display the costs as the same  
  1. If the cost of one is less than twice the other, display that it is “slightly more than”  
  1. If the cost of one is less than three times the other, display that it is “twice”  
  1. If the cost of one is less than four times the other, display that it is “triple”  
  1. If the cost of one is less than five times the other, display that it is “quadruple”  
  1. Otherwise, display that as a calculated multiple
  1. The program must indicate the more costly package (if not same cost) and by how much  
  1. The program must calculate and display the appropriate message (including proper dimension and cost format)  
  1. Your code also does NOT (YET) need to worry if the user inputs an invalid value for the input (example: invalid length). We will implement this in a future assignment.
  1. The output of your code must match the samples.  
  1. You must change the title i.e. Welcome to John Doe’s shipping calculator!

Solutions

Expert Solution

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


Related Solutions

Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows:...
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows: student id, test1, test2, and test3. 2. Create one constructor with parameter values for all instance data fields. 3. Create getters and setters for all instance data fields. 4. Provide a method called calcAverage that computes and returns the average test score for an object to the driver program. 5. Create a displayInfo method that receives the average from the driver program and displays...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account....
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that stores the int value represented by this object. • A constructor that creates a MyInteger object for a default int value. What default value did you choose? What other values did you consider? Why did you make this choice? • A constructor that creates a MyInteger object for a specified int value. • A getter method, valueOf(), that returns value. • A setter method,...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
In JAVA please... Define a class named Payment that contains an instance variable "paymentAmount" (non-static member...
In JAVA please... Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type double that stores the amount of the payment and appropriate accessor (getPaymentAmount() ) and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment. Override toString() method to call the paymentDetails() method to print the contents of payment amount and any other details not included in paymentDetails(). Define a class named...
In java: -Create a class named Animal
In java: -Create a class named Animal
CS 209 Data Structure 3. a. Create a class named Point3D that contains 3 instance variables...
CS 209 Data Structure 3. a. Create a class named Point3D that contains 3 instance variables x, y, and z. b. Create a constructor that sets the variables. Also, create get and set methods for each variable. c. Create a toString() method. d. Make Point3D implement Comparable. Also, create a compareTo(Point3D other) method that compares based on the x-coordinate, then y-coordinate for tiebreakers, then z-coordinate for tiebreakers. For example, (1, 2, 5) comes before (2, 1, 4), which comes before...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT