Any other number of wheels will be reported as an
error. Use a switch statement to compute the decision.
import java.util.Scanner;
public class Wheels {
//A Java program to categorize a vehicle
public static void main( String[] args ) {
Scanner sc = new Scanner(System.in);
System.out.print( "How many wheels? " );
int number = sc.nextInt();
switch (number) {
case 2: case 3:
192 ANSWERS TO REVIEW QUESTIONS
System.out.println( "motorcycle" );
break;
case 4:
System.out.println( "car" );
break;
case 6: case 8: case 10: case 12:
case 14: case 16: case 18:
System.out.println( "truck" );
break;
default:
System.out.println( "Error: "
+ number + " wheels?" );
}
}
}
5.6 Write a Java class called Vehicle. The Vehicle class will have instance attributes for color, make,
model, speed, number of occupants, and maximum number of occupants. The Vehicle class will also
have a static variable called vehicleCount that can be used to track the number of vehicles in the
application.
The constructor for Vehicle should expect values for make, model, maximum number of occupants,
and color, and it should set the vehicle speed to zero, the number of occupants to 1, and increment the
count of vehicles each time the constructor is called.
Pages:
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512