Results 1 to 4 of 4
  1. #1
    atac57 is offline Member
    Join Date
    Jan 2012
    Posts
    24
    Rep Power
    0

    Default Critique my change return program

    This pretty much does exactly what I want, I just want to know what I could improve on. Any suggestions? Thanks.

    Java Code:
    import java.util.Scanner;
    import java.text.DecimalFormat;
    
    public class Simulation7{
    
    	public static void main(String[] args){
    		
    		double cost, paid, change, coins;
    		int money, amount, quarters, dimes, nickels, pennies;
    		float cents;
    		
    		Scanner scanner = new Scanner(System.in);
    		System.out.println("Total cost: ");
    		cost = scanner.nextDouble();
    		System.out.println("Amount paid: ");
    		paid = scanner.nextDouble();
    		
    		change = paid - cost;
    		DecimalFormat df = new DecimalFormat("#.00");
    		System.out.println("Change: $" + df.format(change));
    		
    		money = (int) change; 
    		System.out.println(df.format(money) + " dollars");
    		coins = (change - money) * 100;
    		cents = (float) coins;	
    		
    		amount = (int)cents;
    		quarters = amount/25;
    		amount = amount%25;
    		dimes = amount/10;
    		amount = amount%10;
    		nickels = amount/5;
    		amount = amount%5;
    		pennies = amount;
    		
    		System.out.println(quarters + " quarters");
    		System.out.println(dimes + " dimes");
    		System.out.println(nickels + " nickels");
    		System.out.println(pennies + " pennies");
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: Critique my change return program

    Well done, except for a few redundant variables: in line #24 you calculate the change measured in cents; in line #25 you assign that same amount to another variable and in line #27 you do the same with yet another variable.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Mar 2011
    Posts
    94
    Rep Power
    0

    Default Re: Critique my change return program

    Looks good, but I would test for negative values in Line 18 and print an error if that is the case.

  4. #4
    atac57 is offline Member
    Join Date
    Jan 2012
    Posts
    24
    Rep Power
    0

    Default Re: Critique my change return program

    Thank you guys, did both those things. Works perfect!

Similar Threads

  1. Java Code Critique
    By CoderJava in forum New To Java
    Replies: 4
    Last Post: 06-30-2012, 11:14 PM
  2. Replies: 12
    Last Post: 04-20-2012, 05:43 PM
  3. Critique my first Java Program!
    By Lucid15 in forum New To Java
    Replies: 3
    Last Post: 01-26-2012, 08:56 AM
  4. Please critique
    By jim01 in forum New To Java
    Replies: 4
    Last Post: 09-24-2010, 03:43 AM
  5. Critique Java Game: Help Me Improve
    By gretty in forum New To Java
    Replies: 1
    Last Post: 07-15-2010, 04:30 AM

Posting Permissions

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