Movatterモバイル変換


[0]ホーム

URL:


Programming & Design
Question:
Java Money method question?
anonymous
2013-02-07 00:25:53 UTC
I am having trouble with my Java assignment. I cannot figure out how to write the plus and equals methods. Any help would be appreciated.

Also here is my list of objectives for the program which I'll be graded on, If you notice any other errors in my code which I may run into later please feel free to point them out for me!

Use BlueJ to create a Money class with two integer instance variables, dollars and cents. Provide the following methods:

1. A two-parameter constructor that initializes the instance variables. The constructor should check that the cents value is between 0 and 99, and if not, transfer some of the cents to the dollars variable to make it between 0 and 99.
2. A default constructor that initializes the dollars to 0 and cents to 1. It should call the two-parameter constructor.
3. A one parameter constructor that initializes the cents value while setting the dollars value to 0. This constructor should also check that the cents value is between 0 and 99, and if not, transfer some of the cents to the dollars variable to make it between 0 and 99.
4. Accessors for dollars and cents
5. The standard toString method
6. The standard equals method that compares if two Money objects have the same state.
7. The plus method that takes a Money object as its parameter. It creates and returns a new Money object representing the sum of the object whose plus() method is being called and the parameter. It does NOT modify the values of the two existing objects.

Finally, create a MoneyDemo class that creates multiple Money objects. This demo class should test all constructors and methods that you have implemented.

Heres my code:

public class Money
{
int dollars, cents;

public Money(int dollars, int cents)
{
this.cents = dollars;
this.dollars = cents;
while (cents<0)
{
cents += 100;
dollars--;
}
while (cents>99)
{
cents -= 100;
dollars++;
}
}

public Money()
{
this(0,0);
this.cents = 1;
this.dollars = 0;
}

public Money(int cents)
{
this.cents = cents;
this.dollars = 0;
while (cents<0)
{
cents += 100;
dollars--;
}
while (cents>99)
{
cents -= 100;
dollars++;
}
}

public int getDollars()
{
return dollars;
}

public int getCents()
{
return cents;
}

public String toString()
{
String str;
str = "Your total amount is: " + "$" + dollars + "." + cents +"";
return str;
}

public boolean equals(Object obj)
{

if (money1.dollars == money2.dollars && money1.cents == money2.cents)
{
return true;
}

else
{
return false;
}
}

public Money plus(int money3)
{
return money3;
}
}


public class MoneyDemo
{
public static void main(String [] args)
{
Money money1 = new Money(13,57);
Money money2 = new Money(540,143);

System.out.print("Money 2 data: ");
System.out.print(money2.getDollars() + " dollars and ");
System.out.println(money2.getCents() + " cents");

Money money3;
money3 = money1.plus(money2);

System.out.print("Money1 data: ");
System.out.print(money1.getDollars() + " dollars and ");
System.out.println(money1.getCents() + " cents" );

System.out.print("Money2 data: ");
System.out.print(money2.getDollars() + " dollars and ");
System.out.println(money2.getCents() + " cents" );

System.out.print("Money 3 data (sum): ");
System.out.print(money3.getDollars() + " dollars and ");
System.out.println(money3.getCents() + " cents" );
}
}
Three answers:
AnalProgrammer
2013-02-07 01:02:05 UTC
Your constructors should be

public Money(int dollars, int cents) {

this.dollars = (cents / 100);

this.cents = cents % 100;

this.dollars += dollars;

}



public Money() {

this(0, 0);

}



public Money(int cents) {

dollars = (cents / 100);

this.cents = cents % 100;

}



The extra methods should be

public boolean equals(Object obj) {



if (obj instanceof Money) {

Money compare = (Money) obj;

return this.dollars == compare.getDollars() && this.cents == compare.getCents();

}

return false;

}



public Money plus(Money money) {

Money plus = new Money(this.dollars + money.getDollars(), this.cents + money.getCents());

return plus;

}



Have fun.
John
2013-02-07 01:56:47 UTC
i believe the requirements are something like these...



import java.util.logging.Logger;



public class MoneyDemo {



public static void main(String[] args) {



Money money = new Money(5, 299);

if (! money.equal(new Money(7, 99))) throw new RuntimeException("1. transfers cent to dollar if over 99.");



money = new Money();

if (! money.equal(new Money(0, 1))) throw new RuntimeException("2. default money initialises to 1 cent.");



money = new Money(299);

if (! money.equal(new Money(2, 99))) throw new RuntimeException("3. transfers cent to dollar if over 99.");

if (! (money.getDollar() == 2 && money.getCent() == 99)) throw new RuntimeException("4. assessor for dollar and cent");



// 5. standard toString...

System.out.println(money);



// 6. equal has been verify in preceding methods.



Money other = money.plus(money);

other.setCent(1);

other.setDollar(1);

if (other.equal(money)) throw new RuntimeException("7. other does not modify existing money object");



System.exit(0);

}



}



class Money {

private Logger log = Logger.getAnonymousLogger();

private int dollar;

private int cent;



public Money() {

this(0, 1);

}



public Money(int cent) {

this(0, cent);

}



public Money(int dollar, int cent) {

setDollar(dollar);

setCent(cent);

log.info(this.toString());

}



public int getDollar() {

return dollar;

}



public void setDollar(int dollar) {

this.dollar += dollar;

}



public int getCent() {

return cent;

}



public void setCent(int cent) {

setDollar(cent / 100);

this.cent += (cent % 100);

}



public Money plus(Money money) {

setDollar(money.getDollar());

setCent(money.getCent());

return new Money(getDollar(), getCent());

}



public boolean equal(Money money) {

return this.getDollar() == money.getDollar() && this.getCent() == money.getCent();

}



@Override

public String toString() {

return String.format("$%d.%d", getDollar(), getCent()).toString();

}

}



/*

Results...



Feb 07, 2013 7:49:50 PM com.example.dollar.Money

INFO: 7.99

Feb 07, 2013 7:49:51 PM com.example.dollar.Money

INFO: 7.99

Feb 07, 2013 7:49:51 PM com.example.dollar.Money

INFO: 0.1

Feb 07, 2013 7:49:51 PM com.example.dollar.Money

INFO: 0.1

Feb 07, 2013 7:49:51 PM com.example.dollar.Money

INFO: 2.99

Feb 07, 2013 7:49:51 PM com.example.dollar.Money

INFO: 2.99

2.99

Feb 07, 2013 7:49:51 PM com.example.dollar.Money

INFO: 5.98



*/
anonymous
2016-03-08 10:34:10 UTC
Change balance = balance + balance; to the following: balance = balance + otherAcct.getBalance(); Hope this helps.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
about -legalese
Loading...

[8]ページ先頭

©2009-2025 Movatter.jp