posted Oct 3, 2018, 5:48 AM by Konstantinovich Samuel   [ updated Oct 4, 2018, 8:12 AM ]
Class Creation!

For now you can edit using gedit. 

If you already understand everything super well, try learning Emacs while you are at it:
Emacs tutorial/shortcuts.


You are given a class with a main. This class uses an object that doesn't exist yet: Kitty.

-Write an appropriate Kitty class. Save the file in the same directory as the Driver file.

-Fill in all the methods, but leave the body blank (non-void methods need a temporary return value).

-Compile and run the driver with your "Blank" class.

-Compile and run each time you fill in a method!

By reading this main, you can see that the Kitty class requires:
    2 fields:
      age - an integer representation
      name - any non-empty string.

    2 constructors:
            a default [default constructor has no parameters] that makes a 2 year old cat Named "Mittens"
            and a (String,int) constructor that initializes the name/age with the parameters.

    2 accessor methods:

    2 mutator methods:
     changeName(String) - changes the name to the provided argument.
     makeOlder() - add 1 to the age.

    After all of those work try adding this method: (also an accessor)
     toString() - returns "The amazing XXX" where you insert the Cat's name for XXX.

    What changes when you run this? What does this suggest about toString?

public class Driver{
	public static void main(String[]args){
		Kitty a = new Kitty("Matcha", 3);
		//constructor that takes the name and age of the Kitty.

		System.out.println(a.getName() + " is "+a.getAge() + " years old.");
		//should print "Matcha is 3 years old."

		System.out.println(a.getName() + " is "+a.getAge() + " years old.");
		//should print "Matcha is 4 years old."

		System.out.println(a.getName() + " is "+a.getAge() + " years old.");
		//should print "Katya is 5 years old."

		//should print The amazing Katya. This is an implied call to toString()

		Kitty  b =  new Kitty();
		//2nd constructor makes a Kitty named Mittens that is 2 years old
		//this constructor takes no parameters.

		//should print The amazing Mittens. This is an implied call to toString()

		System.out.println(b.getName() + " is " + b.getAge() + " years old.");
		//should print "Mittens is 2 years old."

write a bank account class as follows. You need a Driver to test it. Make a repo MKS21X-Bank
Make sure your repo is public and named exactly.

class name: 

Instance variables required:
      balance - a double
      accountID - int [the account number, an integer]
      password - String

Methods Required:
1. A constructor: Should accept values for EACH of the instance variables
2. get methods for balance, accountID
3. set method for password
4. A toString method that returns (tab separated values):  "AccountID\tBalance"
           e.g. "100203    1023.45"
5a. A method to deposit money into the account.
    It should accept an amount of money, increase the account balance
    by that much, and return true. However, when the argument given is a negative
    deposit, instead it should leave the balance unchanged and returning false. 
5b. Method to withdraw money from the account (subtract from the balance) and
    return true if it was successful,false otherwise. This will not be successful
    if the balance is too low, in which case the action does not change the balance. 
6. A main method (in a separate driver file) to test your code.

Since withdraw and deposit return booleans. We can use them as follows:

       if( var.withdraw(1000) ){
            println("Withdrawal success!");
            println("Withdrawal failure");


    /*Sole constructor requires all field's values*/
     public BankAccount( double balance, int accountID, String password){...} 

/*Return a String to be used to display the account data. "ACCOUNT\tBALANCE" */ 
 public String toString(){...} 

/*standard accessor methods*/ 
 public double getBalance(){...} 
 public int getAccountID(){...} 

/*Change the value of password to the specified value 
 *@param newPass The value to replace the old password with*/ 
     public void setPassword(String newPass){...} 

/*Only when amount is positive: Increase balance by amount and return true. 
 *@return When amount is positive: Return true, otherwise return false. 
 *@param amount The amount is how much to change the balance by. 
     public boolean deposit(double amount){...} 
/*Only when amount is positive and the amount does not exceed the balance: 
 *Decrease balance by amount and return true, return false otherwise. 
 *Make no change if the amount is too large to withdraw 
 *@param amount The amount is how much to change the balance by. 
 *@return true when the balance is large enough, false otherwise.
     public boolean withdraw(double amount){... }