- Allow the user to enter two integers a and b. Have two methods called sum and product and when they are used with the input a and b, they return their sum and product.
- And another part that allows the user to enter 3 real numbers a b and c. Use the method Discriminant (with input a(!=0),b,c) to return the value of D=b^2-4ac. If a=0, the discriminant and the roots do not exist. Program should output the following:
- you have entered a=..., b=..., c=...
- the quadratic equation is axx+bx+c=0
- the discriminant is D=...{D is calculated using the formula D=b*b-4*a*c}
Inside the main body of the program, using conditional statements check the following:
- if D is positive call the method TwoRoots to return the two values of the roots
Output of the roots are x1=-b+Sqr D/2a and x1=-b-Sqr D/2a
- if D is negative output is no real roots
I did the first part but it isn't working. This is my first time using methods and its a bit confusing, i only know how to use them without user input. But i need user input for this. These are the codes i tried: The first code is the with return method and the second code is the without return method.
class quadratics1 {
int a,b;
String input1=JOptionPane.showInputDialog("Please enter an integer");
a=Integer.parseInt(input1);
String input2=JOptionPane.showInputDialog("Please enter another integer");
b=Integer.parseInt(input2);
int sum1(int L, int W){
int sum=(L+W);
return sum;
}
int product1(int L, int W){
int product=2*(L+W);
return product;
}
public static void main(String str[]){
quadratics1 m = new quadratics1();
System.out.println(a+" " + b+ " " + m.sum1(a,b));
System.out.println(a+" " + b+ " " + m.product1(a,b));
}
}
The errors i am getting are: identifier expected for the variables a and b in lines 4 and 6
I also tried this:
import javax.swing.*;
class quadratics1{
void sum(int L, int W){
int sum1=(L+W);
System.out.println(a+" " + b+ " " + m.sum1(L,W));
}
void product(int L, int W){
int product1=2*(L+W);
System.out.println(a+" " + b+ " " + m.product1(L,W));
}
public static void main(String str[]){
int a,b;
quadratics1 m = new quadratics1();
String input1=JOptionPane.showInputDialog("Please enter an integer");
a=Integer.parseInt(input1);
String input2=JOptionPane.showInputDialog("Please enter another integer");
b=Integer.parseInt(input2);
}
}
I do not know what i am doing wrong, I get errors for the variables. Any help would be greatly appreciated. I have been trying to figure this out for a few days. I'm new to coding and I have to have this done by tomorrow
For the second part, I wrote this code for the equations but i dont know how to make it into a method.
double a, b, c,d,r,rr;
String input1 = JOptionPane.showInputDialog("Please, enter the first real number");
a = Double.valueOf(input1).doubleValue();
String input2 = JOptionPane.showInputDialog("Please, enter the second real number");
b = Double.valueOf(input2).doubleValue();
String input3 = JOptionPane.showInputDialog("Please, enter the third real number");
c = Double.valueOf(input3).doubleValue();
d= Math.pow(b,2) - 4*a*c;
d = Math.round(d*100)/100.0;
r= (-b + Math.sqrt(Math.pow(b,2) - (4*a*c)))/(2*a);
r = Math.round(r*100)/100.0;
rr= (-b - Math.sqrt(Math.pow(b,2) - (4*a*c)))/(2*a);
rr = Math.round(rr*100)/100.0;
System.out.println("You have entered a = " + a + " b = " + b + " c = " + c);
System.out.println("The quadratic equation is " + a + "x^2 " + b + "x " + c + " = 0" );
System.out.println("The discriminant is D= " + d + " D is calculated using the formula D= " + b + "^2 " + " - 4" + "("+ a + ")" + "("+ c + ")");
System.out.println("The roots are " + r + " and " + rr);