Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎notes‎ > ‎

### 2017-10-04 HW

posted Oct 4, 2017, 10:17 AM by Konstantinovich Samuel   [ updated Oct 5, 2017, 6:22 AM ]
 `ML Diagrams are used to visualize a class:HW: Draw a UML diagram for a class hierarchy as follows:PointTriangle1. Assume you will make the variables private.2. Because of this add in sensible methods and constructors.3. Also include a method inside of point that would calculate distance to another point. 4. Also include a method inside of Triangle that would calculate perimeter.``You should have started with this:````public class BankAccount{ private double balance; private int accountID; private String password; /** *Create a new BankAccount, with all parameters provided */ public BankAccount( double balance, int accountID, String password ){ this.accountID = accountID; this.balance = balance; this.password = password; } /** *Print the bank account for testing purposes *private method should not show up in javadoc */ private void print(){ System.out.println(accountID+":"+password); System.out.println("\$"+balance); } /** *Return a String consisting of non-password/non-pin information *to be used to display the account. */ public String toString(){ return accountID+",\t\$"+balance; } /** *Return the value of balance */ public double getBalance(){ return balance; } /** *Return the value of accountID */ public int getID(){ return accountID; } /** *Change the value of password to the specified value *@param newPass The value to replace the old password with */ public void setPassword(String newPass){ password = newPass; } /** *When amount is positive: Increase balance by amount and return *true. When amount is not positive: Return false. *@param amount The amount is how much to change the balance by. */ public boolean deposit(double amount){ if(amount > 0.0){ balance += amount; return true; } return false; } /** *When amount is positive and the amount is at least as much as the balance: *decrease balance by amount and return true. Otherwise: Return false. *@param amount The amount is how much to change the balance by. */ public boolean withdraw(double amount){ if(balance > amount && amount > 0){ balance -= amount; return true; } return false; } }````Here are the solutions to yesterday's problems:```` /** *When the password matches the stored password return true. *Otherwise: Return false. *@param password the guess to check against the stored password */ private boolean authenticate(String password){ return password.equals(this.password); } /** *When the passwords match, the amount is positive and the amount is *at least as much as the balance, withdraw from the current account *and deposit into the other account and return true. Return false otherwise. *@param other The BankAccount to transfer money into. *@param amount How much to change the balance by. *@param password The string to check against the stored password. */ public boolean transfer(BankAccount other, double amount,String password){ if(authenticate(password)){ if(withdraw(amount)){ other.deposit(amount); return true; } else{ return false; } } //When you don't need to add any other things to the code //you can just do this: //return authenticate(password) && withdraw(amount) && other.deposit(amount); }```