Still learning and am trying to teach myself java-Please help!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hjc
    New Member
    • Oct 2006
    • 14

    #1

    Still learning and am trying to teach myself java-Please help!

    I am trying to write a program that tells the minium number of coins to give out for any amount of change from 1 cent to 99 cents. For example if the amount is 57 cents in the commmand prompt, the output would be:
    57 cents can be given as
    2 quarters, 1 nickel and 2 pennies.

    while using coins 25, 10, 5, and 1

    I am thinking

    int computeCoin(coi nValue,):
    //Precondition: 0<coinValue<100 ;
    //Postconditionre turned value has been set to equal the max number of coins
    //returnedValue*c oinValue
    ??????????????? ????
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by hjc
    I am trying to write a program that tells the minium number of coins to give out for any amount of change from 1 cent to 99 cents. For example if the amount is 57 cents in the commmand prompt, the output would be:
    57 cents can be given as
    2 quarters, 1 nickel and 2 pennies.

    while using coins 25, 10, 5, and 1

    I am thinking

    int computeCoin(coi nValue,):
    //Precondition: 0<coinValue<100 ;
    //Postconditionre turned value has been set to equal the max number of coins
    //returnedValue*c oinValue
    ??????????????? ????
    One of those programs where the language being used does not really affect the design of the solution. Get the algorithm right first. Here is the greedy approach

    change (int n) {
    int[] coins = {100, 25, 10, 5, 1};
    ArrayList sol = new ArrayList();

    int sum = 0;
    while(sum != n) {
    int x = largest item in coins such that sum + x ? n
    if(no such item)
    return "No Solution";
    sol.add(x);
    sum ? sum + x
    }
    return sol

    Comment

    • hjc
      New Member
      • Oct 2006
      • 14

      #3
      Sorry to sound like a idiot but don't I still have to defind the method? how does the program know what int n is? I am really struggling with this---so thank you for the help---even if you don't reply

      QUOTE=r035198x]One of those programs where the language being used does not really affect the design of the solution. Get the algorithm right first. Here is the greedy approach

      change (int n) {
      int[] coins = {100, 25, 10, 5, 1};
      ArrayList sol = new ArrayList();

      int sum = 0;
      while(sum != n) {
      int x = largest item in coins such that sum + x ? n
      if(no such item)
      return "No Solution";
      sol.add(x);
      sum ? sum + x
      }
      return sol[/QUOTE]

      Comment

      • enzoJava
        New Member
        • Nov 2006
        • 4

        #4
        You need to have a loop to go around (if else) statement
        you need to see if you can divide the amount by 25, if yes then divide it, if no then try to divide it by 10, then 5, and finally by 1
        your loop might look like something like this:

        if(...){
        (amount / 25);
        }
        else if (...){
        (amount / 10);
        }
        else if(...){
        (amount / 5);
        }
        ...

        Originally posted by hjc
        I am trying to write a program that tells the minium number of coins to give out for any amount of change from 1 cent to 99 cents. For example if the amount is 57 cents in the commmand prompt, the output would be:
        57 cents can be given as
        2 quarters, 1 nickel and 2 pennies.

        while using coins 25, 10, 5, and 1

        I am thinking

        int computeCoin(coi nValue,):
        //Precondition: 0<coinValue<100 ;
        //Postconditionre turned value has been set to equal the max number of coins
        //returnedValue*c oinValue
        ??????????????? ????

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          And the n is whatever amount of change you have passed to it (if you wanted 57 cents, you would pass it 57, or .57, depending on how you wanted to do it).

          Comment

          • hjc
            New Member
            • Oct 2006
            • 14

            #6
            I am the dumbest human on the plant!

            If I am understand every one correctly my program should look something like the following:

            class Change{
            public static void main(String[] args){
            int n = Integer.parseIn t(args[0]);
            System.out.prin tln("Your change is: ");

            n = amount;

            if(n / 25){
            (amount / 25);
            }
            else if (n / 10){
            (amount / 10);
            }
            else if(n / 5 ){
            (amount / 5);
            }
            else if(n / 1 ){
            (amount / 1);
            }

            change (int n) {
            int[] coins = {100, 25, 10, 5, 1};
            ArrayList sol = new ArrayList();

            int sum = 0;
            while(sum != n) {
            int x = largest item in coins such that sum + x ? n
            if(no such item)
            return "No Solution";
            sol.add(x);
            sum ? sum + x
            }
            return sol
            }
            }
            this is very much incorrect and i am more confused than ever--i need to be able to put an amount in the command prompt after having compiled the program and in the command prompt it would look like this

            c:/java Change 57
            your change on 57 cents is
            2 quarters, 1 nickle and 2 pennies

            Sorry to be a pain!!

            Comment

            • sicarie
              Recognized Expert Specialist
              • Nov 2006
              • 4677

              #7
              Code:
              public class Change{
                 public static void main(String[] args){
              	int n;
              
              	n = amount;
              	
              	if(n / 25){             // this is not a conditional, this is a statement (see below)
              	(amount / 25);    // amount is not declared before this, plus it's not logically correct
              	}
                     . . . . 
              	}	
              
              /* and more code */
              If you're missing declaring 'amount' and missing conditionals, I would suggest reading the following link:



              Anything you use will have to be declared (such as amount) so that the Java compiler knows how to use it. As well, n/25 does not test anything, it's half of an assignment statement. The only way it would fail would be if it returned 0. Anything else is considered "true" by the java compiler. A test involves something like:

              <=, >=, ==, !=

              Those mean less than or equal to, greater than or equal to, equal to, not equal to, with conditionals. (n >= amount)

              The statements that you have are part of conditionals, and if they were in regular statements (not in the 'if'), would need to be assigned to something.

              Check out that tutorial, and then look over your code again.

              Comment

              • maverick19
                New Member
                • Nov 2006
                • 25

                #8
                use the modulo operator

                Comment

                Working...