# Quadratic Program Problems

Page 1 of 1

## 8 Replies - 1774 Views - Last Post: 06 October 2009 - 05:17 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=130318&amp;s=d38221517e396e9052802c807469b0ab&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 roaster

• New D.I.C Head

Reputation: 0
• Posts: 26
• Joined: 06-October 09

# Quadratic Program Problems

Posted 06 October 2009 - 04:09 PM

Hello everyone, I really need some help. I have homework due tonight and I am really stuck. I keep getting compile errors and I do not understand what I am doing wrong. I am very new to Java so I probably have multiple errors. Anyway here is my homework description:

Your task in this homework is to compute the roots of a quadratic, using the quadratic formula. Your program should read in three double values, the coefficients of the x-squared term, the linear term and the constant term respectively. These values will be separated by blanks in the test input. It should compute the two real roots and display them, one to a line. The first root displayed should be the one where you add the square root of the discriminant. You may assume that the quadratics tested will all have real roots. These roots should be displayed to exactly 4 places past the decimal point, with a leading zero shown before the decimal point if it is between -1.0 and 1.0. Your program should display a single blank line as a prompt to make it possible to type in numbers while you are testing the program using BlueJ and to test it on educate when you turn it in.

If the values input, in bold, are:

1.0 4.0 3.0

The output would be:
-1.0000
-3.0000

Similarly, if the values input, in are:

1.0 2.0 1.0

The output would be:
-1.0000
-1.0000

```import static java.lang.System.*;
import java.util.Scanner;
import static java.lang.Math.*;

{
public static void main(String[] args)
{
//Declare and Initialize Scanner
Scanner scan = new Scanner(in);

double values = scan.nextDouble();

//Convert double values to String str1
String str1 = values.toString();

//Separate Values
String  blankLoc = str1.indexOf(' ');
double a = values.substring(0, blankLoc);
double b = values.substring(blankLoc, blankLoc);
double c = values.substring(blankLoc + 1);

//Discriminant: (b*b) - (4*a*c)
double disc = Math.sqrt((b*b)-(4*a*c));

//Find the Roots
double root1 = (-b + disc)/(2*a);
double root2 = (-b - disc)/(2*a);

DecimalFormat fmt1 = new DecimalFormat("0.0000");
out.println(fmt1.format(root1));
out.println(fmt1.format(root2));
}
}

```

The first error that I am encountering is that "double cannot be dereferenced"

Another issue that I have, the three numbers are entered in by the user on one line. Then I need to separate these numbers into 3 separate numbers so that they can be used in the quadratic formula. My problem is that I am not sure how to separate the numbers. I know that I need to use blankLoc but do not really know how to use it correctly(i don't think). If you look in my code under the //Separate Values comment, you will see what I am trying to do. Any suggestions?

Thank You,
Roaster

Is This A Good Question/Topic? 0

## Replies To: Quadratic Program Problems

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11395
• Posts: 42,937
• Joined: 27-December 08

## Re: Quadratic Program Problems

Posted 06 October 2009 - 04:30 PM

Try this:

```Scanner scan = new Scanner(System.in);
double a = 0, b = 0, c = 0;

System.out.println("Enter A, B and C separated by spaces. Hit enter when you are done");
a = scan.nextDouble();
b = scan.nextDouble();
c = scan.nextDouble();

double discr = getDiscriminant(a,b,c);
if(discr == -1.0) System.out.println("No real roots");
else if(discr == 0) System.out.println("One root at 0");
else{
System.out.println("Root 1: " + ((-1*b + discr)/(2*a)));
System.out.println("Root 1: " + ((-1*b - discr)/(2*a)));
}

public double getDiscriminant(double a, double b, double c){
if((b*b) < (4 * a * c)) return -1;
else if((b*b) == (4*a*c)) return 0;
else return Math.sqrt(b*b-(4*a*c));
}

```

### #3 chili5

• D.I.C Lover

Reputation: 20
• Posts: 1,144
• Joined: 28-December 07

## Re: Quadratic Program Problems

Posted 06 October 2009 - 04:33 PM

Here you are only reading one double:

```double values = scan.nextDouble();

```

This would only read the A value. The rest looks right. You just need to read in the 3 doubles. So something like this:

```a = scan.nextDouble();
b  = scan.nextDouble();
c = scan.nextDouble();

```

You don't need that weird string code.

### #4 roaster

• New D.I.C Head

Reputation: 0
• Posts: 26
• Joined: 06-October 09

## Re: Quadratic Program Problems

Posted 06 October 2009 - 04:43 PM

macosxnerd101, on 6 Oct, 2009 - 03:30 PM, said:

Try this:

```Scanner scan = new Scanner(System.in);
double a = 0, b = 0, c = 0;

System.out.println("Enter A, B and C separated by spaces. Hit enter when you are done");
a = scan.nextDouble();
b = scan.nextDouble();
c = scan.nextDouble();

double discr = getDiscriminant(a,b,c);
if(discr == -1.0) System.out.println("No real roots");
else if(discr == 0) System.out.println("One root at 0");
else{
System.out.println("Root 1: " + ((-1*b + discr)/(2*a)));
System.out.println("Root 1: " + ((-1*b - discr)/(2*a)));
}

public double getDiscriminant(double a, double b, double c){
if((b*b) < (4 * a * c)) return -1;
else if((b*b) == (4*a*c)) return 0;
else return Math.sqrt(b*b-(4*a*c));
}

```

Thanks for replying, this has been driving me crazy. Your help is a lifesaver. I have copied your code in but I still get an error, I probably did something wrong with it though. The error that I get is "illegal start of expression" at this line:
public double getDiscriminant(double a, double b, double c){

Here is the code:
```public class Trial1
{

public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);
double a = 0, b = 0, c = 0;

System.out.println("Enter A, B and C separated by spaces. Hit enter when you are done");
a = scan.nextDouble();
b = scan.nextDouble();
c = scan.nextDouble();

double discr = getDiscriminant(a,b,c);
if(discr == -1.0) System.out.println("No real roots");
else if(discr == 0) System.out.println("One root at 0");
else{
System.out.println("Root 1: " + ((-1*b + discr)/(2*a)));
System.out.println("Root 1: " + ((-1*b - discr)/(2*a)));
}

public double getDiscriminant(double a, double b, double c){
if((b*b) < (4 * a * c)) return -1;
else if((b*b) == (4*a*c)) return 0;
else return Math.sqrt(b*b-(4*a*c));
}

}

```

Any thoughts?

Thanks,
Roaster

### #5 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11395
• Posts: 42,937
• Joined: 27-December 08

## Re: Quadratic Program Problems

Posted 06 October 2009 - 04:50 PM

Move the getDiscriminant() method outside the main method. Remember, you cannot define one method inside another.

### #6 roaster

• New D.I.C Head

Reputation: 0
• Posts: 26
• Joined: 06-October 09

## Re: Quadratic Program Problems

Posted 06 October 2009 - 05:03 PM

macosxnerd101, on 6 Oct, 2009 - 03:50 PM, said:

Move the getDiscriminant() method outside the main method. Remember, you cannot define one method inside another.

Ok, I think I moved it outside of the method. My new error is "non-static method getDiscriminant(double, double, double) cannot be referenced from a static context"
at the line:
double discr = getDiscriminate(a,b,c);

Here is my new code:
```import static java.lang.System.*;
import java.util.Scanner;
import static java.lang.Math.*;
import java.text.DecimalFormat;

public class Trial1
{

public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);
double a = 0, b = 0, c = 0;

System.out.println("Enter A, B and C separated by spaces. Hit enter when you are done");
a = scan.nextDouble();
b = scan.nextDouble();
c = scan.nextDouble();

double discr = getDiscriminant(a,b,c);
if(discr == -1.0) System.out.println("No real roots");
else if(discr == 0) System.out.println("One root at 0");
else{
System.out.println("Root 1: " + ((-1*b + discr)/(2*a)));
System.out.println("Root 1: " + ((-1*b - discr)/(2*a)));
}
}

public double getDiscriminant(double a, double b, double c)
{
if((b*b) < (4 * a * c)) return -1;
else if((b*b) == (4*a*c)) return 0;
else return Math.sqrt(b*b-(4*a*c));
}
}

```

### #7 roaster

• New D.I.C Head

Reputation: 0
• Posts: 26
• Joined: 06-October 09

## Re: Quadratic Program Problems

Posted 06 October 2009 - 05:10 PM

chili5, on 6 Oct, 2009 - 03:33 PM, said:

Here you are only reading one double:

```double values = scan.nextDouble();

```

This would only read the A value. The rest looks right. You just need to read in the 3 doubles. So something like this:

```a = scan.nextDouble();
b  = scan.nextDouble();
c = scan.nextDouble();

```

You don't need that weird string code.

Thanks for the help, here is my new code:
```import static java.lang.System.*;
import java.util.Scanner;
import static java.lang.Math.*;
import java.text.DecimalFormat;

{
public static void main(String[] args)
{
//Declare and Initialize Scanner
Scanner scan = new Scanner(in);

double a = scan.nextDouble();
double b  = scan.nextDouble();
double c = scan.nextDouble();

//Separate Values
//String  blankLoc = str1.indexOf(' ');
//double a = values.substring(0, blankLoc);
//double b = values.substring(blankLoc, blankLoc);
//double c = values.substring(blankLoc + 1);

//Discriminant: (b*b) - (4*a*c)
double disc = Math.sqrt((b*b)-(4*a*c));

//Find the Roots
double root1 = (-b + disc)/(2*a);
double root2 = (-b - disc)/(2*a);

DecimalFormat fmt1 = new DecimalFormat("0.0000");
out.println(fmt1.format(root1));
out.println(fmt1.format(root2));
}
}

```

It compiles with no syntax errors. However, I am using BlueJ as my editor. When I go to run the program, the BlueJ busy signal comes up and stays up the program never opens. Any thoughts?

### #8 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11395
• Posts: 42,937
• Joined: 27-December 08

## Re: Quadratic Program Problems

Posted 06 October 2009 - 05:11 PM

Add the word static between public and double in the header of getDiscriminant(). Basically, any method called from the main method has to be static because the main method is instantiated on runtime by the JVM.

### #9 roaster

• New D.I.C Head

Reputation: 0
• Posts: 26
• Joined: 06-October 09

## Re: Quadratic Program Problems

Posted 06 October 2009 - 05:17 PM

macosxnerd101, on 6 Oct, 2009 - 04:11 PM, said:

Add the word static between public and double in the header of getDiscriminant(). Basically, any method called from the main method has to be static because the main method is instantiated on runtime by the JVM.

You are awesome, thank you so much. It all works great.

Roaster