In: Computer Science
Design a program in Java to display the following:
Car Design
Enter the car model's year: 1957
Enter the car's make: Chevy
The model year is 1957
The make is Chevy
The speed is 0
Let's see what it can do!!
The speed is,...
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110
115 120 125 130 135 140 145 150
STOP! STOP! Let me OUT!
The speed is,...
145 140 135 130 125 120 115 110 105 100 95 90 85 80 75 70 65 60 55
50 45 40 35 30 25 20 15 10 5 0
Whew. I'll just walk from here - thanks.
Have a look at the below code. I have put comments wherever required for better understanding.
import java.util.Scanner;
// create the car design class
class CarDesign{
// create instance variables
int year;
String maker;
int speed;
// create the constructior
CarDesign(int y,String m,int s){
this.year = y;
this.maker = m;
this.speed = s;
}
}
class Main {
public static void main(String[] args) {
// create object of scanner class
Scanner sc = new Scanner(System.in);
// take user input for year,maker and speed
System.out.println("Enter the car model's year: ");
int year = sc.nextInt();
System.out.println("Enter the car's make: ");
String maker = sc.nextLine();
System.out.println("Enter the speed of car: ");
int speed = sc.nextInt();
// create car design object
CarDesign cd = new CarDesign(year,maker,speed);
// now you can do anything from here
System.out.println(cd.year);
System.out.println(cd.maker);
System.out.println(cd.speed);
// If speed is greater than 150 you can print a warning message
}
}
Happy Learning!