Docsity

Object Oriented Programming and Design - Assignment 1 | CS 535, Assignments of Computer Science

Toggle sidebar
bg1
10/30/00 Doc 13 Assignment 2 Comments slide # 1
CS 535 Object-Oriented Programming & Design
Fall Semester, 2000
Doc 13 Assignment 1 Comments
Contents
References ...................................................................................1
Assignment 1 Comments.............................................................. 2
Need for Comments ................................................................... 3
Exceptions..................................................................................6
Use is-kind-of Inheritance........................................................... 7
Fields as Parameters .................................................................8
Double State............................................................................. 10
One interface with one implementation.................................... 11
Positional Data ......................................................................... 12
Information Hiding .................................................................... 13
Cut and Paste........................................................................... 14
Many Methods..........................................................................17
Some Solution ............................................................................18
multiply, divide, add, subtract................................................... 19
multiply ..................................................................................... 20
Implementing add.....................................................................23
Implementing convertTo...........................................................24
Further Issues ..........................................................................31
References
Student papers
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download Object Oriented Programming and Design - Assignment 1 | CS 535 and more Assignments Computer Science in PDF only on Docsity!

CS 535 Object-Oriented Programming & Design

Doc 13 Assignment 1 Comments References

 - Fall Semester, 
  • References Contents
  • Assignment 1 Comments..............................................................
    • Need for Comments
    • Exceptions..................................................................................
    • Use is-kind-of Inheritance...........................................................
    • Fields as Parameters
    • Double State.............................................................................
    • One interface with one implementation
    • Positional Data
    • Information Hiding
    • Cut and Paste...........................................................................
    • Many Methods..........................................................................
  • Some Solution
    • multiply, divide, add, subtract
    • multiply
    • Implementing add.....................................................................
    • Implementing convertTo...........................................................
    • Further Issues

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 {

/**

  • Change the currency to lower format
  • @return float, a float number after change public int lowerFormat();

/**

  • Change the currency to upper format
  • @return float, a float number after change public float upperFormat();

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