-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathQ9_Inheritance.java
120 lines (106 loc) · 3.24 KB
/
Q9_Inheritance.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
Even the author is not satisifed with this approach. But have to as now other details are given in the question. For the specific exam relates
Program please don't refer this program. Refer Q9_Inheritance2.java
*/
import java.util.*;
abstract class Vehicle
{
int year_of_manfacture;
public abstract void getData();
public abstract void putData();
}
class TwoWheeler extends Vehicle
{
protected String brand;
protected int cost;
String EngineType;
public String color;
Scanner s;
public void getData()
{
s = new Scanner(System.in);
System.out.println("Enter Brand of TwoWheeler:");
brand=s.next();
System.out.println("Enter Cost of TwoWheeler");
cost = Integer.parseInt(s.next());
System.out.println("Enter Engine Type TwoWheeler (2_Stroke or 4_ Stroke)");
EngineType = s.next();
System.out.println("Enter Color of TwoWheeler");
color = s.next();
}
public void putData()
{
System.out.println("TWO WHEELER\nBrand:" + brand + "\nEngineType: "
+ EngineType + "\nCost:" + cost + "\nColor:" + color+"\n\n");
}
}
final class FourWheeler extends Vehicle
{
private String brand;
protected int cost;
String EngineType;
public String color;
Scanner s;
public void getData()
{
s = new Scanner(System.in);
System.out.println("Enter Brand of FourWheeler:");
brand=s.next();
System.out.println("Enter Cost of FourWheeler");
cost = Integer.parseInt(s.next());
System.out.println("Enter Engine Type of FourWheeler (2_Stroke or 4_Stroke)");
EngineType = s.next();
System.out.println("Enter Color of FourWheeler");
color = s.next();
}
public void putData()
{
System.out.println("FOUR WHEELER\nBrand:" + brand + "\nEngineType: "
+ EngineType + "\nCost:" + cost + "\nColor:" + color+"\n\n");
}
}
class MyTwoWheeler extends TwoWheeler
{
String OwnerName;
MyTwoWheeler(String ow,String br, int co, String et, String colo)
{
OwnerName = ow;
super.brand=br;
super.cost = co;
super.EngineType = et;
super.color = colo;
}
public void prin()
{
System.out.println("The Vehicle \nBrand:"+super.brand+"\nEngineType:" + super.EngineType + "\nCost:" + super.cost + "\nColor: " + super.color);
System.out.println("Owner:" + OwnerName);
}
}
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
TwoWheeler tw = new TwoWheeler();
tw.getData();
tw.putData();
FourWheeler fw=new FourWheeler();
fw.getData();
fw.putData();
System.out.println("Enter the details for your two wheeler");
System.out.println("Enter the Name of ownwer");
String name = sc.next();
System.out.println("Enter the Brand of Two wheeler");
String brand = sc.next();
System.out.println("Enter the color of Two wheeler");
String color = sc.next();
System.out.println("Enter the engine_type of Two wheeler");
String engine_type = sc.next();
System.out.println("Enter the cost of Two wheeler");
int cost = sc.nextInt();
System.out.println("Enter the Year of manufacture of Two wheeler");
int year_of_manufacture = sc.nextInt();
MyTwoWheeler mtw = new MyTwoWheeler(name,brand,cost,engine_type,color);
mtw.prin();
}
}