Spaces:
Running
Running
File size: 4,310 Bytes
1161255 | 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 | The program defines the class 'Connection'.
Write the class 'EncryptedConnection', which inherits the class Connection.
The class has 2 new string-type attributes, encryption and password.
Implement get methods for these.
In addition, 3 constructor must be written in the class:
1
A constructor that takes as parameters speed, encryption(string) and password (also string):
the constructor sets the values of the attirbutes according to the parameters.
2
A constructor that takes as its parameter only the speed.
A constructor that sets the speed attribute according to the parameter.
The attribute encryption value is "SHA" and the password is "abcabc".
3
A constructor with no parameters at all.
The speed is set to 100, and the other attributes are set to the same values as in the case of constructor 2
import java.util.Random;
public class Test{
public static void main(String[] args){
final Random r = new Random();
System.out.println("Testing class EncryptedConnection...");
String[] hash = "SHA SSH ABC LTD SMP DDR OY RNO RKK".split(" ");
String[] passwords = "abc123 pass12 password parrwosd 123456".split(" ");
// Testing inheritance first
Connection y = new EncryptedConnection();
// Constructors
String selectedH = hash[r.nextInt(hash.length)];
String passW = passwords[r.nextInt(passwords.length)];
int speed = r.nextInt(100) + 1;
System.out.println("Creating an encrypted connection with the following parameters");
System.out.println("(" + speed + ", " + selectedH + ", " + passW + ")");
EncryptedConnection conn = new EncryptedConnection(speed, selectedH, passW);
System.out.println("Speed: " + conn.getSpeed());
System.out.println("Protection: " + conn.getEncryption());
System.out.println("Password: " + conn.getPassword());
System.out.println("");
speed = r.nextInt(100) + 1;
System.out.println("Creating an encrypted connection with the following parameters");
System.out.println("(" + speed + ")");
conn = new EncryptedConnection(speed);
System.out.println("Speed: " + conn.getSpeed());
System.out.println("Protection: " + conn.getEncryption());
System.out.println("Password: " + conn.getPassword());
System.out.println("");
System.out.println("Creating an encrypted connection with the following parameters");
System.out.println("()");
conn = new EncryptedConnection();
System.out.println("Speed: " + conn.getSpeed());
System.out.println("Protection: " + conn.getEncryption());
System.out.println("Password: " + conn.getPassword());
System.out.println("");
}
}
class Connection {
protected int speed;
public Connection(int speed) {
this.speed = speed;
}
public int getSpeed() {
return speed;
}
}
//ADD
class EncryptedConnection extends Connection {
private String encryption;
private String password;
//constructor1
public EncryptedConnection(int speed, String encryption, String password){
super(speed);
this.encryption = encryption;
this.password = password;
}
//constructor2
public EncryptedConnection(int speed){
super(speed);
this.encryption = "SHA";
this.password = "abcabc";
}
//constructor3
// APPROACH1
// public EncryptedConnection(){
// super(100);
// this.encryption = "SHA";
// this.password = "abcabc";
// }
//APPROACH2
// CALL/reuse CONSTRUCTOR2
public EncryptedConnection() {
this(100);
}
public String getEncryption() {
return encryption;
}
public String getPassword() {
return password;
}
}
Testing class EncryptedConnection...
Creating an encrypted connection with the following parameters
(17, OY, 123456)
Speed: 17
Protection: OY
Password: 123456
Creating an encrypted connection with the following parameters
(23)
Speed: 23
Protection: SHA
Password: abcabc
Creating an encrypted connection with the following parameters
()
Speed: 100
Protection: SHA
Password: abcabc
|