Fields
2 / 2  -is there a static field(s) for the total amount of money?
2 / 2  -is there a field(s) for the value of money?
 
Constructors
1 / 1  -does the no argument constructor initialize the money correctly?
1 / 1  -does the constructor throw the correct exception when required?
1 / 1  -does the constructor initialize the money correctly?
1 / 1  -does the copy constructor copy the other money correctly?

Total amount of money created
2 / 2  -do all constructors and methods correctly count the total amount of 
       money created?
 
Methods
-getCents
0.5 / 1  -returns the number of cents for this money?
 
-getDollars
0.5 / 1  -returns the number of dollars for this money?

-getTotalDollars
1 / 1  -returns the correct amount of total money rounded to the nearest
       dollars?

-addCents
1 / 1  -adds c to the cents for this money?
1 / 1  -correctly deals with the case when the cents for this money 
       becomes greater than 99 (can also be done in getCents and
       getDollars)

-addDollars
1 / 1  -adds d the dollars for this money?

-toString
1 / 1  -returns the correct string when cents is less than 10?
1 / 1  -returns the correct string when cents is greater than 9?
--------------------
TA Comments:
-cent, dollar, and current should all be private 

-in getCents:

                if (this.cent < CENTS_PER_DOLLAR)
                        cents = this.cent % CENTS_PER_DOLLAR;

should be:

                if (this.cent >= CENTS_PER_DOLLAR)
                        cents = this.cent % CENTS_PER_DOLLAR;

-in getDollars:

                if (this.cent < CENTS_PER_DOLLAR)
                        dollars += this.cent / CENTS_PER_DOLLAR;

should be:

                if (this.cent >= CENTS_PER_DOLLAR)
                        dollars += this.cent / CENTS_PER_DOLLAR;

--------------------
Unit tester output:

YOUR SUMBISSION FAILED SOME UNIT TESTS
Here is the test output:

java -classpath .:/home/burton/work/teaching/2017F/2030/marking/secEtest2B/_jar/* org.junit.runner.JUnitCore test2.Test2Suite
JUnit version 4.12
...........E...
Time: 0.011
There was 1 failure:

1) test10_addCents(test2.MoneyTest)
java.lang.AssertionError: addCents failed expected:<0> but was:<100>

FAILURES!!!
Tests run: 14,  Failures: 1

--------------------

Your submission:

package test2;

/**
 * A class for representing quantities of money consisting of
 * dollars and cents. Money objects behave more like amounts
 * that appear on receipts or bank accounts rather than 
 * physical bills and coins; the method <code>getCents</code>
 * will always return a value between 0 and 99, even if the
 * Money object was created using only a large number of cents:
 * 
 * <pre>
 * Money m = new Money(0, 957);      // 0 dollars and 957 cents
 * int dollars = m.getDollars();
 * int cents = m.getCents();
 * </pre>
 * 
 * <p>
 * In the above example, <code>dollars</code> will be equal to 9
 * and <code>cents</code> will be equal to 57.
 * 
 * <p>
 * The class keeps track of the current total value of all Money
 * object created by the class. See the test document for a
 * detailed example.
 *
 */
public class Money {

        /**
         * The number of cents in a dollar.
         */
        public static final int CENTS_PER_DOLLAR = 100;
        
        
        public int cent;
        public int dollar;
        public static Money current = new Money();
        /**
         * Initializes this money object so that it has zero dollars and zero cents.
         */
        public Money() {
                this.cent = 0;
                this.dollar = 0;
        }

        /**
         * Initializes this money object so that it has the specified number of
         * dollars and the specified number of cents.
         * 
         * @param dollars
         *            the number of dollars
         * @param cents
         *            the number of cents
         * @throws IllegalArgumentException
         *             if dollars is less than zero
         * @throws IllegalArgumentException
         *             if cents is less than zero
         */
        public Money(int dollars, int cents) throws IllegalArgumentException
        {
                if ((dollars < 0) || (cents < 0))
                                throw new IllegalArgumentException("dollars or cents cannot be negative");
                this.dollar = dollars;
                this.cent = cents;
                
                current.cent += this.cent;
                current.dollar += this.dollar;
        }

        /**
         * Initializes this money object so that it has the number of
         * dollars and cents equal to that of the specified money object.
         * 
         * @param other a money object
         */
        public Money(Money other) {
                this.dollar = other.dollar;
                this.cent = other.cent;
                
                current.cent += this.cent;
                current.dollar += this.dollar;
        
        }

        /**
         * Returns the total value of all created money objects rounded
         * to the nearest dollar. See the test document for a detailed
         * example.
         * 
         * @return the total value of all created money objects rounded
         * to the nearest dollar
         */
        public static int getTotalDollars() {
                int result = current.getDollars() + (int)Math.round((double)current.getCents()/100);
                return result;
                        
        }

        /**
         * Add the specified number of cents to this money object.
         * 
         * @param c
         *            the number of cents to add to this money object
         * @throws IllegalArgumentException
         *             if c is less than zero
         */
        public void addCents(int c)     throws IllegalArgumentException{
                if (c < 0)
                        throw new IllegalArgumentException("Cannot add negative cents");
                this.cent += c;
                current.cent += c;
        
        }

        /**
         * Add the specified number of dollars to this money object.
         * 
         * @param d
         *            the number of dollars to add to this money object
         * @throws IllegalArgumentException
         *             if d is less than zero
         */
        public void addDollars(int d) throws IllegalArgumentException{
                if (d < 0)
                        throw new IllegalArgumentException("Cannot add negative dollars");
                this.dollar +=d;
                current.dollar += d;
        }

        /**
         * Returns the number of cents belonging to this money object. The
         * returned value is always between 0 and 99, inclusive. For example:
         * 
         * <pre>
         * Money m = new Money(0, 999);      // 0 dollars and 999 cents
         * int dollars = m.getDollars();
         * int cents = m.getCents();
         * </pre>
         * 
         * <p>
         * In the example, <code>dollars</code> will be equal to 9 and
         * <code>cents</code> will be equal to 99.
         * 
         * @return the number of cents belonging to this money object
         */
        public int getCents() {
                int cents = 0;
                if (this.cent < CENTS_PER_DOLLAR)
                        cents = this.cent % CENTS_PER_DOLLAR;
                else cents = this.cent;
                return cents;
        }

        /**
         * Returns the number of dollars belonging to this money object.
         * The returned value is always greater than or equal to 0.
         * For example:
         * 
         * <pre>
         * Money m = new Money(0, 999);      // 0 dollars and 999 cents
         * int dollars = m.getDollars();
         * int cents = m.getCents();
         * </pre>
         * 
         * <p>
         * In the example, <code>dollars</code> will be equal to 9 and
         * <code>cents</code> will be equal to 99.
         * 
         * @return the number of dollars belonging to this money object
         */
        public int getDollars() {
                int dollars = this.dollar;
                if (this.cent < CENTS_PER_DOLLAR)
                        dollars += this.cent / CENTS_PER_DOLLAR;
                return dollars;
        }

        /**
         * Returns a string representation of this money object. The returned string
         * begins with a dollar sign, and is followed by the number of dollars of
         * this money object, followed by a period, followed by the number of cents
         * of this money object.
         * 
         * <p>
         * If the number of cents of this money object is a single digit, then the
         * returned string has a zero in front of the number of cents. For example:
         * 
         * <pre>
         * Money m = new Money(1, 5);          // one dollar and five cents
         * String s = m.toString();
         * </pre>
         * 
         * <p>
         * the string s will be equal to the string <code>"$1.05"</code>
         * 
         * @return a string representation of this money object
         */
        @Override
        public String toString() {
                if (this.getCents() < 10)
                        return "$" + this.getDollars() + ".0" + this.getCents();
                else
                        return "$" + this.getDollars() + "." + this.getCents();
        }
        
        
        
        public static void main(String[] args) {
            Money m1 = new Money(1, 0);        // $1.00
            Money m2 = new Money(2, 0);        // $2.00
            Money m3 = new Money(1, 49);       // $1.49

            // the total current value of all Money object is $4.49

            System.out.println(Money.getTotalDollars());   // prints 4 (rounds to nearest dollar)

            m3.addCents(2);

            // the total current value of all Money object is $4.51

            System.out.println(Money.getTotalDollars());   // prints 5 (rounds to nearest dollar)

            m1.addDollars(10);

            // the total current value of all Money object is $14.51

            System.out.println(Money.getTotalDollars());   // prints 15 (rounds to nearest dollar)

            Money m4 = new Money(100, 49);     // $100.49

            // the total current value of all Money object is $115.00
            
            System.out.println(Money.getTotalDollars());   // prints 115 (rounds to nearest dollar)
        }
        
}

--------------------

Written answers marking scheme

A 
1 / 1  no?
0 / 1  is a suitable invariant given?

B
1 / 1  yes? (or no depending on the explanation in part (b))
0 / 2  is there a suitable explanation?

C 
0 / 1  this == obj should return true instead of false?
0 / 1  does not check if (obj == null) ?
1 / 1  && should be || ?
0 / 2  x.equals(null) does not return false, or x.equals(x) returns false?

D 
0 / 1  static factory method?
0 / 1  is there a suitable explanation?

--------------------
TA Comments

A (a) No. The constructors and mutator methods all throw exceptions preventing
  the number of cents and dollars from becoming negative. 

  (b)
  1. The amount of cents is greater than or equal to 0.
  2. The amount of dollars is greater than or equal to 0.
  3. The total number of dollars is greater than or equal to 0. 

B (a) Yes, the implementation returns an int value. (Arguably, the answer 
  is no; see part (b) below) 

  (b) No. The calculation can overflow the range of int which will cause the
  wrong value to be returned. 

C (a) 
  1. x.equals(x) returns false instead of true
  2. The method returns false if and only if both the number of dollars and the 
     number of cents are different (the && should be ||)
  3. The method throws an exception if obj is null. 

  (b) No. x.equals(null) does not return false and x.equals(x) returns false. 

D (a) static factory method 

  (b) No, because all three methods have a single parameter of type int and we 
  cannot have three constructors with the same signature. 

--------------------

Your submission:

A.
a)      It is impossible to have negative amount of money, since there is throw IllegalException when dollar or cent is less than 0.

b)      addDollars(), addCents()

B.
a)      It is a legal implementation of CompareTo.

b)      It is not a good implementation, because it only compare total number of cents but not returning both dollars and cents 

C.
a)      the following part is errors:
/*
Money other = (Money) obj;
if (this.getDollars() != other.getDollars() && this.getCents() != other.getCents()) {
        return false;
*/

b)      it should be using || (or) instead of && (and), because whichever is false, then return false, but not when both dollar and cent are not equal then return false.

D.
a)

b)      we can provide three constructors "addNickels", "addDimes", and "addQuarters" to store the number of Nickels, Dimes, and Quarters.
        they can be switched to be cents by multiplying them to 5, 10 or 25.
        

--------------------