92.3k views
1 vote
Write a class named Car that has the following data attributes:

__year_model (for the car's year model)

__make (for the make of the car)

__speed (for the car's current speed).

The Car Class should have an __init__ method that accepts the car's year model and make data attributes. It should also assign 0 to the __speed data attribute.

The class should also have the following methods:

accelerate - the accelerate method should add 5 to the speed data attribute each time it is called

brake - the brake method should subtract 5 from the speed data attribute each time it is called

get speed - the get_speed method should return the current speed

Next, design a program that creates a Car object, and then calls the accelerate method five times. After each call to the accelerate method, get the current speed of the car and display it. Then call the brake method five times. After each call to the brake method, get the current speed of the car and display it.

User Marjun
by
7.4k points

1 Answer

4 votes

ANSWER

The program in JAVA for the given scenario is as follows.

import java.util.*;

public class Car

{

// variables declaration

static int _year_model;

static String _make;

static int _speed;

public static void _init_( int model, String m )

{

// variables initialization

_year_model = model;

_make = m;

_speed = 0;

}

public static void accelerate()

{

_speed = _speed + 5;

}

public static void brake()

{

_speed = _speed - 5;

}

public static int get_speed()

{

// current value of speed is returned

return _speed;

}

public static void main(String[] args) {

// object of class created

Car ob = new Car();

// method called using object of class

ob._init_(2019, "audi");

ob.accelerate();

System.out.println("Accelerated speed " + ob.get_speed());

ob.accelerate();

System.out.println("Accelerated speed " + ob.get_speed());

ob.accelerate();

System.out.println("Accelerated speed " + ob.get_speed());

ob.accelerate();

System.out.println("Accelerated speed " + ob.get_speed());

ob.accelerate();

System.out.println("Accelerated speed " + ob.get_speed());

System.out.println();

ob.brake();

System.out.println("Speed after brake " + ob.get_speed());

ob.brake();

System.out.println("Speed after brake " + ob.get_speed());

ob.brake();

System.out.println("Speed after brake " + ob.get_speed());

ob.brake();

System.out.println("Speed after brake " + ob.get_speed());

ob.brake();

System.out.println("Speed after brake " + ob.get_speed());

}

}

OUTPUT

Accelerated speed 5

Accelerated speed 10

Accelerated speed 15

Accelerated speed 20

Accelerated speed 25

Speed after brake 20

Speed after brake 15

Speed after brake 10

Speed after brake 5

Speed after brake 0

EXPLANATION

1. Variables are declared with String and integer datatypes.

2. _init_() initializes all the variables. This method is also called inside main() using object of Car class in order to initialize the variable speed to 0.

3. Inside main(), object of Car class created.

Car ob = new Car();

4. All the methods are called using object of the class.

ob._init_(2019, "audi");

ob.accelerate();

ob.brake();

5. All the variables are declared as static.

6. All the methods are declared as static since main() method is static inside which all other methods are called.

7. New line is included to improve readability of the output.

User Bob Cross
by
6.1k points