Hello, I really need some help on some Java programming homework that is due tonight. Here is the homework:
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.
I think I understand how to do this all except for the numbers. All three numbers will be written on the same line separated by a space. So, I think that I should use blankLoc. I understand how to use this for the first blank, but what is the code that I should use to get the second and the third numbers. Any help with this program will be greatly appreciated.
Also, if anyone could help clarify this line of the homework:
The first root displayed should be the one where you add the square root of the discriminant.
Not really sure what that means.
Thank You,
Roaster
Basic Java Program Help
Started by roaster, Oct 06 2009 12:13 PM
3 replies to this topic
#1
Posted 06 October 2009 - 12:13 PM
|
|
|
#2
Posted 06 October 2009 - 03:53 PM
The standard formula is: given ax^2 + bx + c, the solutions are:
(-b+sqrt(b^2-4ac))/(2a) and (-b-sqrt(b^2-4ac))/(2a)
(-b+sqrt(b^2-4ac))/(2a) and (-b-sqrt(b^2-4ac))/(2a)
#3
Posted 06 October 2009 - 04:06 PM
Yeah, I got that. Here is my code:
The code compiles fine, no syntax errors. However, I am using BlueJ as my editor. When I go to run the file after it is compiled. The busy sign comes up and nothing happens. Any thoughts?
import static java.lang.System.*;
import java.util.Scanner;
import static java.lang.Math.*;
import java.text.DecimalFormat;
public class Quadratic
{
public static void main(String[] args)
{
//Declare and Initialize Scanner
Scanner scan = new Scanner(in);
//Read in Values
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);
//Display Answers
DecimalFormat fmt1 = new DecimalFormat("0.0000");
out.println(fmt1.format(root1));
out.println(fmt1.format(root2));
}
}
The code compiles fine, no syntax errors. However, I am using BlueJ as my editor. When I go to run the file after it is compiled. The busy sign comes up and nothing happens. Any thoughts?
#4
Posted 06 October 2009 - 11:05 PM
it works in netbeans
but try and put a println before your scans, because it's difficult to see if your program works how it's now
but try and put a println before your scans, because it's difficult to see if your program works how it's now


Sign In
Create Account

Back to top









