我需要代码审查和Java OOP中的一些提示。我正在编写一个将计算二次方程的程序。我想编写一个干净的代码,以便...
这是我的Variable类。我在这里存储变量并进行计算:
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);
}
}
}
}
这是我的Dialog类:我为用户显示消息,在这里我需要帮助。我想在用户输入值a时显示消息a。如果b是b,如果c是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);
}
}
这是我的主要课程:
public class Calculation {
public static void main(String[] args) {
Variable variable = new Variable();
variable.input();
variable.calculate();
}
}
我正在学习编写面向对象的程序,并要求进行代码审查。方法和类的名称写得好吗?如何执行方法isEnteringValues()
?
答案 0 :(得分:0)
使用独立的类QuadraticEquation,并且不对该类进行输入操作。 如下所示的代码可能会对您有所帮助。
import java.util.Scanner;
class QuadraticEquation {
private double a;
private double b;
private double c;
public QuadraticEquation(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public double calculateDiscriminant() {
return (b*b - 4 * a * c);
}
}
public class Calculation{
public static void main(String []args) {
Scanner scan = new Scanner(System.in);
double a = Dialog.isEnteredValues(scan, "a");
double b = Dialog.isEnteredValues(scan, "b");
double c = Dialog.isEnteredValues(scan, "c");
QuadraticEquation eq = new QuadraticEquation(a, b, c);
double disc = eq.calculateDiscriminant();
if(disc < 0) {
Dialog.isNoSolution();
}else if(disc > 0) {
double x1 = (-b - Math.sqrt(disc)) / 2 * a;
double x2 = (-b + Math.sqrt(disc)) / 2 * a;
Dialog.isTwoSolution(x1, x2);
}else {
double x0 = -b / (2 * a);
Dialog.isOneSolution(x0);
}
}
}
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 double isEnteredValues(Scanner scan, String str)
{
System.out.println("Enter value " + str);
return scan.nextDouble();
}
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);
}
}