in java Part One: Define a class named Money whose objects represent amounts of
ID: 3693017 • Letter: I
Question
in java
Part One: Define a class named Money whose objects represent amounts of U.S.
money. The class should have two instance variables of type int for the dollars and
cents in the amount of money. Include a constructor with two parameters of type
int for the dollars and cents, one with one constructor of type int for an amount of
dollars with zero cents, and a no-argument constructor. Include the methods add and
minus for addition and subtraction of amounts of money. These methods should be
static methods, should each have two parameters of type Money, and return a value
of type Money. Include a reasonable set of accessor and mutator methods as well as
the methods equals and toString. Write a test program for your class.
Part Two: Add a second version of the methods for addition and subtraction.
These methods should have the same names as the static version but should use a
calling object and a single argument. For example, this version of the add method
(for addition) has a calling object and one argument. So m1.add(m2) returns the
result of adding the Money objects m1 and m2. Note that your class should have all
these methods; for example, there should be two methods named add .
Alternate Part Two: Add a second version of the methods for addition and subtrac-
tion. These methods should have the same names as the static version but should
use a calling object and a single argument. The methods should be void methods.
The result should be given as the changed value of the calling object. For example,
this version of the add method (for addition) has a calling object and one argu-
ment. Therefore,
m1.add(m2);
changes the values of the instance variables of m1 so they represent the result of
adding m2 to the original version of m1. Note that your class should have all these
methods; for example, there should be two methods named add .
(If you want to do both Part Two and Alternate Part Two, they must be two
classes. You cannot include the methods from both Part Two and Alternate Part
Two in a single class. Do you know why?)
Explanation / Answer
Version 1:
Money.java
package org.students;
public class Money {
private int dollars;
private int cents;
public Money(int dollars, int cents) {
super();
this.dollars = dollars;
this.cents = cents;
}
public Money(int dollars) {
super();
this.dollars = dollars;
}
public Money() {
super();
}
public static Money add(Money m1,Money m2)
{
return new Money(m1.getDollars()+m2.getDollars(),m1.getCents()+m2.getCents());
}
public static Money minus(Money m1,Money m2)
{
return new Money(m1.getDollars()-m2.getDollars(),m1.getCents()-m2.getCents());}
public int getDollars() {
return dollars;
}
public void setDollars(int dollars) {
this.dollars = dollars;
}
public int getCents() {
return cents;
}
public void setCents(int cents) {
this.cents = cents;
}
@Override
public String toString() {
return "dollars=" + dollars + ", cents=" + cents ;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Money other = (Money) obj;
if (cents != other.cents)
return false;
if (dollars != other.dollars)
return false;
return true;
}
}
__________________________________________________________________________________________
Test.java
package org.students;
public class Test {
public static void main(String[] args)
{
Money m1=new Money(12,12);
Money m2=new Money(8,6);
System.out.println("After Adding :"+m1.add(m1, m2));
System.out.println("After Subtracting :"+m2.minus(m1, m2));
}
}
___________________________________________________________________________________________
output:
After Adding :dollars=20, cents=18
After Subtracting :dollars=4, cents=6
___________________________________________________________________________________________
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.