Question

In: Computer Science

Create a Java class file for a Car class. In the File menu select New File......

  1. Create a Java class file for a Car class.
    1. In the File menu select New File...
    2. Under Categories: make sure that Java is selected.
    3. Under File Types: make sure that Java Class is selected.
    4. Click Next.
    5. For Class Name: type Car.
    6. For Package: select csci2011.lab7.
    7. Click Finish.
    8. A text editor window should pop up with the following source code (except with your actual name):
    1. csci1011.lab7;

      /**
      *
      * @author Your Name
      */
      public class Car {

      }
  1. Implement the Car class.
    1. Add the following private instance variables to the Car class:
      • An instance variable called model of type String.
      • An instance variable called color of type String.
      • An instance variable called year of type int.
      • An instance variable called currentSpeed of type double
    2. Add the following methods to the Car class:
      • A void method called initialize that takes two Strings, an int and a double as arguments, and uses those arguments to set the values of the model, color, year, and currentSpeed instance variables.
      • A void method called display that displays the model, color, year followed by the currentSpeed in parentheses. For example, if the model of the car is Ford, the color is Red, the year is 2018, and the current speed is 43 mph it should display Ford, Red, 2019 (43 mph).
    3. In the main method of your main class, create two Car objects and perform the following:
      • Initialize the car objects with a model, color, year, and current Speed of your choice, and display then. Your output might look like this:
    1. and display:
      Ford, Red, 2019 (43 mph)
      • For each instance variable change the value, then use the display method to make sure that the value changed.
    • Run the main program to see if the tests were successful.
      • Here is a sample output of the program.
      1. and display:
      2. Red, 2018 (43.0)

      1. Car's features
      2. Green, 2017 (43.0)

      1. and display:
      2. Yellow, 2019 (50.0)

      1. Car's features
      1. Purple, 2015 (70.0)

Solutions

Expert Solution

/***********************************Car.java******************************/


public class Car {

   /*
   * private instance variable
   */
   private String model;
   private String color;
   private int year;
   private double currentSpeed;

   /*
   * Default constructor
   */
   Car() {

       this.model = "";
       this.color = "";
       this.year = 2000;
       this.currentSpeed = 0.0;
   }

   /*
   * Parameterized constructor
   */
   public Car(String model, String color, int year, double currentSpeed) {
       this.model = model;
       this.color = color;
       this.year = year;
       this.currentSpeed = currentSpeed;
   }

   // getter and setter method for get and set the instance variables
   public String getModel() {
       return model;
   }

   public void setModel(String model) {
       this.model = model;
   }

   public String getColor() {
       return color;
   }

   public void setColor(String color) {
       this.color = color;
   }

   public int getYear() {
       return year;
   }

   public void setYear(int year) {
       this.year = year;
   }

   public double getCurrentSpeed() {
       return currentSpeed;
   }

   public void setCurrentSpeed(double currentSpeed) {
       this.currentSpeed = currentSpeed;
   }

   // display method
   public void display() {

       System.out.println(model + ", " + color + ", " + year + "(" + currentSpeed + ")");
   }

}
/*******************************TestCar.java********************************/


public class TestCar {

   public static void main(String[] args) {

       //instantiate a car object
       Car car = new Car("Ford", "Red", 2019, 43.0);

       //display car
       car.display();

       //modification in car's features
       car.setYear(2018);
       System.out.println("Car's features");
       car.display();

       car.setColor("Green");
       car.setYear(2017);
       System.out.println("Car's features");
       car.display();

       car.setColor("YELLOW");
       car.setYear(2019);
       car.setCurrentSpeed(50.0);
       System.out.println("Car's features");
       car.display();

       car.setColor("Purple");
       car.setYear(2015);
       car.setCurrentSpeed(70.0);
       System.out.println("Car's features");
       car.display();

   }
}
/***********************************output************************/

Ford, Red, 2019(43.0)
Car's features
Ford, Red, 2018(43.0)
Car's features
Ford, Green, 2017(43.0)
Car's features
Ford, YELLOW, 2019(50.0)
Car's features
Ford, Purple, 2015(70.0)

Please let me know if you have any doubt or modify the answer, Thanks :)


Related Solutions

Create a menu in Java. A menu is a presentation of options for you to select....
Create a menu in Java. A menu is a presentation of options for you to select. You can do this in the main() method. 1. Create a String variable named menuChoice. 2. Display 3 options for your program. I recommend using 3 System.out.println statements and a 4th System.out.print to show "Enter your Selection:". This needs to be inside the do -- while loop. A. Buy Stock. B. Sell Stock X. Exit Enter your Selection: 3. Prompt for the value menuChoice....
Class, Let's create a menu in Java. A menu is a presentation of options for you...
Class, Let's create a menu in Java. A menu is a presentation of options for you to select. You can do this in the main() method. 1. Create a String variable named menuChoice. 2. Display 3 options for your program. I recommend using 3 System.out.println statements and a 4th System.out.print to show "Enter your Selection:". This needs to be inside the do -- while loop. A. Deposit Cash. B. Withdraw Cash X. Exit Enter your Selection: 3. Prompt for the...
android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called...
android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called DataBaseManager as in Lecture 5 and create a database table in SQLite, called StudentInfo. The fields for the StudentInfo table include StudentID, FirstName, LastName, YearOfBirth and Gender. Include functions for adding a row to the table and for retrieving all rows, similar to that shown in lecture 5. No user interface is required for this question, t -Continuing from , follow the example in...
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED...
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED BELOW THIS QUESTION). Include the following: A variable Roof that holds the type of roof (Ex: Convertible, Hard-Top, Soft-Top) A variable Doors that holds the car’s number of doors (Ex: 2, 4) Implement the ChangeSpeed method to add 20 to the speed each time it is called. Add exception handling to the ChangeSpeed method to keep the speed under 65. Implement the sound method...
Create a new Java file, containing this code public class DataStatsUser { public static void main...
Create a new Java file, containing this code public class DataStatsUser { public static void main (String[] args) { DataStats d = new DataStats(6); d.append(1.1); d.append(2.1); d.append(3.1); System.out.println("final so far is: " + d.mean()); d.append(4.1); d.append(5.1); d.append(6.1); System.out.println("final mean is: " + d.mean()); } } This code depends on a class called DataStats, with the following API: public class DataStats { public DataStats(int N) { } // set up an array (to accept up to N doubles) and other member...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
This code in java: Create a class named car. A car has color, model, company, registration...
This code in java: Create a class named car. A car has color, model, company, registration number. You can stear a car. A car can move forward. A car has a gear box. A typical gear decide weather car is moving forward or backward. A person owns a car. Kindly add some other functionalities like refuel
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT