Question

In: Statistics and Probability

1. You are to write a simple program with two classes. One controller class and a class to hold your object definition.

1. You are to write a simple program with two classes. One controller class and a class to hold your object definition. (Similar to what we used in class) 2. Use a package in your project, the package name should be xxxprojectname. xxx is your initials taken from the first three characters of your Cal Poly email address. 3. Read in the following from the JOptionPane input window: a. Customer First Name b. Customer Last Name c. Customer Phone Number (example: 9093873744) d. Number of Vehicles to be manufactured (example: 5) e. Number of fuel tanks to be mounted on the vehicle. 4. Make sure the following is entered correctly, otherwise display an error message and close the program: California State Polytechnic University, Pomona Computer Information Systems Department Introduction to Object-Oriented Programming with Java a. The first name of the customer is not blank b. The last name of the customer is not blank c. The phone number of the customer is not blank and is 10 characters d. The number of vehicles is between 1 and 10 (includes 1 and 10) e. The number of fuel tanks can only be (2,4,8,10, 15,20) 5. Compute the cost for manufacturing vehicles using the price of $500.19 per vehicle 6. Compute the cost for fuel tanks using the price of $2.15 per fuel cell. 7. For this project each vehicle tank holds 12 Fuel Cells and vehicles can have more than one tank. 8. Use the following formulas to perform the calculations: Cost for manufacturing = number of vehicles * manufacturing price Cost for fuel tanks = number of vehicles * number of tanks * number of fuel cells per tank * price of fuel cell 9. Then you are to test the program using the data listed below. In the case of the first customer enter the data that has errors as shown in the screenshots below. The program should prompt you to reenter the data until the data is valid. First Name Last Name Phone Number of Vehicles Number of Tanks Homer Simpson 9093429871 5 8 Bart Simpson 9093429872 2 2 Lisa Simpson 9093429873 4 15 Marge Simpson 9093429874 3 4 Make sure that you check that the data is valid when the user enters it, Note in the example below how some data entered is incorrect the program then asks you to enter it again.

USE these as start up code

package project2startup;

public class VehicleApp {

   public static void main(String[] args) {
      

   }

}

package project2startup;

public class VehicleFactory {
   //variables to hold our object properties
   private String name;
   private String phone;
   private int nbrVehicles =0;
   private int nbrTanks =0;
   private double manufactureCost =0;
   private double fuelTanksCost =0;
   private double subtotal =0;
   private double tax =0;
   private double total = 0;

   //constants to hold our pricing properties
   private final double VEHICLE_PRICE = 500.19;
   private final double FUELCELL_PRICE = 2.15;
  
   //you have to create a constant and store the tax rate in it
  
  
   public HayloFactory(String aName, String aPhone, int aNbrVehicles, int aNbrTanks){
       //add necessary code in here
  
   }
  
   //Add methods to perform your calculations here
   //for example you have to calculate the manufacturing cost and store it in manufactureCost
   public void calcManufacturingCost(){
      
   }
  
   //for example you have to calculate the fuel tanks cost and store it in fuelTankCost
   public void calcFuelTankCost(){
      
   }
  
   //add method to calculate the subtotal and store it in subtotal
   public void calcSubtotal(){
      
   }
  
   //add method to calculate the tax and store it in tax
   public void calcTax(){
      
   }
  
   //add method to calculate the total and store it in total
   public void calcTotal(){
      
   }
  
   //Add a method to return a summary for this object here
   public String getSummary(){
       //you have to build your summary and store it in the summary variable
      
       return null;
   }
}

Solutions

Expert Solution

Short Summary:

  • Created two classes
    • Controller class - Gets input from user
    • CustomerVehicle - Validates the inputs and returns true, if valid. Otherwise, it retruns false; And it calcualtes manufacturing cost for the vechicle
  • Verify the getManufacturingCost() method from your side. Adjust the calculations, if neccessary.
  • All the inputs have validated properly (for valid data type and satifies business rules)
  • As a good coding practice, all business rules should reside in your class file. So, all the set methods includes the buiness rules and returns true, if the input is valid. Controller gets the input again, if the setter method returns false.

Source Code:

CustomerVehicle.java File:

package xxxcustomervehicle;

import java.text.NumberFormat;

import java.util.Locale;

public class CustomerVehicle {

    //Private members goee here...

    private String firstName;

    private String lastName;

    private String phoneNumber;

    private int numVehicle;

    private int numFuelTank;

   

    //Decalre all constants here..

    private final static int[] VALID_FUEL_TANKS = {2, 4, 8, 10, 15, 20};

    private final static double MANUFACTURE_BASE_COST = 500.19;

    private final static double FUEL_CELL_COST = 2.15;

    private final static int NO_FUEL_CELL_PER_TANK = 12;

   

    //Default contructor, does nothing

    public CustomerVehicle() {

    }

    public String getFirstName() {

        return firstName;

    }

    public String getLastName() {

        return lastName;

    }

    public String getPhoneNumber() {

        return phoneNumber;

    }

    public int getNumVehicle() {

        return numVehicle;

    }

    public int getNumFuelTank() {

        return numFuelTank;

    }

    //Sets first name if it is not blank and returns true; otherwise, false

    public boolean setFirstName(String firstName) {

        if(firstName != null && firstName.length() != 0){

            this.firstName = firstName;

            return true;

        }

        return false;

    }

     //Sets last name if it is not blank and returns true; otherwise, false

    public boolean setLastName(String lastName) {

         if(lastName != null && lastName.length() != 0){

            this.lastName = lastName;

            return true;

        }

        return false;

    }

    //Sets phonenumber if it is not blank and 10 characters lenght and returns true;

    //otherwise, false

    public boolean setPhoneNumber(String phoneNumber) {

         if(phoneNumber != null && phoneNumber.length() == 10){

            this.phoneNumber = phoneNumber;

            return true;

        }

        return false;

    }

    //Sets numVechicle if the parameter value is between 1 and 10 and return true;

    //Otherwise false;

    public boolean setNumVehicle(int numVehicle) {

        if(numVehicle >= 1 && numVehicle <= 10){

            this.numVehicle = numVehicle;

            return true;

        }

        return false;

    }

    private boolean arrayContains(int[] array, int key){

        for(int value: array){

            if(value == key){

                return true;

            }

        }

        return false;

    }

   

    //Sets numVechicle if the parameter value is 2/4/8/10/15/20 and return true;

    //Otherwise false;

    public boolean setNumFuelTank(int numFuelTank) {

        if (arrayContains(VALID_FUEL_TANKS, numFuelTank)){

            this.numFuelTank = numFuelTank;

            return true;

        }

        return false;

    }

    //Calculates and returns vehicle manufacturing cost

    public double getManufacturingCost(){

        double manuCostFuelTank = numVehicle * numFuelTank *

                NO_FUEL_CELL_PER_TANK * FUEL_CELL_COST;

        double manuVehicleCost = (MANUFACTURE_BASE_COST * numVehicle) + manuCostFuelTank;

        return manuVehicleCost;

    }

   

    @Override

    public String toString() {

        return "Customer First Name = " + firstName

                + "\nCustomer Last Name = " + lastName

                + "\nPhone Number = " + phoneNumber

                + "\nNumber of Vehicle = " + numVehicle

                + "\nNumber of Fuel Tanks = " + numFuelTank

                + "\nTotal Manufacturing Cost = "

                + NumberFormat.getCurrencyInstance(new Locale("en", "US")).format(getManufacturingCost());

    }

}

Controller.java File:

package xxxcustomervehicle;

import javax.swing.JOptionPane;

public class Controller {

   

    //Displays input dialog with the given caption and returns the string entered by user

    static String getUserInput(String label){

        return JOptionPane.showInputDialog(null, label, "Input", JOptionPane.QUESTION_MESSAGE);

    }

  

    //Display the error message using JOptionPane.showMessageDialog

    static void showErrorMessage(String errmsg){

        JOptionPane.showMessageDialog(null, errmsg, "Error", JOptionPane.ERROR_MESSAGE);

    }

   

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

       

        CustomerVehicle newVehicle;

        //Create new vehicle

        newVehicle = new CustomerVehicle();

        //Get First name until it is valid

        do{

            //Get first name from user

            String firstName = getUserInput("Enter Customer First Name:");

            if(newVehicle.setFirstName(firstName)){

                break;

            }

            showErrorMessage("First name cannot be blank!");

        }while(true);

        //Get Last name until it is valid

        do{

            String lastName = getUserInput("Enter Customer Last Name:");

            if(newVehicle.setLastName(lastName)){

                break;

            }

           showErrorMessage("Last name cannot be blank!");

        }while(true);

        //Get phone number until it is valid

        do{

            String phoneNumber = getUserInput("Enter Customer Phone Number:");

            if(newVehicle.setPhoneNumber(phoneNumber)){

                break;

            }

            showErrorMessage("Invalid Phone number, please re-enter!");

        }while(true);

        //Get number of vehicle until it is valid

        do{

            String strVehicle = getUserInput("Number vehicles to be manufactured:");

            //Check if user enters valid integer

            int numVehicle = 0;

            try{

                numVehicle = Integer.parseInt(strVehicle);

            } catch(Exception ex){

                //do nothing

            }

            if(newVehicle.setNumVehicle(numVehicle)){

                break;

            }

            showErrorMessage("Number vehicle should be between 1 and 10!");

        }while(true);

       //Get number of vehicle until it is valid

        do{

            String strFuelTank = getUserInput("Number of fuel tanks to be mounted on the vehicle:");

            //Check if user enters valid integer

            int numFuelTank = 0;

           try{

                numFuelTank = Integer.parseInt(strFuelTank);

            } catch(Exception ex){

                //do nothing

            }

            if(newVehicle.setNumFuelTank(numFuelTank)){

                break;

            }

            showErrorMessage("Number of fuel tank can be 2, 4, 8, 10, 15 or 20!");

        }while(true);

           

        

       //Show the final output with Manufaturing cost

       JOptionPane.showMessageDialog(null, newVehicle, "Output", JOptionPane.INFORMATION_MESSAGE);

       

    }

   

}

Sample Run:



Related Solutions

Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel. A dog consists of the following information: • An integer age. • A string name. If the given name contains non-alphabetic characters, initialize to Wolfy. • A string bark representing the vocalization the dog makes when they ‘speak’. • A boolean representing hair length; true indicates short hair. • A float weight representing the dog’s weight (in pounds). • An enumeration representing the type...
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Write two classes Class A and Class B in class A you should read from the...
Write two classes Class A and Class B in class A you should read from the keyboard a number and you should call a public method of the class B that will print on the screen whether the number that was read from the keyboard in class A is multiple of 7 and has the last digit 5.
#1    Write  a simple array program that:    Des not use an “external class & demo      program"    [If you wish, you...
#1    Write  a simple array program that:    Des not use an “external class & demo      program"    [If you wish, you may break it down into methods, but that is NOT         required.]    a.     Set up 4 arrays which each hold 6 employee’s data:              int[ ] empid              int[ ] hours              double[ ] rate              double[ ] wages          b.     Set up loops to load the empid, hours and rate arrays    c.     Set up a loop to calculate values for the wages array. TAKE OVERTIME [hours > 40],  INTO CONSIDERATION, thru the use  of...
Create a simple Java class for a Month object with the following requirements:  This program...
Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...
Your objective is to write a well-documented simple program using classes, a loop, and nested ifs...
Your objective is to write a well-documented simple program using classes, a loop, and nested ifs to simulate an ATM using JAVA. 1. Create an ATM class with class variables name, pin, and balance, a constructor with parameters to assign values to the three instance variables, methods to get the name, pin, and balance, and methods to handle validated deposits and withdrawals ( deposit and withdrawal amounts cannot be negative, and withdrawal amount must not be greater than the existing...
Your objective is to write a well-documented simple program using classes, a loop, and nested ifs...
Your objective is to write a well-documented simple program using classes, a loop, and nested ifs to simulate an ATM using JAVA. 1. Create an ATM class with class variables name, pin, and balance, a constructor with parameters to assign values to the three instance variables, methods to get the name, pin, and balance, and methods to handle validated deposits and withdrawals ( deposit and withdrawal amounts cannot be negative, and withdrawal amount must not be greater than the existing...
Complete the Tree class within the following program: BalancingTrees.zip. There are three two classes and one...
Complete the Tree class within the following program: BalancingTrees.zip. There are three two classes and one driver in this program. You just need to complete or add the Tree class code. In java please. The code for tree class: public class Tree {    Node root;    public Tree() {        root = null;    }    public int getHeight(Node node) { if (node == null) return 0; return node.height; }    public Node insertANDbalance(Node node, int element) {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT