patternjavaMinorCanonical
Quadratic Expression Calculator 2
Viewed 0 times
expressionquadraticcalculator
Problem
After taking some great advice on this question, I'm hoping for some more feedback on my quadratic expression calculator.
This is my code as stands:
```
public class Quadratic_Equations {
private final static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("Please enter a, b and c or enter \"exit\" at any time to exit");
double a = promptUserUntilValidInput("Please enter a:");
double b = promptUserUntilValidInput("Please enter b:");
double c = promptUserUntilValidInput("Please enter c:");
System.out.println(formatOutput(calculateQuadraticFormula(a, b, c)));
System.out.println();
}
}
private static String formatOutput(Set resultsFromEquation) {
if (resultsFromEquation == null)
return "These numbers do not compute - they produce an illegal result.";
else {
return resultsFromEquation.toString();
}
}
private static double promptUserUntilValidInput(String prompt){
if (prompt.equals("exit")) {
sc.close();
System.exit(0);
}
boolean valid = false;
Double value = new Double(0.0);
while(!valid){
System.out.println(prompt);
value = tryParse(sc.nextLine());
if (value != null)
valid = true;
}
return value;
}
private static Double tryParse(String text){
try{
return Double.parseDouble(text);
}
catch (NumberFormatException e){
return null;
}
}
public static Set calculateQuadraticFormula(double a, double b, double c) {
Set results = new HashSet();
double temp = (b b) - (4 a * c);
if (temp 0) {
results.add( (-b + Math.sqrt(temp) ) / (2 * a) );
results.add( (-b - Math.sqrt(temp) ) / (2 * a) );
}
This is my code as stands:
```
public class Quadratic_Equations {
private final static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("Please enter a, b and c or enter \"exit\" at any time to exit");
double a = promptUserUntilValidInput("Please enter a:");
double b = promptUserUntilValidInput("Please enter b:");
double c = promptUserUntilValidInput("Please enter c:");
System.out.println(formatOutput(calculateQuadraticFormula(a, b, c)));
System.out.println();
}
}
private static String formatOutput(Set resultsFromEquation) {
if (resultsFromEquation == null)
return "These numbers do not compute - they produce an illegal result.";
else {
return resultsFromEquation.toString();
}
}
private static double promptUserUntilValidInput(String prompt){
if (prompt.equals("exit")) {
sc.close();
System.exit(0);
}
boolean valid = false;
Double value = new Double(0.0);
while(!valid){
System.out.println(prompt);
value = tryParse(sc.nextLine());
if (value != null)
valid = true;
}
return value;
}
private static Double tryParse(String text){
try{
return Double.parseDouble(text);
}
catch (NumberFormatException e){
return null;
}
}
public static Set calculateQuadraticFormula(double a, double b, double c) {
Set results = new HashSet();
double temp = (b b) - (4 a * c);
if (temp 0) {
results.add( (-b + Math.sqrt(temp) ) / (2 * a) );
results.add( (-b - Math.sqrt(temp) ) / (2 * a) );
}
Solution
Bugs
-
-
The "exit" feature doesn't work. The prompt would never be
Design issues
Style issues
-
The
Also, since there is a
-
calculateQuadraticFormula() calculates wrong results for double roots. Watch your operator associativity!if( temp == 0 ) {
results.add(-b / 2 * a);}-
The "exit" feature doesn't work. The prompt would never be
"exit", would it?private static double promptUserUntilValidInput(String prompt){
if (prompt.equals("exit")) {
sc.close();
System.exit(0);
}Design issues
- Class names should be nouns, and the name should reflect their purpose. I recommend
QuadraticSolveras the class name.
- The
calculateQuadraticFormula()function could then be simply namedsolve().
- The class should not hold a
Scanneras a member variable, as that would violate the Single Responsibility Principle. It can be a local variable inmain()that gets passed to the prompting function. (The scanner should be created using a try-with-resources block.)
- Returning
nullfrom the solver is annoying for the caller to handle. I suggest returning an empty set if there are no real roots.
- I suggest returning two copies of the result if it is a double root, but that is a matter for debate.
Style issues
- You assign the expression
(b b) - (4 a * c)to a variable namedtemp. Why not use a descriptive namediscriminant?
-
The
promptUserUntilValidInput() function could be better if you removed its tryParse() helper, I think. (Furthermore, promptUser… is redundant — who else are you going to prompt?)Also, since there is a
System.exit() hidden inside, it needs to be documented. (Actually, @rolfl's suggestion to throw an exception is better.)/**
* Prompts the user to enter a number, and retries until the input is a
* valid double. Calls System.exit(0) if the input is "exit", or if EOF
* is encountered.
*/
private static double promptUntilValidInput(String prompt, Scanner sc) {
do {
try {
System.out.print(prompt);
String input = sc.nextLine();
if ("exit".equals(input)) {
System.exit(0);
}
return Double.parseDouble(input);
} catch (NoSuchElementException eof) {
System.out.println();
System.exit(0);
} catch (NumberFormatException retryOnBadInput) {
}
} while (true);
}Code Snippets
if( temp == 0 ) {
results.add(-b / 2 * a);}private static double promptUserUntilValidInput(String prompt){
if (prompt.equals("exit")) {
sc.close();
System.exit(0);
}/**
* Prompts the user to enter a number, and retries until the input is a
* valid double. Calls System.exit(0) if the input is "exit", or if EOF
* is encountered.
*/
private static double promptUntilValidInput(String prompt, Scanner sc) {
do {
try {
System.out.print(prompt);
String input = sc.nextLine();
if ("exit".equals(input)) {
System.exit(0);
}
return Double.parseDouble(input);
} catch (NoSuchElementException eof) {
System.out.println();
System.exit(0);
} catch (NumberFormatException retryOnBadInput) {
}
} while (true);
}Context
StackExchange Code Review Q#46322, answer score: 6
Revisions (0)
No revisions yet.