Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Can Somebody Help me?

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Post Can Somebody Help me?

    I was Solving a worded java problem, this is the situation:
    Write a class that calculates and displays the conversion of an entered number of peso into currency denominations - 20s, 10s, 5s, and 1s. Save the class as Dollars.java.

    I think my code (on the top) have missed something
    This is my another code:
    mport java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;

    public class Dollars {
    public static void main(String[] args) throws IOException{

    BufferedReader Kehn = new BufferedReader(new InputStreamReader(System.in));
    int Amnt,Amnt1,Amnt2,Amnt3,Amnt4,Amnt5;
    System.out.println("Enter a value in dollar: ");
    Amnt = Integer.parseInt(Kehn.readLine());
    Amnt5 = Amnt * 20;


    Amnt1 = Amnt /20;
    Amnt1 %= 20;
    Amnt2 = Amnt5 / 10;
    Amnt2 %= 10;
    Amnt3 = Amnt /5;
    Amnt3 %= 5;
    Amnt4 = Amnt / 1;
    Amnt4 %= 1;



    System.out.println(Amnt1);
    System.out.println(Amnt2);
    System.out.println(Amnt3);
    System.out.println(Amnt4);

    If i put 100 the result will be:
    20s = 5
    10s = 0
    5s = 0
    1s = 0

    This is the problem, If i put 50 the result will be:
    20s = 2
    10s = 1
    5s = 0
    1s = 0

    But my result is:
    20s = 2
    10s = 0
    5s = 0
    1s = 0

    another, If i put 15 the result will be:
    20s = 0
    10s = 1
    5s = 1
    1s = 0

    and my result is:
    20s = 0
    10s = 0
    5s = 3
    1s = 0


    Please help what is the formula to correct my mistakes ^^
    Last edited by Jesirism; July 12th, 2012 at 05:05 AM.

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    29
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Can Somebody Help me?

    I compiled and checked the result. The program is running perfectly fine. Let me explain what you program is actually doing. Suppose you wanted to know how much is 1 dollar in peso and you also wanted to know how much is 1 peso in dollar. This is the program that does the same things. if you enter 1 for both the input you can see how much is 1 dollar worth in peso and also how much is 1 peso worth in dollar. Thats all about it. Your program is good.

  3. The Following User Says Thank You to kindk12 For This Useful Post:

    Jesirism (July 12th, 2012)

  4. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Can Somebody Help me?

    I updated the code(The first code i put was incorrect sorry T_T).

  5. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    29
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Can Somebody Help me?

    First let me explain you what your program does. It taken be taken as a money counting machine. Suppose say in your country there are only money bill of 20, 10, 5 and 1. And say you want to know how many bills of 20, 10, 5 and 1 are there in a certain amount. For example if you entered 50, whats the possibility. It can have two 20 bills and one 10 bills. You might wonder you can also have five 10 that can make 50. But our program is not designed to in that way. This should solve your problem
    This is what you need in your program.
    Amnt1 = Amnt/20;
    if(Amnt1 == 0)
    	Amnt2 = Amnt/10;
    else
    	{Amnt = Amnt - 20*Amnt1;
    	 Amnt2 = Amnt/10;
    	}
    if(Amnt2 == 0)
    	Amnt3 = Amnt/5;
    else
    {
    Amnt = Amnt - 10*Amnt2;
    Amnt3 = Amnt/5;
    }
    Amnt4 = Amnt - 5 * Amnt3;
    instead of
    Amnt5 = Amnt * 20;
     
     
    Amnt1 = Amnt /20;
    Amnt1 %= 20;
    Amnt2 = Amnt5 / 10;
    Amnt2 %= 10;
    Amnt3 = Amnt /5;
    Amnt3 %= 5;
    Amnt4 = Amnt / 1;
    Amnt4 %= 1;

  6. The Following User Says Thank You to kindk12 For This Useful Post:

    Jesirism (July 12th, 2012)

  7. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Can Somebody Help me?

    Thanks! I compile your program and it works! all the results are correct. But my instructor wants a simple program(with no if statements T_T).
    But i got an idea on your work

    Amnt = Integer.parseInt(Kehn.readLine());
    Amnt5 = Amnt % 20;
    Amnt6 = Amnt % 10;


    Amnt1 = Amnt /20;
    Amnt1 %= 20;
    Amnt2 = Amnt5 / 10;
    Amnt2 %= 10;
    Amnt3 = Amnt6 /5;
    Amnt3 %= 5;
    Amnt4 = Amnt / 1;
    Amnt4 %= 1;

    Same Result ^^ Problem Solved!