5 Replies - 497 Views - Last Post: 03 February 2013 - 05:03 AM Rate Topic: -----

#1 dvs.amy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-October 12

Code critique: Java -counting change program

Posted 02 February 2013 - 11:46 AM

I am asking if my code is sound and efficient, I am just a week into Java, and I have noticed professors tend to teach the easiest way, not bashing any professors, BUT industry professionals tend to use better formats which use less code.

I just want anyone's opinion or comment/suggestion, because I believe the less code, the more efficient, the better. Right?

Here's my homework question:

Create a program named change.java. Using input dialog boxes or console input, prompt the user how many pennies, nickels, dimes, and quarters they have. Using a message box or console output, tell the user the total dollar amount of money they have.


Here's what I have, but I just feel like it could be better. Thanks for any and all comments/suggestions:



Spoiler


Is This A Good Question/Topic? 0
  • +

Replies To: Code critique: Java -counting change program

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Code critique: Java -counting change program

Posted 02 February 2013 - 11:58 AM

The more efficient the better? Really depends on what you are doing, but in most cases that is not the case. For example when making games efficiency is key. So of course the more efficiency the better. However in more cases readable and maintainable code is better. Efficient code tends to make everything a lot harder to read/understand/maintain, which can make bigger problems than it solves in terms of a couple of ms in speed.

Of course you want to make efficient code, but maintainability is just as, or if not more important. It's key to find a good balance between the two.


But anyways back to your question. Looks fine to me, but it is of course a very small program. Not much room to go wrong.

The only thing I will say is that class names by convention begin with a capital letter (e.g "Change") and more importantly variables in Java by convention are camel cased so begin with a lower case letter ("myVariable" not "MyVariable"). Also in this case it would be better to name your variables better. If it will hold pennies call it "pennies". You aren't paying for variable name lengths.

Finally I don't know why you are doing this (Total*100.0/100.0) - its pointless :)
Was This Post Helpful? 2
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1414
  • View blog
  • Posts: 3,136
  • Joined: 05-April 11

Re: Code critique: Java -counting change program

Posted 02 February 2013 - 12:06 PM

Total * 100.0 / 100.0 is the same as just writing Total
System.out.println("The total of all your coins is $" + Total);



Why are you declaring a Scanner if you are not using it :) ? Remove it
Was This Post Helpful? 2
  • +
  • -

#4 dvs.amy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-October 12

Re: Code critique: Java -counting change program

Posted 02 February 2013 - 12:50 PM

Thank you for your feedback. :tup:/>
Was This Post Helpful? 0
  • +
  • -

#5 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Code critique: Java -counting change program

Posted 02 February 2013 - 06:51 PM

Glad to see that you have your problem fixed however; I see that your variables aren't exactly the most conventional (I had this problem myself). I would check out: http://docs.oracle.c.../variables.html to gain more information on properly naming your variables. Other than that congratulations on the code working :gunsmilie:/>

EDIT: More information on variable naming conventions: http://java.about.co...conventions.htm

This post has been edited by k3y: 02 February 2013 - 06:52 PM

Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5906
  • View blog
  • Posts: 12,811
  • Joined: 16-October 07

Re: Code critique: Java -counting change program

Posted 03 February 2013 - 05:03 AM

Think of how short this could be if you just used one method?
private static int askUser(String coinTypeName) {
	return Integer.parseInt(
		JOptionPane.showInputDialog(null, "Enter the amount of " + coinTypeName + " you have: ")
	);
}



Look for places in your code where you're doing the same thing over and over, with just a little changed. You probably copy and pasted that and made the change yourself. Make the computer do that work. Also, if you need to make changes in the future, you need only do it in one place.

I'd work in pennies until the end. e.g.
total += askUser("quarters") * 25;

double totalDollars = total / 100.0;
JOptionPane.showMessageDialog(null,"Your change totals to $" + totalDollars);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1