In: Computer Science
Hi,
Please find below code as per your requirement.
Added driver class MotorcycleTest.java to test functionality.
Let me know if you have any doubt/concern in answer via comments.
Hope this answer helps you.
Thanks.
/***********************JAVA CODE**********************/
/********************Motorcycle.java**********************/
/**
* This class used to perform accelerate and brake operation on
motorcycle
*
*/
public class Motorcycle {
//instance variable
private int year;
private String make;
private int speed;
/**
* Parameterized constructor which accepting 2
parameters and speed set to 0.
* @param year - year of motorcycle
* @param make - make of motorcycle
*/
public Motorcycle(int year, String make) {
this.year = year;
this.make = make;
this.speed = 0;
}
/**
* This is getter method for year
* @return the year
*/
public int getYear() {
return year;
}
/**
* This is getter method for make
* @return the make
*/
public String getMake() {
return make;
}
/**
* This is getter method for speed
* @return the speed
*/
public int getSpeed() {
return speed;
}
/**
* This method used to increase speed by 5.
*/
public void accelerate() {
this.speed += 5;
}
/**
* This method used to decrease the speed by 5 till
0.
*/
public void brake() {
if(this.speed <= 0) {
this.speed =
0;
} else {
this.speed -=
5;
}
}
}
/********************MotorcycleTest.java**********************/
import java.util.Scanner;
/**
* This class is driver class for Motorcycle class to test
functionality
*
*/
public class MotorcycleTest {
/**
* This main method is starting point of this driver
class which contains code to be tested.
* @param args
*/
public static void main(String[] args) {
//Scanner class is used to take
user input from console.
Scanner sc = new
Scanner(System.in);
//Prompting user to give input for
year
System.out.print("Enter the year
your motorcycle was made :");
//Storing user input in
variable
int year = sc.nextInt();
//Prompting user to give input for
make
System.out.print("Enter the make of
your motorcycle :");
//Storing user input in
variable
String make = sc.next();
//Created class using constructor
with user input given above
Motorcycle motorcycle = new
Motorcycle(year, make);
//Calling accelerate method 4 times
each time printing speed of motorcycle
motorcycle.accelerate();
System.out.println("A "+year+"
"+make+" going "+ motorcycle.getSpeed() +" miles per hour.");
motorcycle.accelerate();
System.out.println("A "+year+"
"+make+" going "+ motorcycle.getSpeed() +" miles per hour.");
motorcycle.accelerate();
System.out.println("A "+year+"
"+make+" going "+ motorcycle.getSpeed() +" miles per hour.");
motorcycle.accelerate();
System.out.println("A "+year+"
"+make+" going "+ motorcycle.getSpeed() +" miles per hour.");
//Calling brake method 3 times each
time printing speed of motorcycle
motorcycle.brake();
System.out.println("A "+year+"
"+make+" going "+ motorcycle.getSpeed() +" miles per hour.");
motorcycle.brake();
System.out.println("A "+year+"
"+make+" going "+ motorcycle.getSpeed() +" miles per hour.");
motorcycle.brake();
System.out.println("A "+year+"
"+make+" going "+ motorcycle.getSpeed() +" miles per hour.");
}
}