-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPD4NamithGInheritanceLabDriver.java
More file actions
275 lines (235 loc) · 9.35 KB
/
PD4NamithGInheritanceLabDriver.java
File metadata and controls
275 lines (235 loc) · 9.35 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
Name: Namith Gangireddyvari Pd: 4
Assignment: Inheritance Lab
To demonstrate the consept of a "is-a" and a "has-a" relationship
Did not use outside reasources/help
***NOTE***
Only commented in the driver class as the other classes have methods that
have names that explain purpose.
Also, reduced amount of comments to keep readability of program
*/
public class PD4NamithGInheritanceLabDriver{
public static void main (String [] args) {
//Demonstrates all methods of the Person Class
System.out.println ("Person Class: ");
Person p1 = new Person ("John", 22);
//Demonstrates toString method
System.out.println (p1);
Person p2 = new Person ("Bob", 24);
System.out.println (p2);
//Case where p3 has same values as p1
Person p3 = new Person ("John", 22);
System.out.println (p3);
//Checks if the attributes of the objects are the same
//Changes value of second object to be differant
//Demonstrates equals method
if(p1.equals(p2)){
System.out.print("Already taken: ");
//Demonstrates setter method
p2.setName("Joe");
//Demonstrates getter method
System.out.println("P2 is now " + p2.getName() + " and is " + p2.getAge() + " years old");
}else if (p1.equals(p3)){
System.out.print("Already taken: ");
p3.setName("Joe");
System.out.println("P3 is now " + p3.getName() + " and is " + p3.getAge() + " years old");
}else if (p2.equals(p3)){
System.out.print("Already taken: ");
p3.setName("Joe");
System.out.println("P2 is now " + p3.getName() + " and is " + p3.getAge() + " years old");
}
//Demonstrates all methods of the Person Class
System.out.println ("\nVehicle Class:");
//Demonstrates a "has-a" relationship. v1 has p1 as its argument, thus has its values.
Vehicle v1 = new Vehicle ("Ford", 21, p1);
//Demonstrates toString
System.out.println(v1);
Vehicle v2 = new Vehicle ("Jeep", 35, p1);
System.out.println(p2);
//Case where v3 has same values as v1
Vehicle v3 = new Vehicle ("Ford", 21, p1);
System.out.println(p3);
//Checks if the attributes of the objects are the same
//Changes value of second object to be differant
//Demonstrates equals method
if(v1.equals(v2)){
System.out.print("Already taken: ");
//Demonstrates all setter methods in class
v2.setManufactureName("Tesla");
v2.setCylinderNum(0);
v2.setOwner(p2);
//Demonstrates all getter methods in class
System.out.println("Manufacture is now " + v2.getManufactureName() + ", it has " + v2.getCylinderNum()
+ " cylinders, and the " + v2.getOwner());
}else if (v1.equals(v3)){
System.out.print("Already taken: ");
v3.setManufactureName("Tesla");
v3.setCylinderNum(0);
v3.setOwner(p3);
System.out.println("Manufacture is now " + v3.getManufactureName() + ", it has " + v3.getCylinderNum()
+ " cylinders, and the " + v3.getOwner());
}else if (v2.equals(v3)){
System.out.print("Already taken: ");
v3.setManufactureName("Tesla");
v3.setCylinderNum(0);
v3.setOwner(p3);
System.out.println("Manufacture is now " + v3.getManufactureName() + ", it has " + v3.getCylinderNum()
+ " cylinders, and the " + v3.getOwner());
}
//Checks if the attributes of the objects are the same
//Changes value of second object to be differant
//Demonstrates equals method
System.out.println ("\nTruck Class:");
//Demonstrates an "is-a" relationship as truck inherits methods from vehicle
Truck t1 = new Truck (1175.23, 21, "Ford", 21, p1);
System.out.println(t1);
Truck t2 = new Truck (123.45, 55, "Jeep", 35, p1);
System.out.println (t2);
Truck t3 = new Truck (1175.23, 21, "Ford", 21, p1);
System.out.println (t3);
//Another case of truck where it will show "method overloading"
//***Note***: this is extra work to show my knowledge on subject
Vehicle t4 = new Truck (1175.23, 21, "Chrysler", 21, p1);
System.out.println (t4);
//Uses method from Vehicle class since Truck does not have this method
t4.setManufactureName("Fiat");
//Uses toString from Truck class becuase both classes have this method
System.out.println (t4);
//Checks if the attributes of the objects are the same
//Changes value of second object to be differant
//Demonstrates equals method
if(t1.equals(t2)){
//Demonstrates all setter methods in class
//Uses newly changed attributes from v2 as argument using a getter
t2.setManufactureName(v2.getManufactureName());
t2.setCylinderNum(v2.getCylinderNum());
t2.setOwner(v2.getOwner());
t2.setLoadCap(223.45);
t2.setTowCap(1);
//Demonstrates all getter methods in class
System.out.println("Car can load " + t2.getLoadCap() + "lbs, can tow " + t2.getTowCap() + " cars, manufacture is "
+ t2.getManufactureName() + ", it has " + t2.getCylinderNum()
+ " cylinders, and the " + t2.getOwner());
}else if (t1.equals(t3)){
System.out.print("Already taken: ");
t3.setManufactureName(v3.getManufactureName());
t3.setCylinderNum(v3.getCylinderNum());
t3.setOwner(v3.getOwner());
t3.setLoadCap(223.45);
t3.setTowCap(1);
System.out.println("Car can load " + t3.getLoadCap() + "lbs, can tow " + t3.getTowCap() + " cars, manufacture is "
+ t3.getManufactureName() + ", it has " + t3.getCylinderNum()
+ " cylinders, and the " + t3.getOwner());
}else if (t2.equals(t3)){
System.out.print("Already taken: ");
t3.setManufactureName(v3.getManufactureName());
t3.setCylinderNum(v3.getCylinderNum());
t3.setOwner(v3.getOwner());
t3.setLoadCap(223.45);
t3.setTowCap(1);
System.out.println("Car can load " + t3.getLoadCap() + "lbs, can tow " + t3.getTowCap() + " car(s), manufacture is "
+ t3.getManufactureName() + ", it has " + t3.getCylinderNum()
+ " cylinders, and the " + t3.getOwner());
}
}// Main
}// PD4NamithGInheritanceLabDriver
class Person{
private String name;
private int age;
public Person (){
name = "No name yet.";
age = 0;
}
public Person (String name, int age){
this.name = name;
this.age = age;
}
public void setName (String newName){
name = newName;
}
public String getName (){
return name;
}
public int getAge (){
return age;
}
public String toString (){
return "Name: "+ name + "\nAge: " + age;
}
public boolean equals (Person other){
return (this.name.equalsIgnoreCase(other.name) && this.age == other.age);
}
}// Person
class Vehicle{
String manufactureName;
int cylinderNum;
Person owner;
public Vehicle (){
manufactureName = "No name yet.";
cylinderNum = 0;
owner = null;
}
public Vehicle (String manName, int num, Person p1){
manufactureName = manName;
cylinderNum = num;
owner = p1;
}
public String getManufactureName (){
return manufactureName;
}
public void setManufactureName (String name){
manufactureName = name;
}
public int getCylinderNum (){
return cylinderNum;
}
public void setCylinderNum (int num){
cylinderNum = num;
}
public Person getOwner (){
return owner;
}
public void setOwner (Person other){
owner = other;
}
public String toString (){
return "Name: "+ owner.getName() + "\nAge: " + owner.getAge() +
"\nManufacture Name: " + manufactureName + "\nCylinder Number: " + cylinderNum;
}
public boolean equals (Vehicle other){
return ((this.manufactureName.equalsIgnoreCase(other.manufactureName) && this.cylinderNum == other.cylinderNum)
&& this.owner.equals(other.owner));
}
}// Vehicle
class Truck extends Vehicle{
private double loadCap;
private int towCap;
public Truck (){
super();
loadCap = 0.0;
towCap = 0;
}
public Truck (double load, int tow, String manName, int num, Person p1){
super(manName, num, p1);
loadCap = load;
towCap = tow;
}
public double getLoadCap (){
return loadCap;
}
public void setLoadCap (double num){
loadCap = num;
}
public int getTowCap (){
return towCap;
}
public void setTowCap (int num){
towCap = num;
}
public String toString (){
return super.toString() + "\nLoad Capacity: "+ loadCap + "\nTow Capacity: " + towCap;
}
public boolean equals (Truck other){
return (this.loadCap == other.loadCap && this.towCap == other.towCap);
}
}// Truck