Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,582 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,929 people online right now. Registration is fast and FREE... Join Now!




Help Please. Stuck on Small Part of Java Code.

 
Reply to this topicStart new topic

Help Please. Stuck on Small Part of Java Code., What am I doing wrong

xcef2005
7 Oct, 2008 - 04:14 PM
Post #1

New D.I.C Head
*

Joined: 7 Oct, 2008
Posts: 13

CODE
package lab04;
import java.util.Scanner;

public class Main {
    
//Programmer:       Kevin Thayer
//Date Due:         September 9, 2008
//Project Number:   4
//Project Name:     Finding Change
//Project Describition:
//        This program should ask the user for the amount of the bill and the
//        payment amount. The program shoudl tell the user how much change is
//        due in bills, and also in coins. This program will now also use
//        functions
    
    public static void main(String[] args) {
        Scanner kbd = new Scanner(System.in);
        double amountOwed;
        double amountGiven;
        double changeDue;
        double coinChange;
        int num100s;
        int num50s;
        int num20s;
        int num10s;
        int num5s;
        int num1s;
        int numQuarters;
        int numDimes;
        int numNickels;
        int numPennies;
        
        //Get Total Due
        amountOwed = GetTotalDue(kbd);
        
        //Get Cash Given
        amountGiven = GetCashGiven(kbd);
        
        //Calculate Change to Return
        changeDue = ChangeToReturn (amountOwed, amountGiven);
        
        //Calculate Number of Each Bill Returned
        num100s = Bills (changeDue, 100);
        changeDue = CalcNewChange(changeDue, num100s, 100);
        num50s = Bills (changeDue, 50);
        num20s = Bills (changeDue, 20);
        num10s = Bills (changeDue, 10);
        num5s = Bills (changeDue, 5);
        num1s  = Bills (changeDue, 1);
        
        //Calculate Number of Each Coin Returned
        numQuarters = Coins (coinChange, 25);
        coinChange = CoinChangeAmount (coinChange, numQuarters, 25);
        numDimes = Coins (coinChange, 10);
        numNickels = Coins (coinChange, 5);
        numPennies = Coins (coinChange, 1);
        
        //Display Bills Returned
        DisplayBillsReturned (num100s, num50s, num20s, num10s, num5s, num1s);
        
        //Display Coins Returned
        DisplayCoinsReturned (numQuarters, numDimes, numNickels, numPennies);
    }
    
    public static double GetTotalDue (Scanner kbd) {
        double amountOwed;
        System.out.println("Amount owed:  ");
        amountOwed = kbd.nextDouble();
        return amountOwed;
    }    
    
    public static double GetCashGiven (Scanner kbd) {
        double amountGiven;
        System.out.println("Amount given:  ");
        amountGiven = kbd.nextDouble();  
        return amountGiven;
    }
    
    public static double ChangeToReturn (double amountGiven , double amountOwed) {
        double changeDue;
        changeDue = amountGiven - amountOwed;
        return changeDue;
    }
    
public static int Bills (double changeDue, int denom) {
        int num;
        num = (int)changeDue / denom;
        return num;
    }
    
    public static double CalcNewChange (double changeDue, int numBills, int denom) {
        double newChange;
        
        newChange = changeDue - numBills * denom;
        return newChange;
    }
    
    public static int BillNum20 (double changeDue) {
        int num20s;
        num20s = (int)changeDue / 20;
        changeDue = changeDue - num20s * 20;
        return num20s;
    }
    public static int BillNum10 (double changeDue) {
        int num10s;
        num10s = (int)changeDue / 10;
        changeDue = changeDue - num10s * 10;
        return num10s;
    }
    
    public static int BillNum5 (double changeDue) {
        int num5s;
        num5s = (int)changeDue / 5;
        changeDue = changeDue - num5s * 5;
        return num5s;
    }
    
    public static int BillNum1 (double changeDue) {
        int num1s;
        num1s = (int)changeDue / 1;
        changeDue = changeDue - num1s * 1;
        return num1s;
    }
    
    public static int CoinChangeAmount (double changeDue, int numCoins, int denom) {
        int coinChange;
        coinChange = (int)(changeDue * 100 + 0.5);
        return coinChange;
    }
    
    public static int Coins (double coinChange, int denom) {
        int num;
        num = (int)coinChange / denom;
        return num;
    }
    
    public static int CoinDime (double coinChange) {
        int numDimes;
        numDimes = (int)coinChange / 10;
        coinChange = coinChange - numDimes * 10;
        return numDimes;
    }
    
    public static int CoinNickel (double coinChange) {
        int numNickels;
        numNickels = (int)coinChange / 5;
        return numNickels;
    }
    
    public static int CoinPennie (double coinChange, int numNickels) {
        int numPennies;
        numPennies = (int)coinChange - numNickels * 5;
        return numPennies;
    }
    
    public static void DisplayBillsReturned (int num100s, int num50s, int num20s,
            int num10s, int num5s, int num1s) {
        
        System.out.println("Return the following Bills: ");
        System.out.format("%5d Hundred Dollar Bills.\n", num100s);
        System.out.format ("%5d Fifty Dollars Bills.\n", num50s);
        System.out.format ("%5d Twenty Dollar Bills.\n", num20s);
        System.out.format ("%5d Ten Dollar Bills.\n", num10s);
        System.out.format ("%5d Five Dollar Bills.\n", num5s);
        System.out.format ("%5d One Dollar Bills.\n", num1s);
    }
    
    public static void DisplayCoinsReturned (int numQuarters, int numDimes,
            int numNickels, int numPennies) {
        System.out.println ("Return the following Coins:  ");
        System.out.format("%5d Quarters.\n", numQuarters);
        System.out.format ("%5d Dimes.\n", numDimes);
        System.out.format ("%5d Nickels.\n", numNickels);
        System.out.format ("%5d Pennies.\n", numPennies);
    }
    
}


Mod Edit added code tags.
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Help Please. Stuck On Small Part Of Java Code.
7 Oct, 2008 - 04:17 PM
Post #2

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,026



Thanked: 82 times
Dream Kudos: 1175
My Contributions
Moved to Java
User is offlineProfile CardPM
+Quote Post

pbl
RE: Help Please. Stuck On Small Part Of Java Code.
7 Oct, 2008 - 04:22 PM
Post #3

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,135



Thanked: 203 times
Dream Kudos: 75
My Contributions
And the problem is:
- code does not compile ? (show compiler error messages)
- compile but crash at run time ? (show error message with stack dump)
- does not display expected behaviour ? (tell us what you expected and what is showed)
User is offlineProfile CardPM
+Quote Post

xcef2005
RE: Help Please. Stuck On Small Part Of Java Code.
7 Oct, 2008 - 04:30 PM
Post #4

New D.I.C Head
*

Joined: 7 Oct, 2008
Posts: 13

Sorry, as you can prob tell this is my first time posting on here. so im alittle new.

Heres the problem im having. Just having the code in NetBeans Under

//Calculate Number of Each Coin Returned
coinChange is underlined in red showing an error.

The error says "Variable coinChange might not have been initialized"

and when I run the code it says:

"numQuarters = Coins (coinChange, 25);
1 error
BUILD FAILED (total time: 0 seconds)"

The idea of the program is for you to be able to ask the user how much the total was. How much your giving to pay for it. And then calculate the change that you are to give the user back in dollars and coins.

Please let me know if you guys need any information and thank you already to those who have already replied and can help me with this situation.

Thanks

User is offlineProfile CardPM
+Quote Post

xcef2005
RE: Help Please. Stuck On Small Part Of Java Code.
7 Oct, 2008 - 04:51 PM
Post #5

New D.I.C Head
*

Joined: 7 Oct, 2008
Posts: 13

Sorry for the double post. Not sure if your allowed to or not. Anyway I got alittle farther on the code somehow but I seem to be having another problem.

CODE
package lab04;
import java.util.Scanner;

public class Main {
    
//Programmer:       Kevin Thayer
//Date Due:         September 9, 2008
//Project Number:   4
//Project Name:     Finding Change
//Project Describition:
//        This program should ask the user for the amount of the bill and the
//        payment amount. The program shoudl tell the user how much change is
//        due in bills, and also in coins. This program will now also use
//        functions
    
    public static void main(String[] args) {
        Scanner kbd = new Scanner(System.in);
        double amountOwed;
        double amountGiven;
        double changeDue;
        double coinChange = 0;
        int num100s;
        int num50s;
        int num20s;
        int num10s;
        int num5s;
        int num1s;
        int numQuarters;
        int numDimes;
        int numNickels;
        int numPennies;
        
        //Get Total Due
        amountOwed = GetTotalDue(kbd);
        
        //Get Cash Given
        amountGiven = GetCashGiven(kbd);
        
        //Calculate Change to Return
        changeDue = ChangeToReturn (amountOwed, amountGiven);
        
        //Calculate Number of Each Bill Returned
        num100s = Bills (changeDue, 100);
        changeDue = CalcNewChange(changeDue, num100s, 100);
        num50s = Bills (changeDue, 50);
        num20s = Bills (changeDue, 20);
        num10s = Bills (changeDue, 10);
        num5s = Bills (changeDue, 5);
        num1s  = Bills (changeDue, 1);
        
        //Calculate Number of Each Coin Returned
        numQuarters = Coins (coinChange, 25);
        coinChange = CoinChangeAmount (coinChange, numQuarters, 25);
        numDimes = Coins (coinChange, 10);
        numNickels = Coins (coinChange, 5);
        numPennies = Coins (coinChange, 1);
        
        //Display Bills Returned
        DisplayBillsReturned (num100s, num50s, num20s, num10s, num5s, num1s);
        
        //Display Coins Returned
        DisplayCoinsReturned (numQuarters, numDimes, numNickels, numPennies);
    }
    
    public static double GetTotalDue (Scanner kbd) {
        double amountOwed;
        System.out.println("Amount owed:  ");
        amountOwed = kbd.nextDouble();
        return amountOwed;
    }    
    
    public static double GetCashGiven (Scanner kbd) {
        double amountGiven;
        System.out.println("Amount given:  ");
        amountGiven = kbd.nextDouble();  
        return amountGiven;
    }
    
    public static double ChangeToReturn (double amountGiven , double amountOwed) {
        double changeDue;
        changeDue = amountGiven - amountOwed;
        return changeDue;
    }
    
public static int Bills (double changeDue, int denom) {
        int num;
        num = (int)changeDue / denom;
        return num;
    }
    
    public static double CalcNewChange (double changeDue, int numBills, int denom) {
        double newChange;
        
        newChange = changeDue - numBills * denom;
        return newChange;
    }
    
    public static int BillNum20 (double changeDue) {
        int num20s;
        num20s = (int)changeDue / 20;
        changeDue = changeDue - num20s * 20;
        return num20s;
    }
    public static int BillNum10 (double changeDue) {
        int num10s;
        num10s = (int)changeDue / 10;
        changeDue = changeDue - num10s * 10;
        return num10s;
    }
    
    public static int BillNum5 (double changeDue) {
        int num5s;
        num5s = (int)changeDue / 5;
        changeDue = changeDue - num5s * 5;
        return num5s;
    }
    
    public static int BillNum1 (double changeDue) {
        int num1s;
        num1s = (int)changeDue / 1;
        changeDue = changeDue - num1s * 1;
        return num1s;
    }
    
    public static int CoinChangeAmount (double changeDue, int numCoins, int denom) {
        int coinChange;
        coinChange = (int)(changeDue * 100 + 0.5);
        return coinChange;
    }
    
    public static int Coins (double coinChange, int denom) {
        int num;
        num = (int)coinChange / denom;
        return num;
    }
    
    public static int CoinDime (double coinChange) {
        int numDimes;
        numDimes = (int)coinChange / 10;
        coinChange = coinChange - numDimes * 10;
        return numDimes;
    }
    
    public static int CoinNickel (double coinChange) {
        int numNickels;
        numNickels = (int)coinChange / 5;
        return numNickels;
    }
    
    public static int CoinPennie (double coinChange, int numNickels) {
        int numPennies;
        numPennies = (int)coinChange - numNickels * 5;
        return numPennies;
    }
    
    public static void DisplayBillsReturned (int num100s, int num50s, int num20s,
            int num10s, int num5s, int num1s) {
        
        System.out.println("Return the following Bills: ");
        System.out.format("%5d Hundred Dollar Bills.\n", num100s);
        System.out.format ("%5d Fifty Dollars Bills.\n", num50s);
        System.out.format ("%5d Twenty Dollar Bills.\n", num20s);
        System.out.format ("%5d Ten Dollar Bills.\n", num10s);
        System.out.format ("%5d Five Dollar Bills.\n", num5s);
        System.out.format ("%5d One Dollar Bills.\n", num1s);
    }
    
    public static void DisplayCoinsReturned (int numQuarters, int numDimes,
            int numNickels, int numPennies) {
        System.out.println ("Return the following Coins:  ");
        System.out.format("%5d Quarters.\n", numQuarters);
        System.out.format ("%5d Dimes.\n", numDimes);
        System.out.format ("%5d Nickels.\n", numNickels);
        System.out.format ("%5d Pennies.\n", numPennies);
    }
    
}


The amount owed and amount given seem to be backwords for some reason. Which makes no sense to me. Is there any way of easily fixing this problem?
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Help Please. Stuck On Small Part Of Java Code.
7 Oct, 2008 - 04:59 PM
Post #6

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 48 times
My Contributions
public static double ChangeToReturn (double amountGiven , double amountOwed) {
double changeDue;
changeDue = amountGiven - amountOwed;
return changeDue;
}

That is your ChangeToReturn method but when you call it, you call it with parameters in opposite order.

//Calculate Change to Return
changeDue = ChangeToReturn (amountOwed, amountGiven);



I'm not sure but that's likely to be it.
User is offlineProfile CardPM
+Quote Post

xcef2005
RE: Help Please. Stuck On Small Part Of Java Code.
7 Oct, 2008 - 05:03 PM
Post #7

New D.I.C Head
*

Joined: 7 Oct, 2008
Posts: 13

Thank you everyone for helping. I am deff going to recomend this site. I actually hit a button on Java and it made my error go away. This prob wasnt the proper way and I dont know how netbeans fixed it for me. But it did. Also I made a silly error of accidently switched around the amount owed and amount given code. Thats why it came out the opposite way.

Thanks again to those who helped
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 12:38AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month