-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem-2a.txt
More file actions
135 lines (101 loc) · 3.78 KB
/
Copy pathproblem-2a.txt
File metadata and controls
135 lines (101 loc) · 3.78 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
2.(a)
car and plane class can be implemented the Vehicle interface
and override the corresponding methods like below:
way:-1 ------------------>>
class Car(val CarWheel:Int,val numOfPassanger:Int,val hasGas:Boolean) : Vehicle{
override fun Int set_num_of_wheels(){
return CarWheel
}
override fun Int set_num_of_passengers(){
return numOfPassanger
}
override fun Boolean has_gas(){
return hasGas
}
}
class Plane(val wheel:Int,val numOfPassanger:Int,val hasGas:Boolean) : Vehicle{
override fun Int set_num_of_wheels(){
return wheel
}
override fun Int set_num_of_passengers(){
return numOfPassanger
}
override fun Boolean has_gas(){
return hasGas
}
}
fun main(){
val car:Car=Car(4,8,true)
val plane:Plane=Plane(8,230,true)
println("Number of Passenger in car is: ${car.set_num_of-wheels()}")
}
way:2 ------------------>>
Another way of implementing the Vehicle interface is without passing values in constructor. Using "getter" and "setter"
i.e.
[note: The naming convention of the methods of the given interface doesn't seem right.
The method name should be "get_num_of_wheels()" instead of "set_num_of_wheels()" because its "getter" method as it has return type.
since the naming convention of the "getter" methods in the question is wrong ,so i'm also gonna name the "setter" method in wrong way in the below code.]
]
class Car:Vehicle{
var wheels : Int = 0
var numberOfPassenger : Int = 0
var hasGas : Boolean = false
//setter method for "wheels"
fun get_num_of_wheels(wheels:Int ){ //as I said earlier in the above "note" that, the naming convention of the "getter" methods is wrong in the given interface.so i'm also write this "setter" method name in wrong way.
this.wheels=wheels // this method name should be "set_num_of_wheels()"
}
//setter method for "passenger"
fun get_num_of_passenger(numberOfPassenger:Int){ // this method name should be "set_num_of_passenger "
this.numberOfPassenger=numberOfPassenger
}
//setter method for "gas"
fun set_has_gas(hasGas:Boolean){
this.hasGas=hasGas
}
//getter method for "wheels"
override fun Int set_num_of_wheels(){ // this method name should be "get_num_of_wheels()"
return this.wheels
}
//getter method for "passenger"
override fun Int set_num_of_passenger(numberOfPassenger:Int){ // this method name should be "get_num_of_passenger "
this.numberOfPassenger=numberOfPassenger
}
//getter method for "gas"
override fun has_gas(hasGas:Boolean){
this.hasGas=hasGas
}
}
class Plane:Vehicle{
var wheels : Int = 0
var numberOfPassenger : Int = 0
var hasGas : Boolean = false
//setter method for "wheels"
fun get_num_of_wheels(wheels:Int ){ //as I said earlier in the above "note" that, the naming convention of the "getter" methods is wrong in the given interface.so i'm also write this "setter" method name in wrong way.
this.wheels=wheels // this method name should be "set_num_of_wheels()"
}
//setter method for "passenger"
fun get_num_of_passenger(numberOfPassenger:Int){ // this method name should be "set_num_of_passenger "
this.numberOfPassenger=numberOfPassenger
}
//setter method for "gas"
fun set_has_gas(hasGas:Boolean){
this.hasGas=hasGas
}
//getter method for "wheels"
override fun Int set_num_of_wheels(){ // this method name should be "get_num_of_wheels()"
return this.wheels
}
//getter method for "passenger"
override fun Int set_num_of_passenger(numberOfPassenger:Int){ // this method name should be "get_num_of_passenger "
this.numberOfPassenger=numberOfPassenger
}
//getter method for "gas"
override fun has_gas(hasGas:Boolean){
this.hasGas=hasGas
}
}
fun main(){
val car:Car=Car()
car.get_num_of_passenger(8)
println(set_num_of_passenger())
}