RE: My second Java program 11-16-2013, 04:27 PM
#11
Since all of the above have encouraged you lot and suggested you things. Then there are few things that I would like to point out as well :-
1. Make it menu driven by using while or do-while loop just as @The Arcanist said, in which ever you are comfortable.
2. Since you have made this number evaluator for only three functions and so I would like to see a few more like, div, mod etc.
3. When you're checking the instructions this way :-
"add".equals(value);
what if I give the instruction as "aDd", it won't execute and so use, "add".equalsIgnoreCase(value);
4. If you are going to extend this program to more functionality then don't use a large if else construct instead use switch case for the purpose but take into consideration that you must have Java 7.
5. It is a good practice to declare instructions separately and then use them for further purposes. You have done something like this :-
"add".equals(value);
But I would have done something like below :-
You can even use enum.
6. whenever you are want to execute any instruction you're taking input from the user within the if block. Its not a good programming practice. Instead use a method for the purpose.
Something like :-
public void getData(){
//write code to get input from the user.
}
7. Its a good programming practice to name your variables in such a way that they are easily understandable. You have used variable names like :- c, value2, value3, etc. The class name is Application. What does this tell the user about your program and so its confusing for the user to understand what does your code does and so name it in such a way that it can be understood easily and so name it as closely as possible in accordance to your code.
Here read these :-
http://docs.oracle.com/javase/tutorial/j...ables.html
http://en.wikipedia.org/wiki/Naming_conv...ogramming)
The above suggestions will make your code more readable and would look better and organized.
Thank you,
Sincerely,
Psycho_Coder
1. Make it menu driven by using while or do-while loop just as @The Arcanist said, in which ever you are comfortable.
Code:
private static boolean CONTINUE = true;
while(CONTINUE){
//write your stuff here
}
2. Since you have made this number evaluator for only three functions and so I would like to see a few more like, div, mod etc.
3. When you're checking the instructions this way :-
"add".equals(value);
what if I give the instruction as "aDd", it won't execute and so use, "add".equalsIgnoreCase(value);
4. If you are going to extend this program to more functionality then don't use a large if else construct instead use switch case for the purpose but take into consideration that you must have Java 7.
5. It is a good practice to declare instructions separately and then use them for further purposes. You have done something like this :-
"add".equals(value);
But I would have done something like below :-
Code:
private final String ADD = "add";
private final String SUB = "sub";
...
...
private String USER_INPUT = null;
...
...
USER_INPUT = scanner.nextLine();
...
...
if (USER_INPUT.equals(ADD)){
// Do addition;
}
...
You can even use enum.
6. whenever you are want to execute any instruction you're taking input from the user within the if block. Its not a good programming practice. Instead use a method for the purpose.
Something like :-
public void getData(){
//write code to get input from the user.
}
7. Its a good programming practice to name your variables in such a way that they are easily understandable. You have used variable names like :- c, value2, value3, etc. The class name is Application. What does this tell the user about your program and so its confusing for the user to understand what does your code does and so name it in such a way that it can be understood easily and so name it as closely as possible in accordance to your code.
Here read these :-
http://docs.oracle.com/javase/tutorial/j...ables.html
http://en.wikipedia.org/wiki/Naming_conv...ogramming)
The above suggestions will make your code more readable and would look better and organized.
Thank you,
Sincerely,
Psycho_Coder
![[Image: OilyCostlyEwe.gif]](http://fat.gfycat.com/OilyCostlyEwe.gif)