Starting from:

$35

CS212- Object-Oriented Programming in Java: Lab 14 And 15 Solved

Create a new class called Money, and include:
       Instance variables for dollars (int) and cents (int).

       A no-argument constructor which sets dollars and cents to zero.

       A two-argument constructor accepting integer dollars and cents.

      Get methods for dollars and cents.

       A toString method which would print 6 dollars and 5 cents as:

            $ 6.05

                              (hint: to get the leading zero, you can take a substring of the last two characters of 

"0"+the String values of cents.) 

      compareTo and equals methods.

Write a small test program including statements like the following and others to test the methods in the class Money.
 Money m1 = new Money();

 Money m2 = new Money(6,5)

 System.out.println(m1.getCents());

 System.out.println(m2.getDollars());

 System.out.println(m2);

 System.out.println(m1.compareTo(m2));  System.out.println(m1.equals(m2));

Aim: Continuing the Money class.

Continue developing the Money class from Lab 14.

Allow the constructor to take any two positive integers as arguments, such as

  Money m = new Money(5,243);

and adjust the dollars and cents such that cents is less than 100. If done correctly, the statement

  System.out.println(m.toString())

will print  $ 7.43  and not $ 5.243

Write a method add in class Money that will add two money values together:

 Money m1, m2;

  m1 = new Money(4,87);  m2 = new Money(5,243);

 m1.add(m2);

 System.out.println(m1.toString())

should print   $ 12.30

Write a test program to demonstrate that your class works.

More products