TurkuBasicOOPinJava / Week 5: Class hierarchies /11B. Encrypted connection
KaiquanMah's picture
super(param1); this(defaultParam2);
1161255 verified
raw
history blame
4.31 kB
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