I need a code review and a little hint in OOP of Java. I'm writing a program that will calculate a quadratic equation. I would like to write a clean code so...
This is my Variable class. Here I store variables and make calculations:
import java.util.Scanner;
class Variable {
private double a;
private double b;
private double c;
void input()
{
// Dialog.isEnteringValues();
Scanner aValue = new Scanner(System.in);
a = aValue.nextDouble();
Scanner bValue = new Scanner(System.in);
b = bValue.nextDouble();
Scanner cValue = new Scanner(System.in);
c = cValue.nextDouble();
Dialog.isEnteredValues(a, b, c);
}
void calculate()
{
double delta = b * b - 4 * a * c;
System.out.println("Delta: " + delta);
if (delta < 0)
Dialog.isNoSolution();
else {
if (delta == 0) {
double x0 = -b / (2 * a);
Dialog.isOneSolution(x0);
}
if (delta > 0) {
double x1 = (-b - Math.sqrt(delta)) / 2 * a;
double x2 = (-b + Math.sqrt(delta)) / 2 * a;
Dialog.isTwoSolution(x1, x2);
}
}
}
}
This is my Dialog class: I display messages for the user and here I need help. I would like when the user enters the value a, display the message a. If b is b, if c is c.
public class Dialog {
// public void isEnteringValues()
// {
// switch (???) {
// case a:
// System.out.print("Input A: ");
// case b:
// System.out.print("Input B: ");
// case c:
// System.out.print("Input C: ");
// }
// }
static void isEnteredValues(double a, double b, double c)
{
System.out.println("A: " + a);
System.out.println("B: " + b);
System.out.println("C: " + c);
}
static void isNoSolution()
{
System.out.println("No solution.");
}
static void isOneSolution(double x0)
{
System.out.println("x0: " + x0);
}
static void isTwoSolution(double x1, double x2)
{
System.out.println("x1: " + x1);
System.out.println("x2: " + x2);
}
}
This is my main class:
public class Calculation {
public static void main(String[] args) {
Variable variable = new Variable();
variable.input();
variable.calculate();
}
}
I am learning to program object-oriented and I am asking for a code review. Are the names of methods and classes written well? How to perform the method isEnteringValues()
?