Objective Instructor notes The only constructor has two parameters: yearModel. F
ID: 3579956 • Letter: O
Question
Objective Instructor notes The only constructor has two parameters: yearModel. First program to create a class and test t then make. Write a class constructor Use literal values to create the car objects. In other Write accessor and mutator methods words, do not prompt the user for input. Example, Car myc new Car (2011 Hyundai Background Reading Use the appropriate mutator method to change the make of the car. ZyBooks Chapter 7 Assignment Write a class named Car that has the folowing fields: yearModel. The yearModel field is an int that holds the car's year model make. The make field references a string object that holds the make of the car speed. The spped field is an int that holds the car's current speed. In addition, the class should have the folowing constructor and other methods: Constructor. The constructor should accept the car's year model and make as arguments These values should be assigned to the object's yearModel and make fields. The constructor should also assign 0 to the speed field. Accessors. Appropriate accessor methods should get the values stored in an object's yearModel, make, and speed selds. Remember in the method header to capitalize the field after prepending 'get or 'set" to the field name. accelerate. The accelerate method should add 5 to the speed feld each time is called. brake. The brake method should subtract 5 from the speed field each timeit is called. Using the Car class Demonstrate the class in a program that creates a car and then cals the accelerate method swe times. After each call to the accelerate methods, get the current speed of the car and display object, smes. oach cal to the brake method, get the current it. Then cal the brake method, five After speed of the car and display it. mission 7.19.1: Lab 8 Due 13-Dec-16Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class Car
{
private int yearModel;
private String make;
private int speed;
public Car(int yearModel,String make) //constructor
{
this.yearModel = yearModel;
this.make = make;
this.speed = 0; //speed = 0
}
public int getYearModel() //accessor and mutator methods
{
return yearModel;
}
public String getMake()
{
return make;
}
public void setMake(String make)
{
this.make = make;
}
public int getSpeed()
{
return speed;
}
public void accelerate()
{
speed = speed +5; //speed is increased by 5
}
public void brake()
{
speed = speed -5; //speed is decreased by 5
}
}
class TestCar
{
public static void main (String[] args)
{
Car car = new Car(2011,"Hyundai");
System.out.println("Car Model Year : "+car.getYearModel()+" Make: "+car.getMake());
System.out.println("Accelerating........"); //calling accelerate() 5 times
car.accelerate();
System.out.println("Speed : "+car.getSpeed());
car.accelerate();
System.out.println("Speed : "+car.getSpeed());
car.accelerate();
System.out.println("Speed : "+car.getSpeed());
car.accelerate();
System.out.println("Speed : "+car.getSpeed());
car.accelerate();
System.out.println("Speed : "+car.getSpeed());
System.out.println("Braking.............."); //calling brake 5 times
car.brake();
System.out.println("Speed : "+car.getSpeed());
car.brake();
System.out.println("Speed : "+car.getSpeed());
car.brake();
System.out.println("Speed : "+car.getSpeed());
car.brake();
System.out.println("Speed : "+car.getSpeed());
car.brake();
System.out.println("Speed : "+car.getSpeed());
}
}
output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.