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 Java class file for an Account class. In the File menu select New File......
Create a Java class file for an Account class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Account. For Package: select csci1011.lab8. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab8; /** * * @author Your Name */ public class Account { } Implement the Account...
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....
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. The article above that is in our discussion board may be helpful. 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...
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...
Start NetBeans. Create a new project called Lab7. Create a Java main class file using the...
Start NetBeans. Create a new project called Lab7. Create a Java main class file using the class name YourlastnameLab7 with your actual last name. Create a Java class file for a Polygon class. Implement the Polygon class. Add a private instance variable representing the number of sides in the polygon. Add a constructor that takes a single argument and uses it to initialize the number of sides. If the value of the argument is less than three, display an error...
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...
1. Create a1. Create a new java file named Name.java that should have Name class based...
1. Create a1. Create a new java file named Name.java that should have Name class based on new java file named Name.java that should have Name class based on the following UML Name - first: String - last: String + Name () + Name (String firstName, String lastName) + getName () : String + setName (String firstName, String lastName) : void + getFirst () : String + setFirst (String firstName) : void + getLast () : String + setLast (String...
Using the Java programming language: Create and implement a class Car that models a car. A...
Using the Java programming language: Create and implement a class Car that models a car. A Car is invented to have a gasoline tank that can hold a constant max of 12.5 gallons, and an odometer that is set to 0 for a new car. All cars have an original fuel economy of 23.4 miles per gallon, but the value is not constant. Provide methods for constructing an instance of a Car (one should be zero parameter, and the other...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT