我有四个班级
我要接受n辆车的信息 并以适当的形式显示信息。在获取数据时,询问用户有关 首先是车辆类型。
VEHICLEMAIN.JAVA
package InheritanceAssignment;
import InheritanceAssignment2.*;
import java.util.Scanner;
public class VehicleMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("How many no of vehicle details you wants to store");
int no_of_vehicles = sc.nextInt();
int count =0;
while(no_of_vehicles --> 0) {
System.out.println("Enter choice of vehicle for vehicle: "+count);
System.out.println("1------> Light Motor Vehicle");
System.out.println("2------> Heavy Motor Vehicle");
Scanner ch = new Scanner(System.in);
int choice = ch.nextInt();
Vehicle r = null;
switch(choice) {
case 1:
r = new LightMotorVehicle();
break;
case 2:
r = new HeavyMotorVehicle();
break;
default:System.out.println("Wrong Choice");
}
r.input_details();
r.display_details();
count = count + 1;
}
sc.close();
}
}
VEHICLE.JAVA
package InheritanceAssignment2;
import java.util.Scanner;
public class Vehicle {
String company;
double price;
public void input_details() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter company Name");
company = sc.next();
System.out.println("Enter Price");
price = sc.nextDouble();
sc.close();
}
public void display_details() {
}
}
LIGHTMOTORVEHICLE.JAVA
package InheritanceAssignment2;
public class LightMotorVehicle extends Vehicle{
int mileage;
final static String vehicle_type = "Light";
void display_details_light_motor() {
System.out.println("Vehicle Type: "+vehicle_type);
System.out.println("Mileage: "+mileage);
}
}
HEAVYMOTORVEHICLE.JAVA
package InheritanceAssignment2;
public class HeavyMotorVehicle extends Vehicle{
double capacity_in_tons;
final static String vehicle_type = "Heavy";
void display_details_light_motor() {
System.out.println("Vehicle Type: "+vehicle_type);
System.out.println("Capacity In Tons: "+capacity_in_tons);
}
}