This is my Transfer class script, which is the most used class in this project, so I put it here for further reference:
public class Transaction
{
private double totalPrice;
private double amountGiven;
private ConvertToCents converter;
private NumberOfDollars numDollars;
private NumberOfQuarters numQuarters;
private NumberOfDimes numDimes;
private NumberOfNickels numNickels;
Transaction ()
{
totalPrice = 0;
amountGiven = 0;
converter = new ConvertToCents(amountGiven-totalPrice);
}
Transaction (double totalPrice, double amountGiven)
{
this.totalPrice=totalPrice;
this.amountGiven=amountGiven;
converter =new ConvertToCents(amountGiven-totalPrice);
}//constructor
public void changeAmount(double amountGiven)
{
this.amountGiven = amountGiven;
int changeInCents = converter.convert();
converter = new ConvertToCents(amountGiven-totalPrice);
}
public void changeTotalPrice(double totalPrice)
{
this.totalPrice = totalPrice;
}
public void getChange()
{
int changeInCents = converter.convert();
int changeInCents1 = converter.convert();
int changeInCents2 = converter.convert();
numDollars=new NumberOfDollars(changeInCents);
if(totalPrice<=amountGiven)
{System.out.println("" +numDollars.getDollars() + " dollar(s)" );
changeInCents = numDollars.getChange();
numQuarters = new NumberOfQuarters(changeInCents);
changeInCents1 = numQuarters.getChange();
numDimes = new NumberOfDimes(changeInCents1);
changeInCents2 = numDimes.getChange();
numNickels = new NumberOfNickels(changeInCents2);
System.out.println("" +numQuarters.getQuarters() + " quarter(s)");
System.out.println("" +numDimes.getDimes() + " dime(s)");
System.out.println("" +numNickels.getNickels() + " nickel(s)");}
else System.out.println("Insert more cash please.");
}//getChange
}//class Transaction
3. The purpose of the ConvertToCents class is to convert the full value of the given amount into cents so that there will be a value which is usable by each of the NumerOf classes. This is used in the getChange() method of the Transfer class.
5. Complete:
6. This project exemplifies modularization in that we break down the problem into many subcategories. Furthermore, the Transfer of currency is broken down into costs and amount given, which are further broken down into dollars, quarters, dimes, nickels, and even cents.
This project is also an example of abstraction in which we ignored the sub-categories of dollars, quarters, dimes, nickels, and the cents converter, and assumed they were functioning when we worked with the transfer of the currencies. This is abstraction because we ignored the sub-categories to focus on the bigger picture, the transfer.
7. a. converter = new ConvertToCents(amountGiven-totalPrice) is an object reference in the constructor of the Transfer class. It instantiates the object ConvertToCents from the converter class.
b. if(totalPrice<=amountGiven)
{System.out.println("" +numDollars.getDollars() + " dollar(s)" ); is an if statement in the getChange() method of the Transfer class which is a primitive type. This is because booleans statements are primitive types.
c. numNickels = new NumberOfNickels(changeInCents2); is an object creation in the getChange() method of the Transfer class. It creates the object numNickels from the NumberOfNickels class with the formal parameter changeInCents2.
d. Transaction ()
{
totalPrice = 0;
amountGiven = 0;
converter = new ConvertToCents(amountGiven-totalPrice);
}
Transaction (double totalPrice, double amountGiven)
{
this.totalPrice=totalPrice;
this.amountGiven=amountGiven;
converter =new ConvertToCents(amountGiven-totalPrice);
}//constructor
is an example of overloading, where I created two constructor methods to instantiate an object of the Transfer class.
e. There are no internal method calls in this project; however, just to prove that I know the meaning of the word, an internal method call is a call to a method inside the class.
f. changeInCents1 = numQuarters.getChange(); is a statement from the getChange() method of my Transfer class, which presents an external method call. It uses a method getChange() from the numQuarters class, and therefore is external (different class' method).
8.
No comments:
Post a Comment