Q. Write a Java program to create a super class Vehicle having members Company and price. Derive 2 different classes LightMotorVehicle (members – mileage) and HeavyMotorVehicle (members – capacity-in-tons). Accept the information for n vehicles and display the information in appropriate form. While taking data, ask the user about the type of vehicle first.
Program:
import java.io.*;
class Vehicle{
String company;
double price;
public void accept() throws IOException{
System.out.println("Enter the Company and price of the Vehicle: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
company=br.readLine();
price=Double.parseDouble(br.readLine());
}
public void display(){
System.out.println("Company: "+company+" Price: "+price);
}
}
class LightMotorVehicle extends Vehicle{
double mileage;
public void accept() throws IOException{
super.accept();
System.out.println("Enter the mileage of the vehicle: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
mileage=Double.parseDouble(br.readLine());
}
public void display(){
super.display();
System.out.println("Mileage: "+mileage);
}
}
class HeavyMotorVehicle extends Vehicle{
double captons;
public void accept() throws IOException{
super.accept();
System.out.println("Enter the capacity of vehicle in tons: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
captons=Double.parseDouble(br.readLine());
}
public void display(){
super.display();
System.out.println("Capacity in tons: "+captons);
}
}
public class sa3 {
public static void main(String [] args) throws IOException{
int i;
System.out.println("Enter the type of vehicle: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("1.Light Vehicle");
System.out.println("2.Heavy Vehicle");
int ch=Integer.parseInt(br.readLine());
switch(ch){
case 1:
System.out.println("Enter the number of Light vehicles: ");
int n=Integer.parseInt(br.readLine());
LightMotorVehicle [] l=new LightMotorVehicle[n];
for(i=0;i<n;i++){
l[i]=new LightMotorVehicle();
l[i].accept();
}
for(i=0;i<n;i++){
l[i].display();
}
break;
case 2:
System.out.println("Enter the number of Heavy vehicles: ");
int m=Integer.parseInt(br.readLine());
HeavyMotorVehicle [] h=new HeavyMotorVehicle[m];
for(i=0;i<m;i++){
h[i]=new HeavyMotorVehicle();
h[i].accept();
}
for(i=0;i<m;i++){
h[i].display();
}
break;
}
}
}
Program:
import java.io.*;
class Vehicle{
String company;
double price;
public void accept() throws IOException{
System.out.println("Enter the Company and price of the Vehicle: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
company=br.readLine();
price=Double.parseDouble(br.readLine());
}
public void display(){
System.out.println("Company: "+company+" Price: "+price);
}
}
class LightMotorVehicle extends Vehicle{
double mileage;
public void accept() throws IOException{
super.accept();
System.out.println("Enter the mileage of the vehicle: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
mileage=Double.parseDouble(br.readLine());
}
public void display(){
super.display();
System.out.println("Mileage: "+mileage);
}
}
class HeavyMotorVehicle extends Vehicle{
double captons;
public void accept() throws IOException{
super.accept();
System.out.println("Enter the capacity of vehicle in tons: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
captons=Double.parseDouble(br.readLine());
}
public void display(){
super.display();
System.out.println("Capacity in tons: "+captons);
}
}
public class sa3 {
public static void main(String [] args) throws IOException{
int i;
System.out.println("Enter the type of vehicle: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("1.Light Vehicle");
System.out.println("2.Heavy Vehicle");
int ch=Integer.parseInt(br.readLine());
switch(ch){
case 1:
System.out.println("Enter the number of Light vehicles: ");
int n=Integer.parseInt(br.readLine());
LightMotorVehicle [] l=new LightMotorVehicle[n];
for(i=0;i<n;i++){
l[i]=new LightMotorVehicle();
l[i].accept();
}
for(i=0;i<n;i++){
l[i].display();
}
break;
case 2:
System.out.println("Enter the number of Heavy vehicles: ");
int m=Integer.parseInt(br.readLine());
HeavyMotorVehicle [] h=new HeavyMotorVehicle[m];
for(i=0;i<m;i++){
h[i]=new HeavyMotorVehicle();
h[i].accept();
}
for(i=0;i<m;i++){
h[i].display();
}
break;
}
}
}
Output:
Enter the type of vehicle:
1.Light Vehicle
2.Heavy Vehicle
1
Enter the number of Light vehicles:
2
Enter the Company and price of the Vehicle:
yamaha
12000
Enter the mileage of the vehicle:
12
Enter the Company and price of the Vehicle:
suziki
14000
Enter the mileage of the vehicle:
13
Company: yamaha Price: 12000.0
Mileage: 12.0
Company: suziki Price: 14000.0
Mileage: 13.0
2 Comments
Write a program consisting of a base class named vehicle. Drive two classes loader_vehicle and passenger_cars respectively. Drive the loader class publically and the passenger car class as a friend class. Show the impact of different access specifier by having or by not having the access to all the member variables or member functions in the base class. Describe the working of your code and all the technical aspects.
ReplyDeleteany one help me
In scan
ReplyDelete