0

Okay, so I just started this homework lab and I'm having a little bit of trouble. I've searched for any solutions but they all seem way more complex than what I can comprehend. I'm looking for the dollars needed and change needed to buy something. I just came up with some numbers and when i run the file i get an error. Can anyone help me find how to complete the equations to get the sum value. Also dollarsNeeded needs to be int and not double. The changeNeeded can be double. Any help would be greatly appreciated.

java:30: error: possible loss of precision
    findDollars = xboxOne + newGame;
                          ^
required: int
found:    double
1 error

public class MoneyNeeded
{
public static void main(String[] args)
{
    double xboxOne, newGame, moneyNeeded;
    xboxOne = 320.41;
    newGame = 64.36;  
    moneyNeeded = findMoney(xboxOne, newGame);
    System.out.println(moneyNeeded);

    int dollarsNeeded;
    dollarsNeeded = findDollars(xboxOne, newGame);
    System.out.println(dollarsNeeded);

    double changeNeeded;
    changeNeeded = findChange(xboxOne, newGame);
    System.out.println(changeNeeded);
}

public static double findMoney(double xboxOne, double newGame)
{
    double findMoney;
    findMoney = xboxOne + newGame;
    return findMoney;
}

public static int findDollars(double xboxOne, double newGame)
{
    int findDollars;
    findDollars = xboxOne + newGame;
    return findDollars;
}

public static double findChange(double findDollars, double findMoney)
{
    double findChange;
    findChange = findMoney % findDollars;
    return findChange;
}
}

3 Answers3

0

You can't sum two doubles and put the result in an int, because the int cannot contain the decimal part, so you will .. well .. loose precision.

If you are ok with loosing the decimal part, you can explicitly drop it in away :

findDollars = (int)(xboxOne + newGame); // Simply truncate the decimal part away

But usually is not a good idea, cause 0.2 + 0.2 will return 0.

Simone Gianni
  • 11,426
  • 40
  • 49
0

I think your having issues because you're using datatypes as parameter for your methods, when they should be used for arguments in the method. I' m not sure though, so make sure to double check this.

CuriousDev
  • 17
  • 2
0

So you can do :

findDollars = xboxOne.intValue() + newGame.intValue();

or

findDollars = (int)(xboxOne + newGame);
user3145373 ツ
  • 7,858
  • 6
  • 43
  • 62
  • This is my output. I need to simply subtract the first two methods to get 0.77 but instead i get -256. I dont why this is the output. --------------------Configuration: -------------------- 384.77000000000004 384 -256.05 Process completed. – Junior110697 Sep 03 '14 at 03:18
  • why have you use Integer in your one method ? and you want to subtract `findChange()` and `findMoney()`'s output from `findDollars()`'s output ? right. – user3145373 ツ Sep 03 '14 at 03:26
  • why don't you use `-` instead of `%` ? – user3145373 ツ Sep 03 '14 at 03:34
  • & i think you are passing wrong argument in change function, you ll have to pass the first 2 functions output in that as argument. – user3145373 ツ Sep 03 '14 at 03:36