Thread: Help with Java

  1. #1

    Help with Java

    Hey!
    I'm trying to create a program that calculates the vertex coordinates of the maximi/minimivalue in a quadratic equation with the "pq-formula" and I've run into a problem.

    Here comes my code!

    //Det här programmet ska räkna ut koordinaterna för vertex i en andragradsekvation
    //(där det är givet att formeln står i "pq-kompatibelt läge")
    import java.util.*;
    import static java.lang.Math.*;
    public class Vertex

    { public static void main(String [] args)
    {
    Scanner Skanna = new Scanner(System.in);
    double p = Skanna.nextDouble();
    double q = Skanna.nextDouble();

    // x1=(-p/2)+sqrt((Math.pow((p/2),2) - q))
    System.out.println("x1 = "+ (-p/2)+sqrt((Math.pow((p/2),2))-q));
    System.out.println("x2 = "+ (-p/2)-sqrt((Math.pow((p/2),2))-q));
    }

    }

    -------------------------------------------------------------------------------------------------------------
    Error message:

    D:\Dropbox\Programmering\Vertex.java:15: error: bad operand types for binary operator '-'
    System.out.println("x2 = "+ (-p/2)-sqrt((Math.pow((p/2),2))-q));
    ^
    first type: String
    second type: double
    1 error

    Tool completed with exit code 1
    --------------------------------------------------------------------------------------------------------------

    So I understand that it somehow thinks that I'm trying to write a text, but why can't I subtract the sqrt and how do I correct it?

  2. #2

    .

    You should try harder before posting this to a list. Anyways, break the println into two:

    double x1 = (-p/2)+sqrt((Math.pow((p/2),2))-q);
    System.out.println("x1 = " + x1);

  3. #3
    The error is being thrown because the program is attempting to concatenate the separate segments together, via '+'. This actually means that both of your print statements will be incorrect. In the case of the error the '-' is viewed as an attempt to concatenate the String, but is not a valid operator. To remedy this, you could surround the equation with an extra set of parenthesis. However, I would recommend doing the calculation separately and storing it as a variable (as recommended above). This will also make the code more readable.

  4. #4
    Kudos for the tips!

    So, I've encountered a new problem which I've been stuck at for an hour, going back and forth. The problem is that I keep getting the wrong Y-coordinate for the maximi/minimivalue, everything else works like a charm.

    ------------------------------------------------------------------------------
    //Det här programmet ska räkna ut koordinaterna för vertex i en andragradsekvation

    import java.util.*;
    import static java.lang.Math.*;
    public class Vertex

    { public static void main(String [] args)
    {
    Scanner Skanna = new Scanner(System.in);
    double a = Skanna.nextDouble();
    double EntryP = Skanna.nextDouble();
    double EntryQ = Skanna.nextDouble();
    System.out.print (a+"x^2 + ");
    System.out.print (EntryP+"x = ");
    System.out.println (EntryQ);
    double p = EntryP/a;
    double q = (EntryQ/a);


    // Räknar ut nollpunkterna
    double sqrt= sqrt((Math.pow((p/2),2))-q);
    double X1 = ((-p/2)+sqrt);
    double X2 = ((-p/2)-sqrt);
    System.out.println("X1 = "+ X1);
    System.out.println("X2 = "+ X2);


    //räknar ut symetrilinjens placering samt koordinaten för maximi/minimipunkten
    double Xvertex = ((X1+X2)/2);
    System.out.println("Symetrilinjens placering = "+ Xvertex);

    double Yvertex = (a * Xvertex) + (EntryP * Xvertex) ;
    if( (a>0 ) )
    {
    System.out.println( "Maximi-koordinat = (" + Xvertex + ";" + Yvertex + ")");
    }
    else
    {
    System.out.println( "Minimi-koordinat = (" + Xvertex + ";-" + Yvertex + ")");
    }


    }

    }

  5. #5
    I'm not sure why you used this expression for Yvertex. If the expression for the parabola is y = a*x^2+b*x+c then Yvertex = a*Xvertex^2+b*Xvertex + c.

    By the way, if you know calculus it can be shown that Xvertex = -b/2a. No need to mess around with the quadratic formula.

    Also, an hour is not long to be working on a problem.

    Quote Originally Posted by mittacc View Post
    Kudos for the tips!


    double Yvertex = (a * Xvertex) + (EntryP * Xvertex) ;
    if( (a>0 ) )
    {
    System.out.println( "Maximi-koordinat = (" + Xvertex + ";" + Yvertex + ")");
    }
    else
    {
    System.out.println( "Minimi-koordinat = (" + Xvertex + ";-" + Yvertex + ")");
    }


    }

    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •