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 2 of 2

Thread: Java program to display single and plural words

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Java program to display single and plural words

    Hi all I am having some issues with an assignment I need some guidance with the red wording the rest I have been able to finish. Give me a hint or something....thanks

    Write a Java program that modifies Example 2.4, “Computing Changes.” Display the non-zero denominations only. Display singular words for single units like 1 dollar and 1 penny, and display plural words for more than one unit like 2 dollars and 3 pennies. (Use 23.67 to test your programs.) If the user enters zero or a negative amount, your program should exit properly and display a message stating that the amount entered by the user was zero or negative.

    If input is 0, your output should be:

    Your amount is zero

    If input is -2000.25, your output should be:

    Your amount is negative

    //I have coded the above and added it to this I just cannot figure out or wrap my head around the task
    //below if anyone could help me out in my mind it seems simple but for some reason I cant make it work


    If input is 23.67, your output should be:

    Your amount 23.67 consists of 23 dollars 2 quarters 1 dime 1 nickel 2 pennies


    Important Notes : The input and output must use JOptionPane dialog and display boxes.

     
    // ComputeChange.java: Break down an amount into smaller units
     
    import javax.swing.JOptionPane;
     
    public class ComputeChange {
     
    /** Main method */
     
    public static void main(String[] args) {
     
    double amount; // Amount entered from the keyboard
     
    // Receive the amount entered from the keyboard
     
    String amountString = JOptionPane.showInputDialog(null,
     
    "Enter an amount in double, for example 11.56",
     
    "Example 2.4 Input", JOptionPane.QUESTION_MESSAGE);
     
    // Convert string to double
     
    amount = Double.parseDouble(amountString);
     
    int remainingAmount = (int)(amount * 100);
     
    if(amount == 0){
    	String message1=
    		String.format("Your amount is zero.");
    		JOptionPane.showMessageDialog(null, message1);
    			System.exit(0);}
     
    if(amount < 0){
    	String message2=
    		String.format("Your amount is negative.");
    		JOptionPane.showMessageDialog(null, message2);
    			System.exit(0);}
     
    // Find the number of one dollars
     
    int numOfOneDollars = remainingAmount / 100;
     
    remainingAmount = remainingAmount % 100;
     
    // Find the number of quarters in the remaining amount
     
    int numOfQuarters = remainingAmount / 25;
     
    remainingAmount = remainingAmount % 25;
     
    // Find the number of dimes in the remaining amount
     
    int numOfDimes = remainingAmount / 10;
     
    remainingAmount = remainingAmount % 10;
     
    // Find the number of nickels in the remaining amount
     
    int numOfNickels = remainingAmount / 5;
     
    remainingAmount = remainingAmount % 5;
     
    // Find the number of pennies in the remaining amount
     
    int numOfPennies = remainingAmount;
     
    // Display results
     
    String output = "Your amount " + amount + " consists of \n" +
     
    numOfOneDollars + " dollars\n" +
     
    numOfQuarters + " quarters\n" +
     
    numOfDimes + " dimes\n" +
     
    numOfNickels + " nickel\n" +
     
    numOfPennies + " pennies";
     
    JOptionPane.showMessageDialog(null, output,
     
    "Example 2.4 Output", JOptionPane.INFORMATION_MESSAGE);
     
    System.exit(0);
     
    }
     
    }
    Last edited by Deep_4; November 8th, 2012 at 01:30 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help with final piece of assignment Singular/Plural display

    I'm confused on what it is you want... I ran your program and it seems to give the correct output each time.

    edit:

    oh wait, i see what you want. The easiest way to do this is with if statements:
    // replace your String output = ... with this
    String output = "Your amount " + amount + " consists of \n";
            if (numOfOneDollars == 1)
            {
                output += numOfOneDollars + " dollar\n";
            }
            else if (numOfOneDollars > 1)
            {
                output += numOfOneDollars + " dollars\n";
            }
            if (numOfQuarters == 1)
            {
                output += numOfQuarters + " quarter\n";
            }
            else if (numOfQuarters > 1)
            {
                output += numOfQuarters + " quarters\n";
            }
            if (numOfDimes == 1)
            {
                output += numOfDimes + " dime\n";
            }
            else if (numOfDimes > 1)
            {
                output += numOfDimes + " dimes\n";
            }
            if (numOfNickels == 1)
            {
                output += numOfNickels + " nickel\n";
            }
            else if (numOfNickels > 1)
            {
                output += numOfNickels + " nickels\n";
            }
            if (numOfPennies == 1)
            {
                output += numOfPennies + " penny\n";
            }
            else if (numOfPennies > 1)
            {
                output += numOfPennies + " pennies\n";
            }
    Last edited by helloworld922; August 10th, 2009 at 11:19 PM.

Similar Threads

  1. How to display sound signal waveforms continously?
    By ces_31 in forum Java Theory & Questions
    Replies: 4
    Last Post: August 10th, 2009, 09:43 AM
  2. [SOLVED] Need to display multiple images from database on a webpage
    By raghuprasad in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: May 13th, 2009, 03:15 AM
  3. How to display the contents of my queue?
    By rocafella5007 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 30th, 2009, 11:46 AM
  4. New assignments for JAVA beginners
    By Peetah05 in forum File I/O & Other I/O Streams
    Replies: 24
    Last Post: April 24th, 2009, 11:33 AM
  5. [SOLVED] Detecting whether the piece of code is executing?
    By vivek1982 in forum Web Frameworks
    Replies: 4
    Last Post: March 27th, 2009, 06:17 AM