KaiquanMah commited on
Commit
1161255
·
verified ·
1 Parent(s): 36a8302

super(param1); this(defaultParam2);

Browse files
Week 5: Class hierarchies/11B. Encrypted connection ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ The program defines the class 'Connection'.
2
+ Write the class 'EncryptedConnection', which inherits the class Connection.
3
+
4
+ The class has 2 new string-type attributes, encryption and password.
5
+ Implement get methods for these.
6
+
7
+ In addition, 3 constructor must be written in the class:
8
+ 1
9
+ A constructor that takes as parameters speed, encryption(string) and password (also string):
10
+ the constructor sets the values of the attirbutes according to the parameters.
11
+
12
+ 2
13
+ A constructor that takes as its parameter only the speed.
14
+ A constructor that sets the speed attribute according to the parameter.
15
+ The attribute encryption value is "SHA" and the password is "abcabc".
16
+
17
+ 3
18
+ A constructor with no parameters at all.
19
+ The speed is set to 100, and the other attributes are set to the same values as in the case of constructor 2
20
+
21
+
22
+
23
+
24
+
25
+ import java.util.Random;
26
+
27
+ public class Test{
28
+ public static void main(String[] args){
29
+ final Random r = new Random();
30
+
31
+
32
+ System.out.println("Testing class EncryptedConnection...");
33
+
34
+ String[] hash = "SHA SSH ABC LTD SMP DDR OY RNO RKK".split(" ");
35
+ String[] passwords = "abc123 pass12 password parrwosd 123456".split(" ");
36
+
37
+ // Testing inheritance first
38
+ Connection y = new EncryptedConnection();
39
+
40
+ // Constructors
41
+ String selectedH = hash[r.nextInt(hash.length)];
42
+ String passW = passwords[r.nextInt(passwords.length)];
43
+ int speed = r.nextInt(100) + 1;
44
+
45
+ System.out.println("Creating an encrypted connection with the following parameters");
46
+ System.out.println("(" + speed + ", " + selectedH + ", " + passW + ")");
47
+ EncryptedConnection conn = new EncryptedConnection(speed, selectedH, passW);
48
+ System.out.println("Speed: " + conn.getSpeed());
49
+ System.out.println("Protection: " + conn.getEncryption());
50
+ System.out.println("Password: " + conn.getPassword());
51
+ System.out.println("");
52
+
53
+ speed = r.nextInt(100) + 1;
54
+ System.out.println("Creating an encrypted connection with the following parameters");
55
+ System.out.println("(" + speed + ")");
56
+ conn = new EncryptedConnection(speed);
57
+ System.out.println("Speed: " + conn.getSpeed());
58
+ System.out.println("Protection: " + conn.getEncryption());
59
+ System.out.println("Password: " + conn.getPassword());
60
+ System.out.println("");
61
+
62
+ System.out.println("Creating an encrypted connection with the following parameters");
63
+ System.out.println("()");
64
+ conn = new EncryptedConnection();
65
+ System.out.println("Speed: " + conn.getSpeed());
66
+ System.out.println("Protection: " + conn.getEncryption());
67
+ System.out.println("Password: " + conn.getPassword());
68
+ System.out.println("");
69
+ }
70
+ }
71
+
72
+
73
+ class Connection {
74
+ protected int speed;
75
+
76
+ public Connection(int speed) {
77
+ this.speed = speed;
78
+ }
79
+
80
+ public int getSpeed() {
81
+ return speed;
82
+ }
83
+ }
84
+
85
+
86
+
87
+
88
+ //ADD
89
+ class EncryptedConnection extends Connection {
90
+ private String encryption;
91
+ private String password;
92
+
93
+ //constructor1
94
+ public EncryptedConnection(int speed, String encryption, String password){
95
+ super(speed);
96
+ this.encryption = encryption;
97
+ this.password = password;
98
+ }
99
+
100
+ //constructor2
101
+ public EncryptedConnection(int speed){
102
+ super(speed);
103
+ this.encryption = "SHA";
104
+ this.password = "abcabc";
105
+ }
106
+
107
+ //constructor3
108
+ // APPROACH1
109
+ // public EncryptedConnection(){
110
+ // super(100);
111
+ // this.encryption = "SHA";
112
+ // this.password = "abcabc";
113
+ // }
114
+ //APPROACH2
115
+ // CALL/reuse CONSTRUCTOR2
116
+ public EncryptedConnection() {
117
+ this(100);
118
+ }
119
+
120
+ public String getEncryption() {
121
+ return encryption;
122
+ }
123
+
124
+ public String getPassword() {
125
+ return password;
126
+ }
127
+ }
128
+
129
+
130
+
131
+
132
+
133
+ Testing class EncryptedConnection...
134
+ Creating an encrypted connection with the following parameters
135
+ (17, OY, 123456)
136
+ Speed: 17
137
+ Protection: OY
138
+ Password: 123456
139
+
140
+ Creating an encrypted connection with the following parameters
141
+ (23)
142
+ Speed: 23
143
+ Protection: SHA
144
+ Password: abcabc
145
+
146
+ Creating an encrypted connection with the following parameters
147
+ ()
148
+ Speed: 100
149
+ Protection: SHA
150
+ Password: abcabc
151
+
152
+