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

2017-10-02 HW

posted Oct 2, 2017, 5:48 AM by Samuel Konstantinovich   [ updated Oct 3, 2017, 9:18 AM by Konstantinovich Samuel ]
Class Creation!

Emacs tutorial/shortcuts.

Here is a main class that uses an object that doesn't exist

By reading this main, you can see that the Kitty class requires:
    2 fields
      age - an integer representation
      name - any non-empty string.
    3 accessor methods:
     toString - returns the Name of the cat. 
    2 mutator methods:
     changeName(String) - changes the name to the provided argument.
     makeOlder() - add 1 to the age.
    2 constructors - a default [ no parameters constructor] and a (String,int) constructor.

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

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

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

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

		//should print Marmalade.

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

		//should print 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 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!

A constructor and a toString method are good places to start.

write a bank account class as follows. You need a Driver to test it. Bring your file to class tomorrow to discuss and modify.

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. A toString method that returns (tab separated values): "AccountID\tBalance"
            e.g. "100203    1023.45"
      3. get methods for balance, accountID
      4. set method for password
      5a. A method to deposit money into the account. It should accept an amount of money, and increase the account balance by that much and return true. This should not apply a negative deposit, instead it should leave the balance unchanged and returning false. 
      5b. Method to withdraw money from the account 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 (included in the same 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( int accountID,  double balance, 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){...} 

/*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){...} 
/*When amount is positive and the amount is at least as much as the balance: 
 *Decrease balance by amount if the balance is large enough. 
 *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){... }

public class Kitty{
    private String name;
    private int age;

    public Kitty(){//default constructor
	name = "Mittens";
	age = 2;
	//this("Mittens",2);//alternate way you don't know yet.
    public Kitty(String n, int a){
	name = n;
	age = a;

    public String toString(){
	return getName();

    public int getAge(){
	return age;
    public String getName(){
	return name;

    public void makeOlder(){
    public void changeName(String n){
	name = n;