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.
In case of any query do comment. Please rate answer as well. Thanks
Code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
int carModelYear;
String carMake;
//take input from the user
Scanner scnr = new Scanner(System.in);
System.out.print("Enter the car model's year: ");
carModelYear = Integer.parseInt(scnr.nextLine());
System.out.print("Enter the car's make: ");
carMake = scnr.nextLine();
scnr.close(); //close the scanner
//print the output as shown
System.out.println("The model year is " + carModelYear);
System.out.println("The make is " + carMake);
System.out.println("The speed is 0");
System.out.println();
System.out.println("Let's see what it can do!!");
System.out.println("The speed is,...");
//for loop to increase the speed and display it on output window
for (int i=5;i <=150 ;i+=5 )
{
System.out.print(i + " ");
}
//asking for stop
System.out.println("STOP! STOP! Let me OUT!");
System.out.println("The speed is,...");
//decrease the speed by 5
for (int i=150;i >=0 ; i-=5 )
{
System.out.print(i + " ");
}
System.out.println(); //to print a new line
System.out.println("Whew. I'll just walk from here - thanks.");
}
}
==============screen shot of the code==========
Output: