So I was a sick for some time and missed a few uni lectures and I was at a complete loss when we were supposed to do the mock test. It was based on these resources I found on our module webpage. Obviously, it's not just a single thing I missed out on. I know the very basics of Java, I know a little bit about creating and using multiple classes with objects and I know a little bit about constructors. What do I need to catch up on to understand any of this code?
public class BankAccount { private double balance; public BankAccount () { balance = 0; } public BankAccount (double b) { balance = b; } public void setBalance (double b) { balance = b; } public double getBalance () { return balance; } public String toString() { return "\n\tbalance: " + balance; } } //end class BankAccount
/** Base class for clerk and customers: name and age. */ public class Person { public static final String NONAME = "No name yet"; private String name; private int age; //in years public Person () { name = NONAME; age = 0; } public Person (String initialName, int initialAge) { /* name = initialName; if (initialAge >= 0) age = initialAge; else { System.out.println("Error: Negative age or weight."); System.exit(0); } */ this.set(initialName); this.set(initialAge); } public void set (String newName) { name = newName; //age is unchanged. } public void set (int newAge) //name is unchanged. { if (newAge >= 0) { age = newAge; } else { System.out.println("Error: Negative age."); System.exit(0); } } public String getName ( ) { return name; } public int getAge ( ) { return age; } public String toString( ) { return (name + " is " + age + " years old\n"); } } //end class Person
public class Clerk extends Person { private String socialSecurityNumber; // constructor public Clerk( String initialName, int initialAge, String ssn ) { /* name = initialName; age = initialAge; */ super (initialName, initialAge); socialSecurityNumber = ssn; } public Clerk () { super(); socialSecurityNumber = ""; } // sets social security number public void setSocialSecurityNumber( String number ) { socialSecurityNumber = number; // should validate as } // returns social security number public String getSocialSecurityNumber() { return socialSecurityNumber; } // returns String representation of Clerk object public String toString() { return getName() + " (" + getAge() + " years old)" + "\n\tsocial security number: " + getSocialSecurityNumber(); } } // end class Clerk
public class Customer extends Person { private BankAccount ba; // constructors public Customer () { super(); ba = new BankAccount(); } public Customer ( String initialName, int initialAge, double b ) { super (initialName, initialAge); ba = new BankAccount (b); } // returns String representation of Clerk object public String toString() { return getName() + " (" + getAge() + " years old)" + "\n\tbank account balance: " + ba.getBalance(); } } // end class Customer