Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By jim829

Thread: trouble with a java problem

  1. #1
    fortuate1273 is offline Member
    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    Unhappy trouble with a java problem

    This method accepts 1 integer, amount (the amount of money). Output the minimum number of in quarters, dimes, nickels and pennies used to make up the amount. For example, an amount of 32 would require 1 quarter, 1 nickel and 2 pennies.




    This is the question^

    My codes are:
    public static int change (int amount) {
    int quarters = amount / 25 ;
    int firstresult = amount % 25 ;
    return quarters ;
    int nickel = firstresult / 5 ;
    int secondresult = firstresult % 55 ;
    return nickel ;
    int penny = secondresult / 1 ;
    int thirdresult = secondresult % 1 ;
    return penny ;
    }

    The codes were working when i used System.out.println instead of return, but our teacher required us to use return (functions).

    I get the compile error: Unreachable statement.

    I am very new to java and I hope to learn more about it.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: trouble with a java problem

    You are doing unconditional returns. This means that any subsequent statements will never be reached. So you should probably break this up into several methods, each one figuring its own denomination requirements.

    A more efficient approach would be using a class or an array and use a single method.

    Regards,
    Jim
    zFollette likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: trouble with a java problem

    The way I would solve this is similar to how I would solve a binary conversion.

    Say the amount is 32, like your example.

    Since 32 > 25, we can assume there is one quarter. Then we would subtract 25 from 32 to get: 7. Since 7 < 10, we can assume there are no dimes. We do nothing for that. Since 7 > 5, we can assume that there is a nickle. We then subtract 5 from 7 to get: 2. Since 2 > 1, we can assume that there is a penny, subtract 1 from 2 to get: 1, since 1 = 1, we can assume that there is a penny. 1 - 1 = 0. Since the remainder is 0, the problem is solved. 1 Quarter, 0 Dimes, 1 Nickle, and 2 Pennies. My way may not be the fastest way of solving, but it gets the right answer.

    Simple, yet effective way:

    Java Code:
    private static void change(int money) {
            int num = money;
    
            int q = 0;
            int d = 0;
            int n = 0;
            int p = 0;
    
            while (num != 0) {
                if (num >= 25) {
                    q++;
                    num -= 25;
                } else if (num >= 10) {
                    d++;
                    num -= 10;
                } else if (num >= 5) {
                    n++;
                    num -= 5;
                } else if (num >= 1) {
                    p++;
                    num--;
                }
            }
    
            System.out.println("Quarters: " + q);
            System.out.println("Dimes   : " + d);
            System.out.println("Nickles : " + n);
            System.out.println("Pennies : " + p);
        }
    It gets your expected output:
    Java Code:
    Enter amount to be broken: 32
    Quarters: 1
    Dimes   : 0
    Nickles : 1
    Pennies : 2
    This could move easily be converted to return statements, rather than modulus division. I think so anyway. Maybe I just like more code. Lol.

    However, your problem is very simple. When you return, it ends the method. So any code after the first return is un-reachable, since the method would have ended before any other code gets the chance to run.

    You will have to implement some kind of loop to call the change function, or do as jim said.
    Last edited by zFollette; 02-15-2014 at 02:46 AM.
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  4. #4
    notivago is offline Heavy Coffe Drinker
    Join Date
    Feb 2014
    Location
    São Paulo, Brazil
    Posts
    29
    Rep Power
    0

    Default Re: trouble with a java problem

    @OP
    The reason the compiler error is happening is that the return statement ends the function when it is encountered. It does not just give the value but also terminates it. So:

    Java Code:
    int a = 1;
    return a; // This terminates the function and returns the value of a whis is 1
    a++;  // This will never be executed because the flow cannot reach it, it is thus an unreachble block of code.
    Your teacher probably wants that you break your function in smaller pieces, each one returning the amount for a given coin so that you understand the working of a return statement:
    [code]
    int quarters = findTheAmountOfQuarters(amount);
    //...
    public int findTheAmountOfQuarters(amount) {
    return amount / 25;
    }
    [/quote]

    @Others
    None of this is OOP, I understand OOP is just learning the language and does not understant even returns statements yet, but should we not be sugesting the modeling of a Money class that would be configured to represtent a given amount in monetary units and then dispensed to fill the ammount?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: trouble with a java problem

    Quote Originally Posted by notivago View Post
    but should we not be sugesting the modeling of a Money class that would be configured to represtent a given amount in monetary units and then dispensed to fill the ammount?
    Not necessarily. A lot of folks who come here are learning their first language. So they just start out writing code in main, not really understanding the class keyword or the significance of main. They they migrate to simply doing functional programming. Even when they begin to use classes, they are using them more like C structures than objects. I mean you can always suggest something, but you may get the "we haven't had that yet" reply.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 3
    Last Post: 04-12-2011, 07:44 AM
  2. Font trouble in Java 2D
    By eskiom in forum Java 2D
    Replies: 1
    Last Post: 10-24-2010, 09:29 PM
  3. Replies: 12
    Last Post: 05-06-2010, 11:54 AM
  4. Trouble with Dr Java.
    By davefanelli in forum New To Java
    Replies: 1
    Last Post: 10-22-2008, 07:20 PM
  5. Trouble with a search hw problem.
    By frasifrasi in forum New To Java
    Replies: 3
    Last Post: 07-10-2008, 10:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •