PDA

View Full Version : how do i properly express this in java?


shadylookin
10-01-2006, 09:32 PM
I am new to java and have a question.


public class USDollars {

private int dollars;
private int cents;
private int increase;

public USDollars(int dollars, int cents, int increase){
this.dollars = dollars;
this.cents = cents;
this.increase = increase;
}

public int add(){
fixedCents = cents + increase;
if (fixedCents > 99){
dollars + 1;
fixedCents-100;
}
}

public String toString(){return dollars+"."+fixedCents;}

}


I want to be able to allow for an increase in cents, but obviously I don't want people to be able to have 15dollars and 190cents because that's just not how it is supposed to be. can anyone give me some tips on how to properly express adding to dollars and subtracting to cents when my cents > 99?

also eclipse is giving me an error when i try and add fixedCents in my toString method. How do i write it so that I get the fixedCents variable from the add method?

Thanks for your time and help

Gox
10-01-2006, 11:30 PM
You are getting an error about the fixedcents variable in your toString method because fixedcents no longer exists.

Fixedcents is declared in your add() method. Any variable that is declared in a method is freed/garbage collected when that method terminates. Hence why you have dollars, cents, and increase declared at the top of your file outside of any method. If you want fixedCents to exist for all your methods declare it with dollars, cents and increase.

EDIT: I should mention that at second glance, I don't see where you've declared FixedCents at all... Thus, my above comment may or may not be correct.

EDIT2: Why don't you just change every instance of fixedCents to cents?
I.E.

public int add(){
cents = cents + increase;
if (cents > 99){
dollars + 1;
cents-100;
}
}

public String toString(){
return dollars+"."+cents;
}

Aradon
10-02-2006, 09:48 AM
I don't understand the purpose of the add method. Also it's not returning anything so that would cause another error.

And if we're speaking about errors, your add method does have another bug. What were to happen if someone wanted to add 15 dollars and 300 cents?

The add method would take off 100 cents, add a dollar, and you'd have 16 dollars and 200 cents.

You could fix this in two ways.

The first is through a while loop. Just replace your if with a while and that _should_ work.

The second, and slicker way, is to use the mod operator with the division The mod operator is used to get find out a remainder.

So for example..


public void add(){
cents = cents + increase;
int addDollars = cents / 100;
cents = cents % 100;
dollars += addDollars
}


So what's going on? Well let's go through it.

Let's say the user puts in 15 dollars and 60 cents with an increase of 350 cents.

So when you call add, 350 is added to 60, giving you 410 cents.
Then integer division occurs and 410 is divided by 100, giving us 4 (with integer division it always cuts off the decimal)
Then we mod 410 by 100 in order to get 10 cents as the remainder, putting that into cents.
We then add the 4 that we found previously to dollars, giving us
19 dollars and 10 cents.

You'll also note that I changed the method to void, since you aren't returning anything, it seemed like a more natural thing to do.

Hope you've learned something! :D