Jump to content

I am new to Java and what I usually tackle once I have familiarized myself with a new language is create a program to calculate the quadratic formula.  I have run across some errors that I do not know how to fix.  I will paste the source code below, but the current error I am getting is that i have a possible loss of precision because I need to define something as a double, not an int, and I can't do that without the word double ending in .class, but that doesn't work because it makes me create a new line, ending my calculation halfway through.  I'm sure there will be other errors, so if somebody could look through it and tell me what I need to fix, and also maybe some good practices, that would be great.

Thanks.

 

 
/** * Write a description of class quadraticformula here. *  * @author (Gabe Spound)  * @[member=versions] (8/21/15) */import java.util.Scanner;public class quadraticformula {    public static int handleradicand(double b, double a, double c){ //Step one, calculate the radicand(the part inside the square root)        int radicand = b * b - (4 * a * c); // b squared minus 4ac        return radicand; //return the radicand    }    public static int rootradicand(double rad){ //find the square root of the radical        int radical = Math.sqrt(rad); // square root of b squared minus 4ac        return radical; //return the radical    }    public static int divideby2a(double b, double rad, double a ){ //finding  -b over 2a and the square root over 2a and storing them in seperate variables.        int zerominusb = 0 - b; //storing -b as a variable because I wasn't sure if I could just type -b in a calculation        int bover2a = zerominusb / (2 * a); //-b over 2a        int radover2a = rad / (2 * a); // the radical over 2a        return bover2a; //return -b over 2a        return radover2a; //return radical over 2a    }    public static int findroots(double BO2A, double RO2A){ //find the roots of the equation        int firstX = BO2A + RO2A; // -b over 2a plus the radical over 2a        int secondX = BO2A - RO2A; // -b over 2a minus the radical over 2a        return firstX; //return the first root        return secondX; //return the second root    }    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.        int 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.        Scanner Bscanner = new Scanner(System.in); //Created a new scanner variable for the B value.        int B = Bscanner.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.        Scanner Cscanner = new Scanner(System.in); //Created a new scanenr variable for the C value.        int C = Cscanner.nextInt(); //Storing the next integer of that scanner variable for an Integer of the C value.        handleradicand(a, b, c); //Finding the radicand        rootradicand(radicand); //Finding the radical        divideby2a(b, radical, a); //Dividing -b and the radical by 2a        findroots(bover2a, radover2a); //returning the roots        System.out.println("Your roots are " + firstX + "and " + secondX); //displaying the roots
Edited by colonel_mortis
Code tags

I can help with programming and hardware.

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

Link to comment
https://linustechtips.com/topic/436682-help-with-java-program-for-quadratic-formula/
Share on other sites

Link to post
Share on other sites

Ok, first things first. Are you using an IDE to help you code? Something like Eclipse or Netbeans? Because there's a few java-specific errors I see, that an IDE will catch for you.

 

 

i have a possible loss of precision because I need to define something as a double

 

Yep, that is true, you do have that "feature".

 

 

 

and I can't do that without the word double ending in .class, but that doesn't work because it makes me create a new line, ending my calculation halfway through.

 

I don't quite understand what you mean by this? You should be able to replace all instances of int, with double.

Ensure a job for life: https://github.com/Droogans/unmaintainable-code

Actual comment I found in legacy code: // WARNING! SQL injection here!

Link to post
Share on other sites

Ok, first things first. Are you using an IDE to help you code? Something like Eclipse or Netbeans? Because there's a few java-specific errors I see, that an IDE will catch for you.

 

Yep, that is true, you do have that "feature".

 

 

I don't quite understand what you mean by this? You should be able to replace all instances of int, with double.

I'm Using BlueJ, here is the screenshot of the error i'm talking about specifically, but please point out the java specific errors and correct them, because sometimes I don't know what they mean.

post-119027-0-33009900-1440436207.png

I can help with programming and hardware.

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

Link to post
Share on other sites

I'm Using BlueJ, here is the screenshot of the error i'm talking about specifically, but please point out the java specific errors and correct them, because sometimes I don't know what they mean.

I solve this error, I am now near the end, I will post another screen shot.  I think its an issue with how my methods are returning variables and how I am handling that.  Below you see the error.  I have the method called handle radicand that takes in the arguments a b and c.  it returns the variable radicand.  The next method takes in the variable radicand but it says that the symbol radicand.  I believe this has to do with the variable not being public, if so, how do i make it public.

I can help with programming and hardware.

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

Link to post
Share on other sites

I solve this error, I am now near the end, I will post another screen shot.  I think its an issue with how my methods are returning variables and how I am handling that.  Below you see the error.  I have the method called handle radicand that takes in the arguments a b and c.  it returns the variable radicand.  The next method takes in the variable radicand but it says that the symbol radicand.  I believe this has to do with the variable not being public, if so, how do i make it public.

Yep. That is one of the java problems I referenced earlier. There is this thing called scope, where variables go byebye and can't be seen by other methods.

 

 

to fix it, you need to put the variables "outside" the methods. ("Private" in this case, means that other java classes can't see the variables)

import java.util.Scanner;public class quadraticformula {    // it's ok for these to be 0, because you'll be changing the values of them later    private double radicand = 0;    private double radical = 0;    private double bover2a = 0;    private double radover2a = 0;    private double firstX = 0;    private double secondX = 0;    public static int handleradicand(double b, double a, double c){ //Step one, calculate the radicand(the part inside the square root)

There's also the case of you can't return two things in java. You need to work around it somehow, but, we already did that, by changing the scope of the variables. So, change all the method's return type to void, instead of int or double.

Ensure a job for life: https://github.com/Droogans/unmaintainable-code

Actual comment I found in legacy code: // WARNING! SQL injection here!

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

×