























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
























Doc 13 Assignment 1 Comments References
- Fall Semester, Assignment 1 Comments
Total points 40 24 points for working program 16 points for design, style, code
Score # of students 40 1 39 1 38 2 37 8 36 8 35 7 34-30 14 29-25 5 24-0 2
Need for Comments
public abstract class Currency {
public static Currency getInstance(String newCurrency) { blah }
Format of newCurrency?
public class BEG extends Currency {
// un-instantiable constructor private BEG() { }
Why?
public class Currency { double exchangeRate; double standard;
Standard what?
Need for Comments
public interface Currency {
/**
/**
But what is upper and lower format?
Use is-kind-of Inheritance
public class US {
stuff here }
public class BE extends US { more stuff }
BE is not a kind of US currency
Fields as Parameters
public class USCurrency extends Currency { private long dollars; private long cents; private double value; private double conversionRate;
public USCurrency( double universalValue, double conversionRate) { cents = dollars = 0L; value = universalValue; this.conversionRate = conversionRate; computeValues(); } //page of code removed
private void computeValues() { cents =(long) value * conversionRate* 100; reduce(); } //page of code removed
private void reduce() { dollar += cents / 100; cents %= 100; }
value and conversionRates are fields used as parameters
computeValues() has side effects that are not clear
Double State
public abstract class Currency { private int value;
public Currency( int totalValue ) { value = totalValue; }
public class USCurrency extends Currency { private int dollars; private int cents;
public USCurrency(int dollars, int cents ) { super( cents + dollars * 100); this.dollars = dollars; this.cents = cents; }
The state is duplicated
Make for more work to maintain same values in two different locations
Causes errors - when duplicated states are not correctly updated
One interface with one implementation
public interface Money { public Money add( Money toAdd); public Money subtract( Money toSubtract); // other method in assignment removed }
public abstract class Currency implement Money { public Money add( Money toAdd); public Money substract( Money toSubtract ) { return add( toSubtract.multiple( -1)); } }
public class USCurrency extends Currency { code removed }
Why have the Money interface?
Information Hiding
public class Currency { int value[];
// stuff removed
public int[] add(int[] amount ) { // add the amount to value
What is Currency?
public class UKCurrency { int valueInUSCents
public UKCurrency(int amount ) { valueInUSCents = amount; } etc. }
Keep internal representation internal
public class UKCurrency { int valueInUSCents
public UKCurrency(int amountInPounds ) { valueInUSCents = amount * POUNDS_TO_DOLLARS; }
Cut and Paste
public String toString() { return UKP + " UKP " + UKS + " UKS " + UKE + " UKE " + UKF + " UKF "; }
public void display() { // for debugging System.out.println( UKP + " UKP " + UKS + " UKS " + UKE + " UKE " + UKF + " UKF ");
Why not call toString()?
More Cut and Paste
public class UKCurrency { public Currency add( Currency toAdd) { USCurrency asUS = toAdd.toUS(); UKCurrency ukToAdd = fromUS( asUS); int total = this.value + ukToAdd.value; return new UKCurrency( total ); }
public Currency subtract( Currency toSubtract) { USCurrency asUS = toSubtract.toUS(); UKCurrency ukToSubtract = fromUS( asUS); int total = this.value - ukToSubtract.value; return new UKCurrency( total ); } }
public class DECurrency { public Currency add( Currency toAdd) { USCurrency asUS = toAdd.toUS(); DECurrency deToAdd = fromUS( asUS); int total = this.value + deToAdd.value; return new DECurrency( total ); }
public Currency subtract( Currency toSubtract) { USCurrency asUS = toSubtract.toUS(); DECurrency deToSubtract = fromUS( asUS); int total = this.value - deToSubtract.value; return new DECurrency( total ); } } A lot of repeated structure
Many Methods
public class UKCurrency { int valueInUSCents
public Currency toUS() { //blah }
public Currency toUK() { return this; }
public Currency toDE() { //blah }
public Currency toBE() { //blah }
Repeated in each Currency class
Adding a new currency type means adding methods to all the existing currency classes, which we would like to avoid
multiply, divide, add, subtract
Will make them return a new currency object and not change receiver
How to avoid duplicating code in these methods?
Reuse methods
package currency;
public abstract class Currency { private int amount; // In smallest units of native currency
public abstract Currency add(Currency toAdd); public abstract Currency multiply(float multiplier); public Currency subtract( Currency toSubtract) { return add( toSubtract.multiply( -1)); }
public Currency divide(float divisor) throws ArithmeticException { if ( divisor == 0 ) { throw new ArithmeticException("Zero divide"); } return multiply( 1/divisor); } }
multiply
public class USCurrency extends Currency {
public Currency multiply(float multiplier) { USCurrency result = new USCurrency(); result.amount = (int) Math.floor(amount * multiplier); return result; } }
This leads to nearly identical methods in each Currency subclass