• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make my program better.  RSS feed

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im Done with this short program anybody with ideas on how to make it better.


 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at the main method, I have three comments:

1) You can declare answer and assign the value on the same line
2) The compare to is the same as if (! answer.equals("2")). I think the later is clearer.
3) How come you only exit on the else?
 
Elim Banda
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks how would I do that (point number 2) im taking a logic course that uses java and have decided i want to persue this further.
 
Elim Banda
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Looking at the main method, I have three comments:

1) You can declare answer and assign the value on the same line
2) The compare to is the same as if (! answer.equals("2")). I think the later is clearer.
3) How come you only exit on the else?


Sorry I meant, how do I do number 1. Im taking a logic course and just on the surface with java
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by making the code legible. You have inconsistent indentation (we have some suggestions here) and you have lines so long that nobody can read the whole of them.
 
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: and you have lines so long that nobody can read the whole of them.

In case you didn't know Meki, you can split your print statements like this:

When your program is executed it will all be glued back together onto a single line.
 
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me add two thoughts to Steffe's very good comment:

1. Please note that Steffe included a blank space at the end of each string, except the last one. It's a common mistake to leave those out.

Steffe Wilson wrote:In case you didn't know Meki, you can split your print statements like this:



2. You can include newlines in a single string.


This prints as

First line
Second line


That can be useful if you want to pass mutliple lines of text around in a single String object, instead of as multiple String objects.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meki Banda wrote:Sorry I meant, how do I do number 1. Im taking a logic course and just on the surface with java

 
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couple few remarks from me.
1. Why these are Double? Always prefer primitives unless you really need to otherwise.
2. "hieght" I think should be spelled "heigth".
3. You could avoid on demand import "import javax.swing.*;" and specify exact class you're going to use.
4. "main" method probably is slightly too long. Ideally could be 1 or 2 lines. Check here.
5. I think comment below or method name is misleading.
6. Comment below is inaccurate too.7. Place [] next to the type, not to the variable. If you ever need to use variable arguments ... and you would write "String args..." you would get compile error.
8. What this number mean in computeSphere method 3.142 ? If it is a Pi maybe use it as a constant with a self explaining variable name.
9. Look at printf/format (<- link) method and maybe apply to line 54.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!