Jump to content

simple Java help

I created a program for the quadratic formula, now i'm trying to add handling for negative radicands(the part under the radical).  Down in the main method, i have an if method that if tem1 is less that zero double tem2 = something and if anything else, tem2 = something.  After that if statement, i use the variable tem2 in a method, and it says that it is not found, do i have to return variables out of an if statement?

Thanks in advance

 

 

/**
 
 * Write a description of class quadraticformula here.
 * 
 * @author (??) 
 * @versions (8/21/15)
 */
import java.util.Scanner;
import java.util.*;
public class main {
    public static double handleradicand(double a, double b, double c){ //Step one, calculate the radicand(the part inside the square root)
        double radicand = b * b - (4 * a * c); // b squared minus 4ac
        return radicand; //return the radicand
    }
    public static double sub_0rootradicand(double rad){ //find the square root of the radical
        double radical = Math.sqrt(Math.abs(rad));
        return radical;
    }
    public static double rootradicand(double rad){
        double radical = Math.sqrt(rad);
        return radical;
    }
    public static double divideBby2a(double b, double a) { //finding  -b over 2a and the square root over 2a and storing them in seperate variables.
        double zerominusb = 0 - b; //storing -b as a variable because I wasn't sure if I could just type -b in a calculation
        double bover2a = zerominusb / (2 * a); //-b over 2a
        return bover2a;
    }
    public static double divideRADby2a(double rad, double a){
        double radover2a = rad / (2 * a); // the radical over 2a
        return radover2a;
    }
    public static double findroot1(double BO2A, double RO2A){ //find the roots of the equation
        double first_X = BO2A + RO2A; // -b over 2a plus the radical over 2a
        return first_X; //return the first root
    }
    public static double findroot2(double BO2A, double RO2A){
        double second_X = BO2A - RO2A;
        return second_X;
    }
    public static void main(String[] args){ //execute the previously defined methods to calculate the quadratic equation.
        System.out.println("What is the A value?"); //Asking the user for the A value in their quadratic equation.
        Scanner Ascanner = new Scanner(System.in); //Created a new scanner variable for the A value.
        double a = Ascanner.nextInt(); //Storing the next integer of that scanner variable for an Integer of the A value.
        System.out.println("What is the B value?"); //Asking the user for the B value in their quadratic equation.
        double b = Ascanner.nextInt(); //Storing the next integer of that scanner variable for an Integer for the B value.
        System.out.println("What is the C value?"); //Asking the user for the C value in their quadratic equation.
        double c = Ascanner.nextInt(); //Storing the next integer of that scanner variable for an Integer of the C value.
        //Finding the radicand
        double tem1 = handleradicand(a,b,c);
        System.out.println("radicand" + tem1);
        //Finding the radical
        if (tem1 <= 0) {
           double tem2 = sub_0rootradicand(tem1);
        } else {
           double tem2 = rootradicand(tem1);
        }
        System.out.println("radical" + tem2);
        double tem3 = divideBby2a (b,a);
        double tem4 = divideRADby2a (tem2, a);
        double firstX = findroot1 (tem3, tem4);
        double secondX = findroot2 (tem3, tem4);
        if (tem1 <= 0) {
            System.out.println("Your roots are X = i" + firstX + " " + "and X = i" + secondX);
        } else {
            System.out.println("Your roots are X = " + firstX + " " + "and X = " + secondX); //displaying the roots
    }
    }
}

I can help with programming and hardware.

<Script>alert("This website is vulnerable to XSS");</Script>

Link to comment
Share on other sites

Link to post
Share on other sites

I'm more familiar with C# but given that it's pretty similar to Java generally I would assume variable scoping works the same between the two.

 

You should move your declaration code outside (prior to) the if statement and perform the assignment to tem2 within the if/else block.  As it is currently, the variable tem2 only exists within the if/else block and then goes out of scope once the block exits.

Link to comment
Share on other sites

Link to post
Share on other sites

I'm more familiar with C# but given that it's pretty similar to Java generally I would assume variable scoping works the same between the two.

 

You should move your declaration code outside (prior to) the if statement and perform the assignment to tem2 within the if/else block.  As it is currently, the variable tem2 only exists within the if/else block and then goes out of scope once the block exits.

I kind of understand, do you mean i should set tem 2 = to the if statement?

I can help with programming and hardware.

<Script>alert("This website is vulnerable to XSS");</Script>

Link to comment
Share on other sites

Link to post
Share on other sites

I kind of understand, do you mean i should set tem 2 = to the if statement?

you don't actually have to set tem2. but you should declare it before the if statement, you can assign it whatever you like but this ensures that any methods that use tem2 has been initialized. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×