Return to lecture notes index

15-100 Lecture 14 (Wednesday February 16, 2005)

The Morning Exam Explained

1. Please write a Java class specification that specifies a type as described below [90 points]:

a. You are describing an RetirementAccount.

b. The account has a balance, which can be positive or negative (ouch!) and measures the amount of money in dollars, including cents as fractions thereof.

c. The account may be taxDefered, or not.

d. The account also has a holder, who owns the asset

e. All of the above attributes must be set upon initialization

i. IMPORTANT EXCEPTION: If no balance is specified, it should be initialized to $0.00

f. All of the above attributes should be individually accessible.

g. It should be possible to toggleDeferedStatus such that a tax-defered account becomes taxable and vice-versa.

h. It should be possible to contribute additional money to the account - but not to withdraw money.

i. If a negative balance is specified, this method should return false to indicate the problem. Otherwise, it should return true to indicate the successful change.

i. It should be possible for the balance to appreciate by a provided percentage specified as a whole number.

Negative appreciation indicates that the account has lost money. For example,

i. $100 appreciating by 1% yields a balance of $101.

ii. $100 appreciating by -2$ yields an account balance of $98.

An account should lose its tax-defered status if it appreciates by more than (positive) 10%.

j. The RetirementAccount should be able to reduce itself to a String representation via the usual technique.

k. The RetirementAccount should be able to compareTo another reitrment account to indicate if it is greater than, less than, or equal to that account. This should be done in the usual way.

class RetirementAccount {
	private double balance; //why private? only the methods of this class should be able to change the balance
	private boolean taxDeferred;//boolean is the strongest type of variable to use here 
	                            //(strings and ints have infinate number of states)
	private String holder;//note that String is capitalized that means that String is a class
	public RetirementAccount(double balance, boolean taxDeferred, String holder){
		//this is calling on the instance variables of this class because there 
		//is a space conflict
		this.balance = balance;
		this.taxDeferred = taxDeferred;
		this.holder = holder;
	public RetirementAccount(boolean taxDeferred, String holder){
		this.balance = 0.00; //the this here isn't necessary, but it will do no harm
		this.taxDeferred = taxDeferred;
		this.holder = holder;
	//the accessors...
	public double getBalance(){
		return balance; //balance is in scope because it is an instance variable declared for the whole class
	public boolean getTaxDeferred(){
		return taxDeferred;
	public String getHolder(){
		return holder;
	//the elegant way of writing toggleTaxDeferred (note this is the correct interpretation
	//of the question...many people passed into the method what state they wanted taxDeferred
	//to be set equal to.  
	public void toggleTaxDeferred(){
		taxDeferred = !taxDeferred;
	/*another way of writting toggleTaxDeferred...
	 *public void toggleTaxDeferred(){
	 *	if (taxDeferred == true)//same as "if (taxDeferred)"
	 *		taxedDeferred = flase;
	 *	else 
	 *		taxDeferred = true;
	public boolean contribute(double contribution) {
		if (contribution < 0)
			return false;//when you "return" you leave the method
		else { 			   //this else isn't necessary
			balance += contribution;
			return true;
	public void appreciate(int percentage){
		if (percentage > 10)
			taxDeferred = false;
		double percentageFraction = ((double)percentage)/100;//you have to type cast this
		//because dividing two integers (would have gotten 0 as answer in this int 
		//division)..also could fix by writting (percentage/100.0)
		double appreciation = balance*percentageFraction;
		balance += apprecation;//equivallent to balance = balance + appreciation;
	public String toString() {
		String deferred;
		if (taxDeferred)
			deferred = "Tax Deferred";
			deferred = "Taxable";
		return "$" + balance + ", " + deferred + ", " + holder;
	public int compareTo(Object o){
		RetirementAccount ra = (RetirementAccount) o;
		long myBalance = balance*100;
		long otherBalance = ra.balance * 100;
		return (myBalance - otherBalance);

2. Please write a main() method, with the usual form, that creates a new RetirementAccount, with the attributes of your choice, and then prints it out. [5 points]

  //note that this would be the main of the above class
	public static void main(String[] args){
		RetirementAccount sampleAccount = new RetirementAccount  (100.0, false, Greg Kesden);

3. The .equals() method can be invoked on any object - even if not defined within the object's class specification. Why? [5 points]

Every new type of thing (ie every class we create) is a specification of the more general Object class. Thus everything is a subclass of Object. So all classes start out with Object's properties (it's methods) and from there modify it. We inherit the .equals() method from Object. Why do we write the .equals() method again? .equals() is a generic equals method and when inherited only compares the object's identities (ie: is it the same object or two different ones). So we replace .equals with a method that compares the two objects in a more meaningful way (usually by comparing the states of their instance variables).