-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissile.java
More file actions
106 lines (88 loc) · 2.76 KB
/
Missile.java
File metadata and controls
106 lines (88 loc) · 2.76 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
package javaIntroduction;
/*
* @Author: Mangaliso Mngomezulu.
* @Channel: https://youtube.com/channel/UCNgcWqzPYDLSitGo2QnKUdw
* @Description: A simple class on the concept of encapsulation. This class code should be evaluted in conjunction with the
* MissileTester.java file.
*
*@Tips: if using jGrasp, you may want to remove the first line
* * */
public class Missile {
//attributes
private String missileID;
private double weight; //has a fractional part/ decimal
private String manufacturer; //should be surrounded by double quotes
private boolean isFast; //true, false
//constructors/constructor
public Missile(){
missileID = "";
weight = 0;
manufacturer = "";
isFast = false;
}
//constructor with parameters
public Missile(String missileIDValue , double weightValue, String manufacturerName, boolean isFastValue) {
setMissileID(missileIDValue);
setWeight(weightValue);
setManufacturer(manufacturerName);
setIsFast(isFastValue);
}
/*
* Getters
* */
public String getMissileID() {
return missileID;
}
public double getWeight() {
return weight;
}
public String getManufacturer() {
return manufacturer;
}
public boolean getIsFast() {
return isFast;
}
/*
* Setters: setters are methods used to initialize attribute values. The reason we use methods and not just the
* simply initializing is that we want to control the way the attributes values are assigned to the attributes.
* The methods allow us to have this control. The methods are then called/invoked in the Constructor.
* */
public void setIsFast(boolean isFastValue) {
if(isFastValue == true) {
isFast = isFastValue;
}
else {
throw new IllegalArgumentException("Sorry!, We only use fast Missiles.");
}
}
public void setManufacturer(String manufacturerName) {
if(manufacturerName.equalsIgnoreCase("Denel")) {
manufacturer = manufacturerName;
}
else {
throw new IllegalArgumentException("Sorry!, We only take use Missiles from Denel.");
}
}
public void setMissileID(String missileIDValue) {
if(missileIDValue.length() >= 10) {
missileID = missileIDValue;
}
else {
throw new IllegalArgumentException("Missile Id Must be at least 10 charecters");
}
}
public void setWeight(double weightValue) {
if(weightValue >= 200) {
weight = weightValue;
}
else {
throw new IllegalArgumentException("Weight should be at least 200kg");
}
}
/*
* This method simply returns a string representation of the object invoking it.
* */
public void displayInfo(){
System.out.println(missileID+", "+weight+", "+manufacturer+", "+isFast);
}
}