6 Replies - 837 Views - Last Post: 27 January 2014 - 11:47 PM Rate Topic: -----

#1 FisherC  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 08-January 14

Code Efficiency [Java]

Posted 24 January 2014 - 09:55 AM

This is a simple dollar store change calculator I did for my CS 102 class. Don't worry, I'm not asking for a solution, it meets the requirements and is already turned in. I just want to learn to be better at efficient programming.

What I am curious about is if there is a better or more efficient way to do this. From the kids I have talked to in my class, this is the least complicated and fewest lines of code in the class.

import java.util.Scanner;
/* This program takes the cost of an item at a dollar store, no more than 100 cents, and gives the 
 * change for a dollar broken into quarters, dimes, nickels and pennies.
 * 
 * @author Christian Fisher
 * @version Jan 23 2014
 */

public class makingChange {
	public static void main(String[] args){
		int cost, pennies = 0, nickels = 0, dimes = 0,quarters = 0;
		final int dollar = 100;
		
		Scanner keyboard = new Scanner(System.in);
		
		System.out.println("This program takes in the cost of an item and makes change by displaying");
		System.out.println("the number of quarters, dimes, nickels, and pennies.");
		
		//prompt for the cost, save in variable COST
		System.out.println("Type in the cost of an item in cents: ");
		cost = keyboard.nextInt();
		
		//Calculate the amount of each coin we need
		if (cost <= dollar){
			int tempDiff = dollar - cost;
			quarters = tempDiff/25;
			tempDiff = tempDiff - (quarters * 25);
			dimes = tempDiff/10;
			tempDiff = tempDiff - (dimes * 10);
			nickels = tempDiff/5;
			tempDiff = tempDiff - (nickels * 5);
			pennies = tempDiff/1;
			
			//grammar stuff
			if (quarters > 1 || quarters == 0)
				System.out.println(quarters + " quarters");
			else 
				System.out.println(quarters + " quarter");
			
			if (dimes > 1 || dimes == 0)
				System.out.println(dimes + " dimes");
			else
				System.out.println(dimes + " dime");
			
			if (nickels > 1 || nickels == 0)
				System.out.println(nickels + " nickels");
			else
				System.out.println(nickels + " nickel");
			
			if (pennies > 1 || pennies == 0)
				System.out.println(pennies + " pennies");
			else
				System.out.println(pennies + " penny");
		} else{
			System.out.println("Nothing in the store costs that much.");
		}
		
		
		
		System.out.println("");
		System.out.println("End Program");
		
	}
}



Is This A Good Question/Topic? 1
  • +

Replies To: Code Efficiency [Java]

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Code Efficiency [Java]

Posted 24 January 2014 - 10:06 AM

Moved to Java. Please do not post programming questions in the Student Campus forum.

This line pennies = tempDiff/1; does not need the division. Take it out and your solution is optimal.
Was This Post Helpful? 1
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5641
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Code Efficiency [Java]

Posted 24 January 2014 - 11:42 AM

Any time you have a lot of code going on in one place, a method is good. Any time all that code is in main, a method is preferred.

Possible methods include:
int getCentsFromUser() { /* your code here */ }
String getNamedQty(int amount, String singluar, String plural) { /* your code here */ }
String getChange(int cents) { /* your code here */ }
void main(String[] args){
    System.out.println(getChange(getCentsFromUser()));
    System.out.println("");
    System.out.println("End Program");
}


Was This Post Helpful? 2
  • +
  • -

#4 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,837
  • Joined: 05-April 11

Re: Code Efficiency [Java]

Posted 24 January 2014 - 01:36 PM

It is also common to use a loop which iterates over the coins (quarters to pennies) until the remaining change equal 0
Was This Post Helpful? 1
  • +
  • -

#5 FisherC  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 08-January 14

Re: Code Efficiency [Java]

Posted 27 January 2014 - 10:48 PM

Thank you everyone for the responses.

I must admit though I am quite new to java, even though I have dabbled in the past, so I'm not entirely sure how to implement your suggestions.
The class I referenced is CS 102: Structured programming. The CS 101 class was half Python and half computer background (we went through an overview of all the layers of computing).

Anyways, this class in strictly teaching us programming skills using Java, and since the semester has only just started I'm not very far. We are working with functions this week though, is that what you mean by methods to do everything?

Also, baavgai, you mentioned that the code should be put in methods instead of just in main, so what would you say is the standard practice for what goes in main, and where everything else goes?
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7292
  • View blog
  • Posts: 12,097
  • Joined: 19-March 11

Re: Code Efficiency [Java]

Posted 27 January 2014 - 11:36 PM

View PostFisherC, on 28 January 2014 - 12:48 AM, said:

Also, baavgai, you mentioned that the code should be put in methods instead of just in main, so what would you say is the standard practice for what goes in main, and where everything else goes?



Main should simply start the ball rolling and get out of the way. Basically, it exists to instantiate one or more objects, and to kick off some action by them. Think of it as a constructor for your program as a whole. The parallel is not exact - the constructor terminates and returns an object, while main does not terminate until the program ends (or more precisely, the program ends exactly when main terminates).
As a rule, I don't like to see primitive instructions in main(). No arithmetic operators, no flow control, none of that. Instantiate an object or several, call a few methods, and let them do the work.

So this is literally the main for one medium-sized program that I was working on a little while back:

        public static void main(String[] args) {
                Tournament tourney = new Tournament();
                tourney.setupAndRun();
        }



There's good reasons for this, which we can go into if you're interested in delving into the inner workings of the language. However, some of it is eye-wateringly dull stuff so I don't intend to inflict it on you unless you're actually interested.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7292
  • View blog
  • Posts: 12,097
  • Joined: 19-March 11

Re: Code Efficiency [Java]

Posted 27 January 2014 - 11:47 PM

View PostCasiOo, on 24 January 2014 - 03:36 PM, said:

It is also common to use a loop which iterates over the coins (quarters to pennies) until the remaining change equal 0



This is the main thing that I would have mentioned. Combining this advice with a well-made method, run in a loop, and you can get rid of a lot of that code.

Here's an interesting thing that you might like to think about as well. This algorithm works well for US coins, in that (as far as I know) it always produces the minimal number of coins summing to the requested value. Is this always true? That is, could you either prove that this biggest-first approach produces the optimal result for any set of coins that's sufficient to make change, or else produce a set of coins and a requested value for which it fails to produce an optimal result?
If you find a set for which this algorithm is suboptimal, you might also like to think about how you'd make a better algorithm. If you need a justification for thinking about it, I can tell you that this is a problem which will turn up again later on in your programming career. But more than that, it's a couple of interesting questions which might amuse an hour or two of your time.



*let "plausible" = capable of making change for any amount from 0.01 to 0.99
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1