Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab Objectives Chapter 8 Lab More Classes and Objects ? Be able to write a copy

ID: 3565585 • Letter: L

Question

Lab Objectives

Chapter 8 Lab More Classes and Objects

? Be able to write a copy constructor

? Be able to write equals and toString methods

? Be able to use objects made up of other objects (Aggregation)

? Be able to write methods that pass and return objects

Introduction

We discussed objects in Chapter 6 and we modeled a television in the Chapter 6 lab. We want build on that lab, and work more with objects. This time, the object that we are choosing is more complicated. It is made up of other objects. This is called aggregation. A credit card is an object that is very common, but not as simple as a television. Attributes of the credit card include information about the owner, as well as a balance and credit limit. These things would be our instance fields. A credit card allows you to make payments and charges. These would be methods. As we have seen before, there would also be other methods associated with this object in order to construct the object and access its fields.

Examine the UML diagram that follows. Notice that the instance fields in the CreditCard class are other types of objects, a Person object or a Money object. We can say that the CreditCard

Explanation / Answer

Address.java

public class Address
{

// The Street number and street name
private String street;

// The city in which the address is located
private String city;

// The state in whcih the address is located
private String state;

// The zip code associated with that city and street
private String zip;

// Constructor create an address using four parameters
// road describes the street number and name
// town describes the city
// st describes the state
// zipcode describes the state
public Address (String road, String town, String st, String zipCode)
{

street = road;
city = town;
state = street;
zip = zipCode;

}

// toString method returns information about the address
public String toString()
{

return (street + ", " + city + ", "+ state + " " + zip);

}

}

CreditCard.java

public class CreditCard
{

// private fields from UML
private Money balance;
private Money creditLimit;
private Person owner;

public CreditCard(Person newCardHolder, Money limit)
{

owner = newCardHolder;
balance = new Money(0);
creditLimit = new Money(limit);

}

public Money getBalance()
{
return new Money(balance);
}

public Money getCreditLimit()
{
return new Money(creditLimit);
}

public String getPersonals()
{
return owner.toString();
}

public void charge(Money amount)
{
Money tempVar = new Money(balance.add(amount));
if (tempVar.compareTo(creditLimit) > 0)
{
System.out.println("Exceeds credit limit");
}
else
{
balance = tempVar;
System.out.println("Charge: " + amount);
}
}

public void payment(Money amount)
{

Money temp = new Money(balance.subtract(amount));

}
}

CreditCardDemo.java

public class CreditCardDemo
{

public static void main (String [] args)
{

final Money LIMIT = new Money(1000);
final Money FIRST_AMOUNT = new Money(200);
final Money SECOND_AMOUNT = new Money(10.02);
final Money THIRD_AMOUNT = new Money(25);
final Money FOURTH_AMOUNT = new Money(990);
Person owner = new Person("Christie", "Diane",new Address("237J Harvey Hall","Menomonie","WI" , "54751"));
CreditCard visa = new CreditCard(owner, LIMIT);
System.out.println(visa.getPersonals());
System.out.println("Balance: " + visa.getBalance());
System.out.println("Credit Limit : " + visa.getCreditLimit());
System.out.println();
System.out.println("Attempt to charge " + FIRST_AMOUNT);
visa.charge(FIRST_AMOUNT);
System.out.println("Balance : " + visa.getBalance());
System.out.println("Attempt to charge " + SECOND_AMOUNT);
visa.charge(SECOND_AMOUNT);
System.out.println("Balance : "+ visa.getBalance());
System.out.println("Attempt to pay" + THIRD_AMOUNT);
visa.charge(THIRD_AMOUNT);
System.out.println("Balance : " + visa.getBalance());
System.out.println("Attempt to charge" + FOURTH_AMOUNT);
visa.charge(FOURTH_AMOUNT);
System.out.println("Balance :" + visa.getBalance());

}

}

Money.java

public class Money
{

private long dollars; // A number of dollars
private long cents;    // A number of cents

// Constructor creates a Money object using the amount of money in
// dollars and cents represented with a decimal number

public Money(double amount)
{

if(amount < 0)
{
System.out.println("Error: Negative amount of money are not allowed.");
System.exit(0);
}

else
{
long allCents = Math.round(amount*100);
dollars = allCents/100;
cents = allCents%100;
}
}

// TASK #1 Overload the Constructor
public Money (Money otherObject)
{
this.dollars = otherObject.dollars;
this.cents = otherObject.cents;
}

// Adds the calling Money object to the parameter Money object
public Money add(Money otherAmount)
{

Money sum = new Money(0);
sum.cents = this.cents + otherAmount.cents;
long carryDollars = sum.cents/100;
sum.cents = sum.cents%100;
sum.dollars = this.dollars + otherAmount.dollars + carryDollars;
return sum;

}

// Subtracts the parameter Money object from the calling Money
// object and returns the difference
public Money subtract (Money amount)
{
Money difference = new Money(0);
if (this.cents < amount.cents)
{
this.dollars = this.dollars -1;
this.cents = this.cents + 100;
}

difference.dollars = this.dollars - amount.dollars;
difference.cents = this.cents - amount.cents;
return difference;
}
// compares instance variable of the calling object
// with the parameter object. It returns -1 if the
// dollars and the cents of the calling object are
// less than the dollars and the cents of the parameter
// object, 0 if the dollars and the cents are calling
// object are equal to the dollars and cents of the
// parameter object, and 1 if the dollars and the cents
// of the calling object are more than the dollars and
// the cents of the parameter object.
public int compareTo(Money amount)
{
int value;
if (this.dollars < amount.dollars)
{
value = -1 ;
}
else if (this.dollars > amount.dollars)
{
value = 1 ;
}
else if (this.cents < amount.cents)
{
value = -1 ;
}
else if (this.cents > amount.cents)
{
value = 1 ;
}
else
{
value = 0 ;
}
return value;

}

// Task #2 Equals method
// This method will return a String that looks
// like money, including the dollar sign. Remember
// that if you have less than 10 cents, you will need
// to put a 0 before printing the cents so that it appears
// correctly with 2 decimal places.

public boolean equals(Money amount)
{
return (this.dollars == amount.dollars && this.cents == amount.cents);
}

// Task #2 toString method
// The method compares the instance variables of the
// calling object with instance variables of the parameter
// object for equality and returns true if the dollars and
// the cents of the calling object are the same as the dollars
// and the cents of the parameter object. Otherwise, it returns
// false

public String toString()
{
// creates a string that looks like money
String str = "$" + dollars;
if (cents < 10)
{
str = str + ".0" + cents;
return str;
}
else
{
str = str + "." + cents;
return str;
}

}

}

MoneyDriver.java

import java.io.*;

public class MoneyDriver
{

// This is a driver for testing the class
public static void main (String[] args)
{

final int BEGINNING = 500;
final Money FIRST_AMOUNT = new Money(10.02);
final Money SECOND_AMOUNT = new Money(10.02);
final Money THIRD_AMOUNT = new Money(10.88);
Money balance = new Money (BEGINNING);
System.out.println("The current amount is"+ balance.toString());
balance = balance.add(SECOND_AMOUNT);
System.out.println("Adding" + SECOND_AMOUNT + "gives" + balance.toString());
balance= balance.subtract(THIRD_AMOUNT);
System.out.println("Subtracting" + THIRD_AMOUNT + "gives" + balance.toString());
boolean equal = SECOND_AMOUNT.equals(FIRST_AMOUNT);

if(equal)
System.out.println(SECOND_AMOUNT + "equals" + FIRST_AMOUNT);
else
System.out.println(SECOND_AMOUNT + "does not equal" + FIRST_AMOUNT);

equal = THIRD_AMOUNT.equals(FIRST_AMOUNT);

if(equal)
System.out.println(THIRD_AMOUNT + "equals" + FIRST_AMOUNT);
else
System.out.println(THIRD_AMOUNT + "does not equal" + FIRST_AMOUNT);

}

}

Person.java

public class Person
{

// The person