Question

CS1180 Lab 9 Students will learn how to create a user-defined class. Part 1. Create a user-defined class. 1. Follow the UML diagram below to create a user-defined class, Automobile When implementing the constructors, use the default value of 0 for numeric types and unknown for VehicleMake and VehicleModel when not given as a parameter Implement the toString method in a format ofyour choosing, as long as it clearly includes information for all four member variables Make sure to include Javadoc comments for each method of the class. Automobile -vehicleMake: String vehicleModel: String - num - estMPG: double berCylinders: int +Automobile 0 +Automobile (vehicleMake: String, vehicleModel : String) +Automobile (vehicleMake: String, vehicleModel : String, numberCylinders : int, estMPG: double) + setVehicleMake (String vehicleMake) : void + getVehicleMake O: String + setVehicleModel (String vehicleModel) void +getVehicleModel 0: String +setNumberCylinders ( numberCylinders : int): void +getNumberCylinders: int + setEstMPG(estMPG: double) void + getEstMPG ): double +toString0 String

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM CODE:

Automobile.java

package automobile;

/**

*

* @author your name

* Automobile class that talks about the make, model, cylinders and estimated MPG of a vehicle

*/

public class Automobile {

  

   private String vehicleMake;

   private String vehicleModel;

   private int numberCylinders;

   private double estMPG;

  

   /**

   * Constructor with two arguments - make and model

   * Default value for cylinders is 0

   * Default value for estimated MPG is 0

   * @param vehicleMake

   * @param vehicleModel

   */

   public Automobile(String vehicleMake, String vehicleModel) {

       this.vehicleMake = vehicleMake;

       this.vehicleModel = vehicleModel;

       this.numberCylinders = 0;

       this.estMPG = 0;

   }

  

   /**

   * Constructor with arguments for all the member variables

   * @param vehicleMake

   * @param vehicleModel

   * @param numberCylinders

   * @param estMPG

   */

   public Automobile(String vehicleMake, String vehicleModel, int numberCylinders, double estMPG) {

       this.vehicleMake = vehicleMake;

       this.vehicleModel = vehicleModel;

       this.numberCylinders = numberCylinders;

       this.estMPG = estMPG;

   }

  

   /**

   * Default constructor with default value of 'unknown' for make and model

   * Default value for cylinders is 0

   * Default value for estimated MPG is 0

   */

   public Automobile() {

       this.vehicleMake = "unknown";

       this.vehicleModel = "unknown";

       this.numberCylinders = 0;

       this.estMPG = 0;

   }

  

   /**

   *

   * @return make of the vehicle

   */

   public String getVehicleMake() {

       return vehicleMake;

   }

  

   /**

   * sets the make to the parameter

   * @param vehicleMake

   */

   public void setVehicleMake(String vehicleMake) {

       this.vehicleMake = vehicleMake;

   }

  

   /**

   *

   * @return model of the vehicle

   */

   public String getVehicleModel() {

       return vehicleModel;

   }

  

   /**

   * sets the model to the parameter

   * @param vehicleModel

   */

   public void setVehicleModel(String vehicleModel) {

       this.vehicleModel = vehicleModel;

   }

  

   /**

   *

   * @return number of cylinders

   */

   public int getNumberCylinders() {

       return numberCylinders;

   }

  

   /**

   * sets the number of cylinders to the parameter

   * @param numberCylinders

   */

   public void setNumberCylinders(int numberCylinders) {

       this.numberCylinders = numberCylinders;

   }

  

   /**

   *

   * @return estimated MPG

   */

   public double getEstMPG() {

       return estMPG;

   }

  

   /**

   * sets the estMPG to the parameter

   * @param estMPG

   */

   public void setEstMPG(double estMPG) {

       this.estMPG = estMPG;

   }

  

   /**

   * returns the vehicle information in one line

   */

   @Override

   public String toString() {

      

       return "Make: " + vehicleMake + ", Model: " + vehicleModel + ", Cylinders: " + numberCylinders + ", MPG: " + estMPG;

   }

}

AutomobileTester.java

package automobile;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

//Tester class

public class AutomobileTester {

   /**

   * This method reads the automobile data from the file and adds then into the automobile array

   * @param filename - name of the file to be read

   * @return an array of automobiles

   */

   public static Automobile[] readFromFile(String filename)

   {

       Scanner fileReader;

       Automobile automobiles[] = null;

       try {

           fileReader = new Scanner(new File(filename));

           int numberOfAutomobiles = Integer.valueOf(fileReader.nextLine());

           automobiles = new Automobile[numberOfAutomobiles];

           for(int i=0; i<numberOfAutomobiles; i++)

           {

               Automobile automobile = new Automobile(fileReader.nextLine(), fileReader.nextLine(),

                       Integer.valueOf(fileReader.nextLine()), Double.valueOf(fileReader.nextLine()));

               automobiles[i] = automobile;

           }

           fileReader.close();

       } catch (FileNotFoundException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

      

       return automobiles;

   }

  

   /**

   * this method displays all the vehicle information that matches the 'make'

   * @param automobiles - array of automobiles

   * @param make - the make of the vehicle that needs to be searched

   */

   public static void displayVehicles(Automobile automobiles[], String make)

   {

       System.out.println("\nMake\t\tModel\tNum of Cylinders Estimated MPG\n");

       for(int i=0; i<automobiles.length; i++)

       {

           if(automobiles[i].getVehicleMake().equalsIgnoreCase(make))

           {

               System.out.printf("%-10s\t%-10s\t%d\t\t%.1f\n", automobiles[i].getVehicleMake() , automobiles[i].getVehicleModel() ,

                       automobiles[i].getNumberCylinders() , automobiles[i].getEstMPG());

           }

       }

   }

  

   public static void main(String[] args) {

       //Part 1 of the assignment

       //creating separate vehicles

       Automobile vehicle1 = new Automobile();

       Automobile vehicle2 = new Automobile("Toyota", "Crysta");

       Automobile vehicle3 = new Automobile("Hyundai", "EON", 3, 62);

       //printing the vehicle information

       System.out.println(vehicle1);

       System.out.println(vehicle2);

       System.out.println(vehicle3);

      

       //using accessors and mutators

       vehicle1.setVehicleMake("Volkswagen");

       vehicle1.setVehicleMake("Polo");

       vehicle1.setNumberCylinders(4);

       vehicle1.setEstMPG(80.7);

      

       System.out.println("Make: " + vehicle1.getVehicleMake());

       System.out.println("Model: " + vehicle1.getVehicleModel());

       System.out.println("Cylinders: " + vehicle1.getNumberCylinders());

       System.out.println("MPG: " + vehicle1.getEstMPG());

      

       /*This is part 2 of assignment. Calling a function to read a file of automobiles and then searching only for

       a particular make and displaying all the vehicles of that particular make

       */

      

       Automobile automobiles[] = readFromFile("Lab9AutoData.txt");

       Scanner reader = new Scanner(System.in);

       String make = "";

       while(true)

       {

           System.out.print("\nEnter Make: ");

           make = reader.nextLine();

           if(make.equalsIgnoreCase("done"))

               break;

           else displayVehicles(automobiles, make);

              

       }

       reader.close();

   }

}

INPUT:

Automobile.java AutomobileTester.javaab9AutoData.txt 3 1 3 2 Volkswagen 3 Polo 5 80.7 6 Hyundai. 7 Etimos 8 3 9 62 10 Volkswa

OUTPUT:

DeclarationProblems SearchConsole 3 <terminated> AutomobileTester [Java Application] /Library Java/JavaVirtualMachines/jdk1.8

Add a comment
Know the answer?
Add Answer to:
CS1180 Lab 9 Students will learn how to create a user-defined class. Part 1. Create a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

  • What this Lab Is About: Given a UML diagram, learn to design a class Learn how...

    What this Lab Is About: Given a UML diagram, learn to design a class Learn how to define constructor, accessor, mutator and toStringOmethods, etc Learn how to create an object and call an instance method. Coding Guidelines for All ments You will be graded on this Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc) Keep identifiers to a reasonably short length. Use upper case for constants. Use title case (first letter is u case)...

  • The current code I have is the following: package uml; public class uml {        public...

    The current code I have is the following: package uml; public class uml {        public static void main(String[] args) {              // TODO Auto-generated method stub        } } class Account { private String accountID; public Account(String accountID) { this.accountID = accountID; } public String getAccountID() { return accountID; } public void setAccountID(String accountID) { this.accountID = accountID; } @Override public String toString() { return "Account [accountID=" + accountID + "]"; } } class SuppliesAccount extends Account { private...

  • C++ Please create the class named BankAccount with the following properties below: // The BankAccount class...

    C++ Please create the class named BankAccount with the following properties below: // The BankAccount class sets up a clients account (name & ID), keeps track of a user's available balance. // It also keeps track of how many transactions (deposits and/or withdrawals) are made. public class BankAccount { Private String name private String id; private double balance; private int numTransactions; // Please define method definitions for Accessors and Mutator functions below Private member variables string getName(); // No set...

  • Create a class called Restaurant that is the base class for all restaurants. It should have...

    Create a class called Restaurant that is the base class for all restaurants. It should have attributes for the restaurant's name{protected) and seats (private attribute that represents the number of seats inside the restaurant). Use the UML below to create the methods. Note, the toString prints the name and the number of seats. Derive a class Fastfood from Restaurant This class has one attribute - String slogan (the slogan that the restaurants uses when advertising - e.g., "Best Burgers Ever!")....

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and...

    The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and writing binary files. In this application you will modify a previous project. The previous project created a hierarchy of classes modeling a company that produces and sells parts. Some of the parts were purchased and resold. These were modeled by the PurchasedPart class. Some of the parts were manufactured and sold. These were modeled by the ManufacturedPart class. In this you will add a...

  • Diego’s Bike Lab Lab Objectives • Be able to create both default and non-default constructors •...

    Diego’s Bike Lab Lab Objectives • Be able to create both default and non-default constructors • Be able to create accessor and mutator methods • Be able to create toString methods • Be able to refer to both global and local variables with the same name Introduction Until now, you have just simply been able to refer to any variable by its name. This works for our purposes, but what happens when there are multiple variables with the same name?...

  • Horse Team(Java) . Horse -name:String -age:int -weight:double +Horse():void +Horse(String, int double):void +getName():String +getAge():int +getWeight():double +setName(String):void +setAge(int):void...

    Horse Team(Java) . Horse -name:String -age:int -weight:double +Horse():void +Horse(String, int double):void +getName():String +getAge():int +getWeight():double +setName(String):void +setAge(int):void +setWeight(double):void +compareName(String):boolean +compareTo(Horse):int +toString():String Team -team:ArrayList<Horse> +Team():void +addHorse(String, int, double):void +addHorse(Horse):void +averageAge():int +averageWeight():int +getHorse(int):Horse +getSize():int +findHorse(String):Horse +largestHorse():Horse +oldestHorse():Horse +removeHorse(String):void +smallestHorse():Horse +toString():String +youngestHorse():Horse HorseTeamDriver +main(String[]):void Create a class (Horse from the corresponding UML diagram) that handles names of horse, the age of the horse, and the weight of the horse. In addition to the standard accessor and mutator methods from the instance fields, the class...

  • You are to create a class Appointment.java that will have the following: 5 Instance variables: private...

    You are to create a class Appointment.java that will have the following: 5 Instance variables: private String month private int day private int year private int hour private int minute 1 default constructor public Appointment() 1 non-default constructor that accepts arguments for all instance variables, your constructor must call the setter methods below to set the values of the instance variables public Appointment(String monthPassed, int dayPassed, int yearPassed, int hourPassed, int minutePassed) 5 setter methods (one for each instance variable)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT