-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCar.java
More file actions
95 lines (77 loc) · 2.44 KB
/
Copy pathCar.java
File metadata and controls
95 lines (77 loc) · 2.44 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
public class Car {
private String make;
private String model;
private int engineSize;
//empty constructor
public Car() {
/** make and model both initialised to null (all Strings initialise to null if not set)
* engineSize initialised to 0 (all integers initialise to 0 is not set)
*/
}
//constructor to initialise make and model and engineSize
public Car(String mk, String md, int e) {
this.make = mk;
this.model = md;
this.engineSize = e;
}
//note the return type is a String as make is a String
public String getMake() {
/** you can use "this" to access the make instance variable
* here "this" refers to this objects instance variables
*/
return this.make;
}
public String getModel() {
/** or as there is no other variable called model you can
* access it directly without the use of "this"
*/
return model;
}
//note the return type is an int as engineSize is an int
public int getEngineSize() {
return engineSize;
}
/**if the parameter you pass in has the same name
* as the instance variable
* the make on it's own refers to the parameter value
* and this.make refers to the instance variable
*/
public void setMake(String make) {
this.make = make;
}
/** if the parameter does not have the same name then you do not
* have to use this you can refer the the instance parameter as simply model
* as there is no other variable called model
*/
public void setModel(String md) {
model = md;
}
public void setEngineSize(int e) {
engineSize = e;
}
/** we are now going to override the objects toString() method
* so that our version will run if we try to print out a Car object
* in the main program if we do the following
* Car c = new Car();
* System.out.println(c);
* the program will attempt to print the Car object - in order to do this
* it will implicitly call the toString() method
**/
/*@Override
public String toString() {
return "Make: " + make + " - Model: " + model + " - Engine Size:" + engineSize;
}*/
//this method takes in another Car object to compare to the existing object
public boolean equals(Car c) {
/** notice we use the equals method of the string to compare
* if the make, model and engine size are the same for them both
* Strings and the == to compare ints
*/
if (c.getMake().equals(make) && c.getModel().equals(model) && c.getEngineSize() == engineSize) {
return true;
}
else {
return false;
}
}
}