This is a really neat program. For what it does, it does a good job.
Really, there'sthere’s only one point of criticism, and a few suggestions. The criticism is designed to get you in to the right habits, it'sit’s not a major bug.... ...
Using try-with-resourcestry‐with‐resources
Java, forFor years, Java has had a problem with people being lazy about closing resources.... ... you open a file, read it'sits contents, and move on.... ... leaving an open file handle and other resources until the Garbage Collectorgarbage collector cleans up the mess. These 'forgotten'‘forgotten’ resources can sometimes lead to unexpected bugs and deadlocks.
In Java7, the concept 'try-with-resources' was added concept ‘try‐with‐resources’ was added to ensure that resources are guaranteed to be closed nicely whether the code succeeds or not. It actually allows the programmer to be even lazier than before, and get a an even better result.
In your case, the Scanner instance is a resource that should be closed.... ...
public static void main(String[] args) {
try (Scanner scannerObject = new Scanner(System.in)) {
double n1, n2;
String operation;
...........
}
}
OK, that'sthat’s the right way to use the Scanner.
Further Improvements
As your program expands, you will find that there are a few things that become 'uncomfortable'....‘uncomfortable’ ... the number of operators will increase, and the complexity of the calculations will increase as well... ... eventually you will want to enter expressions like 100 * ( 17 / 20 )
to get the percent score if you got 17 out of 20 in a test.... ... etc.
What you have at the moment is great for it'sits purpose, but I encourage you to try to find ways to expand on the idea.
Research you may want to do is in to things like 'Reverse Polish Notation (RPN)' Reverse Polish Notation (RPN) which is an easier-to-parseeasier‐to‐parse format for writing mathematical expressions. Then you can look at things that help you convert Infixinfix to Postfixpostfix (RPN) notation... ...
In order to do these more expansiveexpensive operations you will need to find ways to turn operators in to classes (probably an Enum probably an Enum), and have classes that allow you to encapsulate an expression (unary unary or binary binary expressions).
Good job on this, otherwise.
This is a really neat program. For what it does, it does a good job.
Really, there's only one criticism, and a few suggestions. The criticism is designed to get you in to the right habits, it's not a major bug....
Using try-with-resources
Java, for years, has had a problem with people being lazy about closing resources.... you open a file, read it's contents, and move on.... leaving an open file handle and other resources until the Garbage Collector cleans up the mess. These 'forgotten' resources can sometimes lead to unexpected bugs and deadlocks.
In Java7, the concept 'try-with-resources' was added to ensure that resources are guaranteed to be closed nicely whether the code succeeds or not. It actually allows the programmer to be even lazier than before, and get a an even better result.
In your case, the Scanner instance is a resource that should be closed....
public static void main(String[] args) {
try (Scanner scannerObject = new Scanner(System.in)) {
double n1, n2;
String operation;
.....
}
}
OK, that's the right way to use the Scanner.
Further Improvements
As your program expands, you will find that there are a few things that become 'uncomfortable'.... the number of operators will increase, and the complexity of the calculations will increase as well... eventually you will want to enter expressions like 100 * ( 17 / 20 )
to get the percent score if you got 17 out of 20 in a test.... etc.
What you have at the moment is great for it's purpose, but I encourage you to try to find ways to expand on the idea.
Research you may want to do is in to things like 'Reverse Polish Notation (RPN)' which is an easier-to-parse format for writing mathematical expressions. Then you can look at things that help you convert Infix to Postfix (RPN) notation...
In order to do these more expansive operations you will need to find ways to turn operators in to classes (probably an Enum), and have classes that allow you to encapsulate an expression (unary or binary expressions).
Good job on this, otherwise.
This is a really neat program. For what it does, it does a good job.
Really, there’s only one point of criticism, and a few suggestions. The criticism is designed to get you in to the right habits, it’s not a major bug ...
Using try‐with‐resources
For years Java has had a problem with people being lazy about closing resources ... you open a file, read its contents, and move on ... leaving an open file handle and other resources until the garbage collector cleans up the mess. These ‘forgotten’ resources can sometimes lead to unexpected bugs and deadlocks.
In Java7, the concept ‘try‐with‐resources’ was added to ensure that resources are guaranteed to be closed nicely whether the code succeeds or not. It actually allows the programmer to be even lazier than before, and get an even better result.
In your case, the Scanner instance is a resource that should be closed ...
public static void main(String[] args) {
try (Scanner scannerObject = new Scanner(System.in)) {
double n1, n2;
String operation;
......
}
}
OK, that’s the right way to use the Scanner.
Further Improvements
As your program expands, you will find that there are a few things that become ‘uncomfortable’ ... the number of operators will increase, and the complexity of the calculations will increase as well ... eventually you will want to enter expressions like 100 * ( 17 / 20 )
to get the percent score if you got 17 out of 20 in a test ... etc.
What you have at the moment is great for its purpose, but I encourage you to try to find ways to expand on the idea.
Research you may want to do is in to things like Reverse Polish Notation (RPN) which is an easier‐to‐parse format for writing mathematical expressions. Then you can look at things that help you convert infix to postfix (RPN) notation ...
In order to do these more expensive operations you will need to find ways to turn operators in to classes (probably an Enum), and have classes that allow you to encapsulate an expression (unary or binary expressions).
Good job on this, otherwise.
This is a really neat program. For what it does, it does a good job.
Really, there's only one criticism, and a few suggestions. The criticism is designed to get you in to the right habits, it's not a major bug....
Using try-with-resources
Java, for years, has had a problem with people being lazy about closing resources.... you open a file, read it's contents, and move on.... leaving an open file handle and other resources until the Garbage Collector cleans up the mess. These 'forgotten' resources can sometimes lead to unexpected bugs and deadlocks.
In Java7, the concept 'try-with-resources' was added to ensure that resources are guaranteed to be closed nicely whether the code succeeds or not. It actually allows the programmer to be even lazier than before, and get a an even better result.
In your case, the Scanner instance is a resource that should be closed....
public static void main(String[] args) {
try (Scanner scannerObject = new Scanner(System.in)) {
double n1, n2;
String operation;
.....
}
}
OK, that's the right way to use the Scanner.
Further Improvements
As your program expands, you will find that there are a few things that become 'uncomfortable'.... the number of operators will increase, and the complexity of the calculations will increase as well... eventually you will want to enter expressions like 100 * ( 17 / 20 )
to get the percent score if you got 17 out of 20 in a test.... etc.
What you have at the moment is great for it's purpose, but I encourage you to try to find ways to expand on the idea.
Research you may want to do is in to things like 'Reverse Polish Notation (RPN)' which is an easier-to-parse format for writing mathematical expressions. Then you can look at things that help you convert Infix to Postfix (RPN) notation...
In order to do these more expansive operations you will need to find ways to turn operators in to classes (probably an Enum), and have classes that allow you to encapsulate an expression (unary or binary expressions).
Good job on this, otherwise.