Why isn't my quad. equation class working? - java

My returning values are not coming up as right, and I'm just wondering what I am doing wrong, my code looks fine?!
some of the test that are coming up are:
Test failed: expected was not equal to actual
Expected = 9.000000 and actual = 0.000000
The discriminant is not being computed correctly
Test failed: expected was not equal to actual
Expected = -3.000000 and actual = 0.000000
public class QuadraticEquation {
//coefficients
private double a;
private double b;
private double c;
// created a discriminant instance variable so it's easier to access
// rather than tying out "Math.pow(b, 2) - 4 * a * c" every time
// the discriminant is required
private double discriminant = Math.pow(b, 2) - (4 * a * c);
//constructor
public QuadraticEquation(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
//getter for A
public double getA() {
return a;
}
//getter for B
public double getB() {
return b;
}
//getter for C
public double getC() {
return c;
}
// get discriminant
// the discriminant is inside of the square root,
// and b^2 - 4ac is the expression for the discriminant
public double getDiscriminant() {
//b^2 - 4ac
return discriminant;
}
//get root0
public double getRoot0(){
if (discriminant > 0) {
return -b + Math.sqrt(discriminant) / 2*a;
}
return Double.NaN;
}
public double getRoot1() {
if (discriminant > 0) {
return -b - Math.sqrt(discriminant) / 2*a;
}
return Double.NaN;
}
}

You're trying to calculate the discriminant before you've set a, b and c, so naturally it's coming out zero.
Move the line that calculates the discriminant down into the constructor.
public class QuadraticEquation {
//coefficients
private double a;
private double b;
private double c;
private double discriminant;
//constructor
public QuadraticEquation(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
discriminant = b * b - (4 * a * c);
}
// etc.
Note also that b * b generally outperforms Math.pow(b, 2) because of the way the latter is calculated.
Also, the operations inside getRoot0 and getRoot1 are happening in the wrong order. You need parentheses to do something like this.
public double getRoot0(){
if (discriminant > 0) {
return ( -b + Math.sqrt(discriminant)) / (2 * a);
}
return Double.NaN;
}
and similarly in getRoot1.

Related

Why does it show NaN or 0?? The operation clearly doesn't give 0

public static void main(String[] args) throws Exception {
long d = (long) (1-(factorial(364)
/factorial(365-12)*Math.pow(365,11)))*100;
System.out.println(d+"%");
}
private static double factorial (int num) {
if (num==0){
return 1;
}
return num * factorial(num-1);
}
It returns: 0%
And when I put it on double it shows NaN%
I don't know why it won't solve like this so I can then format the result like ##.##%
Try this.
divideFactorial(a, b) returns a! / b!.
private static double divideFactorial(int a, int b) {
double result = 1.0;
for (double d = a; d > b; d -= 1.0)
result *= d;
return result;
}
And
long d = (long) ((1 - divideFactorial(364, 365 - 12) / Math.pow(365, 11)) * 100);
System.out.println(d+"%");
output:
16%

Changing to values in my program to arrays

This is a question for school, I need to write a method to find real roots. My program currently works but now I need to return the two roots as arrays, and generalize it so when a is equals to 0 the method should return an array holding one element, holding -c/b, and if a=b=c=0 my method should throw an exception.
public class Quadratic {
public static void main(String args[]) {
Quadratic obj = new Quadratic();
int a = 1, b = -6, c = 12;
obj.findRoots(a, b, c);
}
public void findRoots(int a, int b, int c) {
// If a is 0, then equation is not
// quadratic, but linear
if (a == 0) {
throw new IllegalArgumentException("a = 0");
}
int d = b * b - 4 * a * c;
double sqrt_val = sqrt(abs(d));
if (d > 0) {
System.out.println("Roots are real and different \n");
System.out.println((double) (-b + sqrt_val) / (2 * a) + "\n" + (double) (-b - sqrt_val) / (2 * a));
} else {
return;
}
}
}
Can I simply convert the variables to arrays? We just started on an array chapter and I have to idea how to go about this.
just store the result to array.
double root[2]={0.0,0.0};
root[0]=(-b + sqrt_val) / (2 * a);
root[1]=(-b - sqrt_val) / (2 * a);
then return it!
public double[] findRoots(int a, int b, int c){
...
return root;
}

Java - multiplication of variables through parameters

what is the best method of calculating the product of integer variables through method parameters? I have tried using mathematical symbols such as '*' to get a result but nothing has been successful and I am lost for answers. Any advice would be greatly appreciated, thanks in advance.
int productOfThreeNumbers(int number1, int number2, int number3){
productOfThreeNumbers(number1 * number2 * number3);
return 0;
}
If you wish to obtain an integer value from a multiplication of integers you could try
public Integer mult(int a,int b){
int c = a*b;
return c;
}
If you want to obtain a double value you could use:
public double mult(int a,int b){
double n1 = (double) a;
double n2 = (double) b;
double c = n1*n2;
return c;
}
you call the method with:
int a = 1;
int b = 2;
int c = mult(a,b);
or
int a = 1;
int b = 2;
double c = mult(a,b);
depending on which method you are using.
But looking at your code just do:
int productOfThreeNumbers(int number1, int number2, int number3){
return (number1 * number2 * number3);
}
Define TriFunction
#FunctionalInterface
interface TriFunction<A,B,C,R> {
R apply(A a, B b, C c);
}
Then, use it:
public class Main {
public static void main(String[] args) {
TriFunction<Integer, Integer, Integer, Integer> triMult = (x,y,z) -> x*y*z;
System.out.println(triMult.apply(2, 1, 3));
}
}

Java: Return char Instead of double in Method

I have a simple program that uses methods to find the largest of, and the product of variables A, B, C and D. Just to explore, is there a way to write code into the methods to return "A", "B", etc. instead of just the value?
public class methods
{
public static void main (String[] args)
{
int A=1, B=10, C=-5, D=20;
System.out.println("The largest of A and B is " + Largest(A,B));
System.out.println("The largest of A, B and C is " + Largest(A,B,C));
System.out.println("The largest of A, B, C and D is " + Largest(A,B,C,D));
System.out.println("The product of A and B is " + Product(A,B));
System.out.println("The product of A, B and C is " + Product(A,B,C));
System.out.println("The product of A, B, C and D is " + Product(A,B,C,D));
}
public static double Largest(int A, int B)
{
if (A > B)
return A;
else
return B;
}
public static double Largest(int A, int B, int C)
{
if (A > B && A > C)
return A;
else if (B > A && B > C)
return B;
else
return C;
}
public static double Largest(int A, int B, int C, int D)
{
if (A > B && A>C && A>D)
return A;
else if (B > A && B>C && B>D)
return B;
else if (C > B && C>A && C>D)
return C;
else
return D;
}
public static double Product(int A, int B)
{
return A*B;
}
public static double Product(int A, int B, int C)
{
return A*B*C;
}
public static double Product (int A, int B, int C, int D)
{
return A*B*C*D;
}
}
Use a char return type instead of double for your methods. Also you have a few formatting issues that you should fix. Your method identifier Largest() should be largest(), with a lowercase "l". Capital letters are reserved for class names and constructors. Also your variables A,B,C,D should also be lowercase. Variables that are all caps should only be used for final variables. For example,
final int MAX = 100;
better to use this logic to find out largest among three
public static double Largest(int A, int B, int C) // complexity of program is getting reduced just by minimizing the comparison.
{
if (A > B){ //a is greater than B
if(A>C)
return A; //return a if A is largest among three
else
return B // return B if B is largest among three
}else{
if(B>C) //this statement will execute if B>A.
return B; //return B if B is largest amoung three.
else
return C; // return C if C is largest among three.
}
}

Getting NaN when I am trying to compute the zeros of a quadratic formula

I have two classes, the quadratic class and the runner class. This is the quadratic class.
public class Quadratic {
double a;
double b;
double c;
public double discrim = (Math.pow(b, 2) - 4*a*c);
public boolean hasSolutions() {
if(discrim >= 0){
return true;
}
else {
return false;
}
}
public double getSolution1() {
double pos = (b*-1 + Math.sqrt(discrim))/(2*a);
return pos;
}
public double getSolution2() {
double neg = (b*-1 - Math.sqrt(discrim))/(2*a);
return neg;
}
public Quadratic(double a, double b, double c) {}
}
And here is the runner class.
public class QuadraticRunner
{
public static void main( String args [])
{
Quadratic test1 = new Quadratic (1, 5, 6);
Quadratic test2 = new Quadratic (1, -4, 4);
Quadratic test3 = new Quadratic (1, 0, 3);
String equation1 = test1.toString();
boolean hasSolution1 = test1.hasSolutions();
double solution1 = test1.getSolution1();
double solution1b = test1.getSolution2();
String equation2 = test2.toString();
boolean hasSolution2 = test2.hasSolutions();
double solution2 = test2.getSolution1();
double solution2b = test2.getSolution2();
String equation3 = test3.toString();
boolean hasSolution3 = test3.hasSolutions();
double solution3 = test3.getSolution1();
double solution3b = test3.getSolution2();
System.out.println(equation1);
System.out.println(hasSolution1);
System.out.println(solution1);
System.out.println(solution1b);
System.out.println("Expected solutions: -2.0, -3.0");
System.out.println();
System.out.println(equation2);
System.out.println(hasSolution2);
System.out.println(solution2);
System.out.println(solution2b);
System.out.println("Expected solution: 2.0");
System.out.println();
System.out.println(equation3);
System.out.println(hasSolution3);
System.out.println(solution3);
System.out.println(solution3b);
System.out.println("NaN");
}
}
When I attempt to run this, the hasSolutions() method works fine, but both getSolution1() and getSolution2() come out as NaN. If the discriminant were negative I would understand, but it isn't negative for all the tests, so I am not sure where I went wrong.
You are calculating the discriminant before any values are assigned to a, b, or c. Also, you aren't using the constructor values passed in.
double a;
double b;
double c;
public double discrim = (Math.pow(b, 2) - 4*a*c);
public Quadratic(double a, double b, double c) {}
Because of this, you are dividing 0 by 0 in getSolution1 and getSolution2, which in floating-point arithmetic yields a NaN. (In math it's undefined.)
Move that calculation inside the constructor, after you assign values to a, b, and c.
public double discrim;
public Quadratic(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
discrim = (Math.pow(b, 2) - 4*a*c);
}
Your constructor doesn't set the field values, so a has a default value of 0 and you divide by zero when you say /(2 * a). Fix your constructor like,
public double discrim; // = (Math.pow(b, 2) - 4*a*c);
public Quadratic(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
this.discrim = (Math.pow(b, 2) - 4*a*c); // <-- also, move the math here.
}

Categories