In: Computer Science
java programming
Create a class named Money. It should have member variables
for
Member Variables
Store dollars and cents as members (both should be int). They
should be
accessible from only inside the class.
Methods
Write a default constructor that sets members to 0.
Write a two-parameter constructor that sets members to the
parameter
values.
Write get/set methods for the member variables.
Write an override method for toString. The returned string
should be
formatted as a normal money string. For example, if dollars is 2
and cents is
5 then it should return $2.05. If dollars is 3 and cents is 50 then
it should
return $3.50.
Write an override method for equals. It should return true if
both the
dollars and cents are equal and false otherwise. This means you
compare
the dollars in the current to the dollars in the other and you
compare the
cents in one to the cents in another.
Class – Main
The main method should be in this class.
Declare an array of Money in main. The array should have 20
elements.
Populate the array with data from a file. Use a loop to read data
from the
input file. Use the input file data given at the end of this
document.
Write a loop that will print all elements of the array on screen.
You should
have read all data from the input file before doing this.
Write a loop that will add up all the money in the array and
store the total
in another Money instance. After the loop print the total money on
screen
using the Money instance that has the total. You should use
toString to get
the formatted money string. The total money that gets print should
not
have a cents value over 99. For example, 7 dollars and 160 cents
should be
8 dollars and 60 cents. You should have read all data from the
input file
before doing this.
Add two calls to the Money equals override. One to demonstrate
that the
instances are equal and another to demonstrate that they are not
equal.
// Money.java
public class Money {
// fields
private int dollars;
private int cents;
// default constructor to set dollars and cents to
0
public Money()
{
this(0,0);
}
// parameterized constructor to set the dollars cents
to input values
public Money(int dollars, int cents)
{
// validate dollars >=0 , else
set it to 0
if(dollars >= 0)
this.dollars =
dollars;
else
this.dollars =
0;
// validate cents >=0 , else set
it to 0
if(cents >= 0 )
this.cents =
cents;
else
this.cents =
0;
// loop to convert to cents to
[0,99] in case it is >= 100
while(this.cents > 99)
{
this.cents -=
100;
this.dollars++;
}
}
// method to return the dollars
public int getDollars()
{
return dollars;
}
// method to return the cents
public int getCents()
{
return cents;
}
// method to set the dollars
public void setDollars(int dollars)
{
// validate dollars >=0, else
don't update
if(dollars >=0 )
this.dollars =
dollars;
}
// method to set the cents
public void setCents(int cents)
{
// validate cents >=0, else
don't update
if(cents >= 0)
this.cents =
cents;
// loop to convert to cents to
[0,99] in case it is >= 100
while(this.cents > 99)
{
this.cents -=
100;
this.dollars++;
}
}
// method to return String representation of the
Money
public String toString()
{
return
String.format("$%d.%02d",dollars,cents );
}
// method to return true if this and obj are equal
else return false
public boolean equals(Object obj)
{
// validate obj is an object of
Money
if(obj instanceof Money)
{
Money other =
(Money)obj; // cast obj to Money
// two Money
objects are equal only if both dollars and cents are equal
return dollars
== other.dollars && cents == other.cents;
}
return false;
}
}
//end of Money.java
// Main.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws
FileNotFoundException {
int size = 0;
int dollars, cents;
// create an array of 20 Money
objects
Money[] money = new
Money[20];
// create the File object for the
input file
File file = new
File("moneyInput.txt");
// open the input file
Scanner fileScan = new
Scanner(file);
// loop to read dollars and cents
from input file till the end of file or till 20 object data are
read
while((fileScan.hasNext())
&& (size < money.length))
{
dollars =
fileScan.nextInt();
cents =
fileScan.nextInt();
money[size] =
new Money(dollars,cents);
size++;
}
fileScan.close(); // close the
file
// create an object to contain sum
of all money objects
Money totalMoney = new
Money();
// loop to display each Money
object and calculate sum of all money objects
for(int i=0;i<size;i++)
{
System.out.println("Money "+(i+1)+": "+money[i]);
totalMoney.setDollars(totalMoney.getDollars()+money[i].getDollars());
totalMoney.setCents(totalMoney.getCents()+money[i].getCents());
}
// display the total money
System.out.println("Total Money:
"+totalMoney);
// test the equals method
Money m1 = new Money(12,50);
Money m2 = new Money(12,50);
Money m3 = new Money(10,50);
System.out.println(m1+" == "+m2+":
"+(m1.equals(m2)));
System.out.println(m1+" == "+m3+":
"+(m1.equals(m3)));
}
}
// end of Main.java
Output:
Input file: moneyInput.txt (Each line of the file contains 2 integers representing dollar and cent)
Output: