-1

I am a newcomer to Java, and in my code attached bellow I have 2 classes, Start.java and ecuație.java. ecuatie.java calculates the square footage of a quadratic ecuation, but for some reason, the constructor doesn't initialize the values properly. Could you shed some light on me as to why does this happen?

package com.ecuatie;

import com.ecuatie.ecuatie;

public class Start {

  public static void main(String[] args) {
      ecuatie exemplu = new ecuatie(1.0d, 0.0d, -4.0d);

      System.out.println(exemplu.delta() + '\n');

      System.out.println(exemplu.X1() + '\n');
      System.out.println(exemplu.X2() + '\n');
  }
}


package com.ecuatie;

import java.lang.Math;

public class ecuatie {
       private double a = 0, b = 0, c = 0;

       ecuatie(double a, double b, double c) {
         this.a = a; this.b = b; this.c = c;
       }

       public double delta() {
         return (b * b) - (4 * a * c);
       }

       public double X1() {
         return (-b + Math.sqrt(delta())) / (2 * a);
       }

       public double X2() {
         return (-b - Math.sqrt(delta())) / (2 * a);
       }
}
  • 4
    Why do you think that? – Tom Mar 10 '19 at 07:00
  • 2
    Read the linked question to get the idea of your issue in this program (it doesn't matter that you use double and the other question uses int) and then use Strings instead of chars. Or don't use `\n` at all, that is redundant. – Tom Mar 10 '19 at 07:04
  • You can use + to concatenation characters to string, however in your case you’d not have a string and so it will add the numerical value. If you had used a “\n“ you would get an error. I would use printf(“%f%n%n“, x) if you need two newlines. – eckes Mar 10 '19 at 19:43

1 Answers1

2

You're getting that because it's adding the ASCII value of the char.

ASCII value for '\n' is 10. so is is like you + exemplu.delta() with 10. also you don't need add enter while you are using println().

so you just need to write your code like this.

  public static void main(String[] args) {
  ecuatie exemplu = new ecuatie(1.0d, 0.0d, -4.0d);

     System.out.println(exemplu.delta() );

     System.out.println(exemplu.X1() );
     System.out.println(exemplu.X2() );
  }