Results 1 to 8 of 8

Thread: New Java Program

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    6

    New Java Program

    Hi i'm stuck on a program that i'm trying to complete. What I'm trying to do is write a Java program that breaks down money into smaller units. Displaying 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. I almost finish with the program but i'm stuck on trying to get the if else statement for the amount less than or equla to zero here is my program:


    // 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);



    // 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;

    // If the number is zero or less<-------
    if (input ==0); <-------
    JOptionPane.showMessageDialog (null, "The amount is zero");<-------

    if (input < 0); <-------
    JOptionPane.showMessageDialog (null, "The amount is negative");<-------

    if (else input > 0 = 23.67); <-------

    // 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);

    }//end main method

    }// end Compute Change

    I put the arrows is where the problem is. I think I can use the if...else statements but can't figure out where or when to use them.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: New Java Program

    No I really doubt you know anything about writing an If statement. read a book or a tutorial or something
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: New Java Program

    ComputerJy this comes from a book.

    i think this would help you:


    Code:
    import javax.swing.JOptionPane;
    
    public class ComputeChange {
    
    public static void main(String[] args) {
    double amount; 
    
    String amountString = JOptionPane.showInputDialog(null,
    "Enter an amount in double, for example 11.56",
    "Example 2.4 Input", JOptionPane.QUESTION_MESSAGE);
    
    amount = Double.parseDouble(amountString);
    
    public int remainingAmount = (int)(amount * 100);
    public int numOfOneDollars = remainingAmount / 100;
    public int remainingAmount = remainingAmount % 100;
    public int numOfQuarters = remainingAmount / 25;
    public int remainingAmount = remainingAmount % 25;
    public int numOfDimes = remainingAmount / 10;
    public int remainingAmount = remainingAmount % 10;
    public int numOfNickels = remainingAmount / 5;
    public int remainingAmount = remainingAmount % 5;
    public int numOfPennies = remainingAmount;
    
    if (input == 0) {
    JOptionPane.showMessageDialog (null, "The amount is zero");
    }
    if (input <= 0) {
    JOptionPane.showMessageDialog (null, "The amount is negative");
    }
    if (input >= 0 == 23.67) {
    
    
    
    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);
    }
    }
    }
    any errors please post.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: New Java Program

    Just take a look at your "if (input <= 0) " then take a look at his.. you'll know what I meant
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: New Java Program

    he uses semikolon isnt my code correct?

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: New Java Program

    Nope, sorry
    1- local variables "public int numOfOneDollars = remainingAmount / 100;" for example don't have any access modifiers (drop the public to fix this)
    2- You cannot declare a variable with the same name twice in a single block "int remainingAmount = remainingAmount % 100;" for example (drop the 'int' to fix this)
    3- You must have all variables declared and assigned to a value - the variable input is never declared or assigned
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7
    Hyperactive Member g4hsean's Avatar
    Join Date
    May 2006
    Posts
    267

    Re: New Java Program

    wow, i would go with ComputerJy on this one.... read a book then you will understand your problem a little more. And even then you could simplify this down to a quarter of the code you have written!

    And good coding practice says that you must declare values to be private within a method and or class.

    Also im going to have to agree with ComputerJy about declaring multiple variables in the same block.

    i will post some code that might just bring an answer to your question.
    Last edited by g4hsean; Nov 26th, 2008 at 05:52 PM.
    Coding's a Breeze if you'r at ease! GOD THATS CORNY!!
    -

  8. #8
    Hyperactive Member g4hsean's Avatar
    Join Date
    May 2006
    Posts
    267

    Cool Re: New Java Program

    Ok, here is A WORKING VERSION of your program. Before i start i would like to say that you MUST read a java book from cover to cover doing exercises along the way because i found many and I MEAN MANY! mistakes that should of never of been made if you had a clue on how to program.

    Anyways i am not here to criticize i am here to help so, i have taken out the bugs so that the program will function. This code is in no way well coded, all i have done is made it work that's it. I would suggest in redoing the coding exercise implementing good coding practices. I have also indented the code as it should be.

    Code:
    // 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);
    
    
    
    		// 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;
    
    		// If the number is zero or less<-------
    		if (amount ==0.00)
    		JOptionPane.showMessageDialog (null, "The amount is zero");
    
    		if (amount < 0.00)
    		JOptionPane.showMessageDialog (null, "The amount is negative");
    
    		if (amount > 0.00 && amount == 23.67)
    		{
    			// 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);
    		}
    	}//end main method
    
    }// end Compute Change
    One thing i would like to give you a little credit on is that at least you put comments.

    Keep on reading, for you have much to learn!
    Coding's a Breeze if you'r at ease! GOD THATS CORNY!!
    -

Posting Permissions

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



Click Here to Expand Forum to Full Width