I am making a quadratic formula solver for java which contains 2 java files. One
ID: 3758532 • Letter: I
Question
I am making a quadratic formula solver for java which contains 2 java files. One has all the code and a tester. Please inform me what I am doing wrong because when I test values a = 1 b = 2 c = 1 , the program displays root1: 0.0 root2: -1.0.
import java.util.Scanner;
public class CalculatorTest {
private int a = 0;
private int b = 0;
private int c = 0;
private double answer = 0;
public int askA()
{
System.out.println("What is the a value?");
Scanner scan1 = new Scanner(System.in);
a = scan1.nextInt();
return a;
}
public int askB()
{
System.out.println("What is the b value?");
Scanner scan2 = new Scanner(System.in);
b = scan2.nextInt();
return b;
}
public int askC()
{
System.out.println("What is the c value?");
Scanner scan3 = new Scanner(System.in);
b = scan3.nextInt();
return c;
}
public void root1()
{
System.out.println("Root 1: ");
answer = (-b + Math.sqrt(b*b - 4*a*c) ) / (2*a);
System.out.println(answer);
}
public void root2()
{
System.out.println("Root 2: ");
answer = (-b - Math.sqrt(b*b - 4*a*c) ) / (2*a);
System.out.println(answer);
}
public CalculatorTest()
{
}
}
Now this is the tester:
public class CalculatorTestTester {
public static void main(String args[]){
CalculatorTest doit = new CalculatorTest();
doit.askA();
doit.askB();
doit.askC();
doit.root1();
doit.root2();
}
}
Explanation / Answer
import java.util.Scanner;
public class CalculatorTest {
// private int a = 0;
// private int b = 0;
//private int c = 0;
private double answer = 0;
public int askA()
{
System.out.println("What is the a value?");
Scanner scan1 = new Scanner(System.in);
a = scan1.nextInt();
return a;
}
public int askB()
{
System.out.println("What is the b value?");
Scanner scan2 = new Scanner(System.in);
b = scan2.nextInt();
return b;
}
public int askC()
{
System.out.println("What is the c value?");
Scanner scan3 = new Scanner(System.in);
b = scan3.nextInt();
return c;
}
public void root1(int a,int b,int c)
{
System.out.println("Root 1: ");
answer = (-b + Math.sqrt(b*b - 4*a*c) ) / (2*a);
System.out.println(answer);
}
public void root2(int a,int b,int c)
{
System.out.println("Root 2: ");
answer = (-b - Math.sqrt(b*b - 4*a*c) ) / (2*a);
System.out.println(answer);
}
public CalculatorTest()
{
}
}
Now this is the tester:
public class CalculatorTestTester {
public static void main(String args[]){
CalculatorTest doit = new CalculatorTest();
int a1,b1,c1;
a1=doit.askA();
b1=doit.askB();
c1=doit.askC();
doit.root1(a1,b1,c1);
doit.root2(a1,b1,c1);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.