Return to lecture notes index

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

Afternoon Exam Explained

1. Please write a Java class specification that specifies a type as described below [90 points]:
a. You are describing an TransactionSummary.

b. The totalAmountInDollars involved, which is normally positive, but can be negative in the event of a refund. It measures the amount of money in dollars, including cents as fractions thereof.

c. A flag to indicate that the transaction isARefund. Otherwise, it is presumed not to be a refund.

d. A changeCounter, initially 0, that indicates the total number of value changes (increases or decreases) after initialization.

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

i. IMPORTANT NOTE: The transaction is a refund if, and only if, the initial amountOfMoney is negative. It is a sale otherwise.

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

g. It should be possible to increaseTheValue of a transaction. The changeInValue should always be expressed as a non-negative dollar amount. It should increment the changeCounter and return true if successful. It should return false, without affecting the changeCounter, if the changeInValue is negative.

h. It should be possible to decreaseTheValue of a transaction. The changeInValue should always be expressed as a non-negative dollar amount. It should take no action and return false if the dollar changeInValue exceeds the totalAmountInDollars or if the changeInValue is negative. It should increment the changeCounter and return true, otherwise. . i. The TransactionSummary should be able to reduce itself to a String representation via the usual technique. j. The TransactionSummary should be able to compareTo another transaction summary to indicate if it is greater than, less than, or equal to the other transaction. This should be done in the usual way. The primary basis for the comparison is the totalAmountInDollars (Note: This is always positive, regardless of transaction type). In the event these are equal, the changeCounter should be used (higher is greater). Note: The above itallicized line makes no sense in the context of this class and the rest of the question's wording. Thankfully it appears that noone was confused by this slip up.



class TransactionSummary{
	
	private double totalAmountInDollars;
	private boolean isARefund; //a boolean is the best choice of variable here because it only has two states
	                           //true and false, while Strings and ints have many other states
	private int changeCounter;
	
  //the constructor...note that you don't need to pass in "isARefund" or changeCounter. We can
  //calculate isARefund from the sign of the totalAmountInDollars (positive or negative) and
  //changeCounter should always be initialized to zero.
  public TransactionSummary (double totalAmountInDollars) {
    
    this.totalAmountInDollars = totalAmountInDollars;
  
    // isARefund = (totalAmountInDollars < 0);
    if (totalAmountInDollars < 0)
      isRefund = true;
    else
      isRefund = false;

    changeCounter = 0;
    
  } 


  public double getTotalAmountInDollars() {
    return totalAmountInDollars;
  }


  public boolean getIsRefund() {
    return isRefund;

    //  alternativly...return (totalAmountInDollars < 0); would return the same result
  }


  public int getChangeCounter() {
    return changeCounter;
  }


  public boolean increaseTheValue(double increaseAmount) {
	//only change the value if value passed in is positive
    if (increaseAmount < 0)
      return false;        //note this will cause Java to exit the method and the 
                           //rest of the code in here will not execute.

    totalAmountInDollars += increaseAmount;//totalAmountInDollars = totalAmountInDollars + increaseAmount

    changeCounter++; // changeCounter = changeCounter + 1;

    return true;  //sucessfully completed the change...thus return true
  }


  public boolean decreaseTheValue(double decreaseAmount) {
    //only decrease the amount if decreaseAmount positive
    if (decreaseAmount < 0)
      return false;
	
	//only decrease if you have enough money in your exchange to decrease it by
    if (decreaseAmount > totalAmountInDollars)
      return false;

    totalAmountInDollars -= decreaseAmount; //totalAmountInDollars = totalAmountInDollars - decreaseAmount


    changeCounter++;

    return true;
  }


  public String toString() {
    String type;
    
    //convert the boolean into a nice string representation
    if (isARefund)
      type = "Refund";
    else
      type = "Sale";

    return "$" + totalAmountInDollars + ", " + type + ", " + 
            changeCounter + " changes.";
  }


  public int compareTo (Object o) {
  	//this is type casting o to a TransactionSummary and storing it in a TransactionSummary
  	//reference variable.  This is the step that will cause a ClassCastException if something 
  	//other than a TransactionSummary is passed into the compareTo method.
    TransactionSummary ts = (TransactionSummary) o; 

    int difference = 100*(int)totalAmountInDollars -    //multiply by 100 so don't lose any cents
                     100*(int)ts.totalAmountInDollars;  //then cast to an int.
	
	//as long as the two total amounts are different you can return difference as the result
    if (difference != 0)
      return difference; //notice that this must be an int (that is our return type)

    //otherwise you must return the difference in the number of changes made to the summary
    return (changeCounter - ts.changeCounter);


/* ALTERNATIVE: 
    if (totalAmountInDollars < ts.totalAmountInDollars)
      return -1;

    if (totalAmountInDollars > ts.totalAmountInDollars)
      return 1;

    //notice it will only get here if the two totalAmountInDollars are the same
    if (changeCounter < ts.changeCounter)
      return -1;

    if (this.changeCounter > ts.changeCounter)
      return 1;

    return 0;
*/
  }
  
}//end of class and question 1

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

  //note that this needs to be part of a class.  You can't just have a method floating around
  //without a class.  Easiest class to put it in would be TransactionSummary (since it is a tester
  //for the class. 
  public static void main (String[] args) {
    TransactionSummary sale = new TransactionSummary(105.51);//have to use one of 

    System.out.println (sale);
  }

3. Please distinguish between overloading and overriding [5 points]

Overloading is when a method name has several different methods it could execute depending on the parameters passed in. Here is an example of an overloaded method:

		public void add(int change) {
			...method written here...	
		}
		
		public boolean add(double change){
			...method written here...
		}
		

Notice that as long as the parameter types are different then the add method can have a completely different signature (above one has a void return type and the other returns a boolean)

Overriding is when you write a method with the same signature as another method in order to replace an inherited method. Examples of this are when we write the toString() or equals(Object o) methods in order to make sure that these methods that we inherited from Object never are used.

So the key difference between overloading and overriding is that in overloading the two methods accept different parameters while in overriding the two methods have the same signature (including their argument list).