March 23rd, 2013, 11:29 AM

I'm stuck
Hello. I am self studying from the textbook "The Art and Science of Java"
I am stuck (although I must admit I have not spent a great deal of time) with the following exercise from the text as follows:
In highschool algebra, you learned that the standard quadratic equation
ax2 +bx+c=0
has two solutions given by the formula
x=–b ± √⎯⎯b⎯⎯–⎯4⎯ac/2a
The first solution is obtained by using + in place of ±; the second is obtained by using – in place of ±.
Write a Java program that accepts values for a, b, and c, and then calculates the two solutions. If the quantity under the square root sign is negative, the equation has no real solutions, and your program should display a message to that effect. You may assume that the value for a is nonzero.
This is what I have came up with so far. I know there are likely many things wrong with this code. I am having problems with warning that my methods do not return the value of type double. Also, I am not sure if the syntax would be correct for the return calls as I have been struggling there as well.
Any suggestions at all (even quit your studies...jk) would be most gracious. Thanks.
Code:
import acm.program.*;
import java.math.*;
public class Quadratic extends ConsoleProgram {
public void run() {
println("Enter coefficients for the quadratic equation.");
double a = readDouble("a: ");
double b = readDouble("b: ");
double c = readDouble("c: ");
double positive = solution1(a, b, c); // if coefficient b is positive
double negative = solution2(a, b, c); // if coefficient b is negative
println(negative);
println(positive);
}
private double solution1(double a, double b, double c) {
double result1 = b + ((Math.sqrt((b * b)  (4 * a * c))) / 2 * a);
if (result1 < 0) {
println("The result cannot be calculated"); }
else {
return result1;
}
}
private double solution2(double a, double b, double c) {
double result2 = b  ((Math.sqrt((b * b)  (4 * a * c))) / 2 * a);
if (result2 < 0) {
println("The result cannot be calculated"); }
else {
return result2;
}
}
}
March 23rd, 2013, 02:04 PM

Partial Solution
Hey,
I am new to the forum!
If you are new to java I highly recommend this:
http://thenewboston.org/list.php?cat=31
There are still some problems within this however you should be able to get it working without to much bother, this is all a learning curve so pointless me finishing this for you ! If you need help feel free to pm me I plan to use this forum more frequently as it seems pretty good!
Code:
//author: StarwarsFTW
//http://forums.devshed.com/javahelp9/imstuck942273.html
import java.util.Scanner;
public class Quadratic
{
public static void main( String args[] )
{
Scanner inputDouble = new Scanner(System.in); //Scanner created
System.out.println("***********************************************");
System.out.println("*Enter coefficients for the quadratic equation*");
System.out.println("***********************************************");
//Input being captured
System.out.print("Enter the value for a :");
double a = inputDouble.nextDouble();
System.out.print("Enter the value for b :");
double b = inputDouble.nextDouble();
System.out.print("Enter the value for c :");
double c = inputDouble.nextDouble();
//retriving the methods
double positive = solution1(a,b,c);
double negative = solution2(a,b,c);
//outputing the methods
System.out.println("The answer for positive is " + positive);
System.out.println("The answer for negative is " + negative);
}
public static double solution1(double a, double b, double c)
{
double result1 = a + b + c;
//double equat = (b * b)  (4 * a * c);
//double result1 =  b + (Math.sqrt(equat)) / (2 * a);
return result1;
}
private static double solution2(double a, double b, double c)
{
double result2 = a  b  c;
//double equat = (b * b)  (4 * a * c);
//double result2 =  b  (Math.sqrt(equat)) / (2 * a);
return result2;
}
}
March 23rd, 2013, 02:26 PM

I appreciate the help and welcome to the forum!!!
I am very new here too... and obviously very new to computer science and Java.
It is too bad the this textbook uses the acm third party library for its coding as most examples and hints come in the form of what I am figuring are built in packages.
I like the way you broke down the equation into a much clearer set of calculations. It simplifies the holisitic view of the methods substantially.
I will modify my program and take another look at it now.
Thanks
March 23rd, 2013, 02:29 PM

I think it is easier when doing equations in java to break it down.
Code:
else {
return result1;
}
The return should be placed outside it.
March 23rd, 2013, 03:15 PM

This is the code I am working with now. I was able to get the errors to disappear but it is buggy still.
The text says I should enter coefficients as follows:
a = 1
b = 5
c = 6
and get the result:
the first answer is 3
the second answer is 2
I get 5.5 for both. I guess the equation is entered wrong. hmm... keep on trucking I guess, see what I can figure.
Code:
import acm.program.*;
import java.math.*;
public class Quadratic extends ConsoleProgram {
public void run() {
println("Enter coefficients for the quadratic equation.");
double a = readDouble("a: ");
double b = readDouble("b: ");
double c = readDouble("c: ");
double positive = solution1(a, b, c); // if coefficient b is positive
double negative = solution2(a, b, c); // if coefficient b is negative
println("The first solution is " +negative);
println("The second solution is " +positive);
}
private double solution1(double a, double b, double c) {
//x = b + ((Math.sqrt((b * b)  (4 * a * c))) / 2 * a);
double equat = (b * b)  (4 * a * c);
double result1 =  b + (Math.sqrt(equat)) / (2 * a);
if (equat < 0) {
println("The result cannot be calculated"); }
return result1;
}
private double solution2(double a, double b, double c) {
//x = b  ((Math.sqrt((b * b)  (4 * a * c))) / 2 * a);
double equat = (b * b)  (4 * a * c);
double result2 =  b + (Math.sqrt(equat)) / (2 * a);
if (equat < 0) {
println("The result cannot be calculated"); }
return result2;
}
}
March 24th, 2013, 05:12 AM

Seems like this is going to be a thread of forumnewbies, i am also new to the forum
to your code:
Yes there is an error, u use the same Version (always plus) in both equations. Since you have two nearly same equations it would be more robust to code if you put it into one function.
But i believe that one shouldn't interfer to much with the own style in the very beginning of the learning curve.
Another problem with ur code is that you used the formula wrong: the /2a must apply to the whole term. So in your code u need to change these two lines:
Code:
result1= ( b + Math.sqrt(equat)) / (2 * a);
...
result2= ( b  Math.sqrt(equat)) / (2 * a);
As said before i would recommend combining the function like this, but this is only for "further reading"
Code:
public void run() {
println("Enter coefficients for the quadratic equation.");
double a = readDouble("a: ");
double b = readDouble("b: ");
double c = readDouble("c: ");
double positive = quadSolution(a, b, c,true); // use positive result of the sqrt
double negative = quadSolution(a, b, c,false); // use negative result of the sqrt
println("The first solution is " +negative);
println("The second solution is " +positive);
}
private double quadSolution(double a, double b, double c,boolean shouldUsePositive) {
//x = (b + (Math.sqrt((b * b)  (4 * a * c))) / 2 * a);
double equat = (b * b)  (4 * a * c);
int factor= 1;
if(!shouldUsePositive)
factor = 1;
double result = (b + factor*Math.sqrt(equat)) / (2 * a);
if (equat < 0) {
println("The result cannot be calculated"); }
return result;
}
For really nice code it would be even better to define an enum which defines the variant you want to use. Therefore it would be more easy to read.
hth
mythos
March 24th, 2013, 09:10 AM

I got it working. I know the code could be more aesthetically pleasing and more efficient... but it works.
Code:
import acm.program.*;
public class Quadratic extends ConsoleProgram {
public void run() {
println("Enter coefficients for the quadratic equation.");
double a = readDouble("a: ");
double b = readDouble("b: ");
double c = readDouble("c: ");
double positive = solution1(a, b, c); // if coefficient b is positive
double negative = solution2(a, b, c); // if coefficient b is negative
println("The first solution is " +positive);
println("The second solution is " +negative);
}
private double solution1(double a, double b, double c) {
//x = b + ((Math.sqrt((b * b)  (4 * a * c))) / 2 * a);
double equat = (b * b)  (4 * a * c);
double result = ( b + (Math.sqrt(equat))) / (2 * a);
if (equat < 0) {
println("The result cannot be calculated");
return 1; }
return result;
}
private double solution2(double a, double b, double c) {
//x = b  ((Math.sqrt((b * b)  (4 * a * c))) / 2 * a);
double equat = (b * b)  (4 * a * c);
double result = ( b  (Math.sqrt(equat))) / (2 * a);
if (equat < 0) {
println("The result cannot be calculated");
return 1; }
return result;
}
}
March 24th, 2013, 09:13 AM

Thanks to all that took the time to respond!