0

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()?

ReactX2
  • 3
  • 2
  • 5
    I'm voting to close this question as off-topic because it belongs to https://codereview.stackexchange.com – Roman Konoval Oct 24 '18 at 09:55
  • This question would be better placed at [codereview](https://codereview.stackexchange.com/) (as the question itself states). I'd like to flag this question, but I don't get the option *codereview* after clicking *This question belongs on another site in the Stack Exchange network*... – deHaar Oct 24 '18 at 09:56
  • I edited my post because I need a help in my method `isEnteringValues()` – ReactX2 Oct 24 '18 at 10:08
  • [How to read integer value from the standard input in Java](https://stackoverflow.com/questions/2506077/how-to-read-integer-value-from-the-standard-input-in-java) – meowgoesthedog Oct 24 '18 at 10:24

1 Answers1

0

Make independent class QuadraticEquation and do not take input operation in the class. code like below may help you.

    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);
      }
    }
Pandey Amit
  • 557
  • 5
  • 17