0

I'm currently modifying a previous Java program that computes quadratic formula type math problems by breaking parts of my code down into methods and calling those methods to complete the same task. Currently I'm stuck on creating a method to calculate the discriminant in the numerator. As assigned, I'm supposed to have one method that receives user input for the a,b, and c values, but I'm not sure how to get those values from one method into the next that is supposed to use those values in calculations.

My instructor wants us to have the a b and c variables input into an array and I know the way it is now is a pretty manual way of putting values into an array, but should still work for this purpose.

Here is what I have thus far and thanks for reading.

EDIT: I've started again from scratch, I can't figure out how to properly return information from my methods so that the next can use it. I keep getting method argument not applicable errors. Any ideas?

import java.util.*;

public class QuadraticMethods {
public static void main(String[] args){

    getValues();
    calcDisc(userInput);



}


public static double[] getValues() {
    double[] userInput;
    userInput = new double[3];
    Scanner kbd = new Scanner(System.in);
    System.out.println("Fourth Assignment by MyNameHere");
    System.out.println("Welcome to the quadratic formula computation tool.");
    System.out.println("This tool will solve problems in the form of: a^x + bx + c.");
    System.out.println("Please enter the values you would like for a, b, and c.");
    for (int i = 0; i < userInput.length; i++) {
        userInput[i] = kbd.nextDouble(); }


    double aValue = userInput[0];
    double bValue = userInput[1];
    double cValue = userInput[2];
    /*
    System.out.println(aValue);
    System.out.println(bValue);
    System.out.println(cValue);
     */
    return userInput;
}


public static double calcDisc(double[] userInput) {
    double aValue = userInput[0];
    double bValue = userInput[1];
    double cValue = userInput[2];
    double radicalValue = (Math.pow(bValue, 2) - (4*aValue*cValue));
    System.out.println(radicalValue);
    return radicalValue;
}

}

B_Ran
  • 13
  • 1
  • 5
  • When you call `getValues` in main, it returns `double[]`. You need to capture the return value using `double[] userInput = getValues();`. Then pass it in as an input argument to `calcDisc()`. You'll need to change the method signature of `calcDisc` to `calcDisc(double[] userInput)`. – DavidS Feb 10 '15 at 03:45
  • In the future you'll have an easier time if you start learning what a variable's "scope" is. Google "Java variable scope" to learn more. – DavidS Feb 10 '15 at 03:47
  • Thanks David that makes sense. I'm currently learning about scope in class right now. Would I be capturing that return value at the beginning of my second method? or at the end of the first? – B_Ran Feb 10 '15 at 03:49
  • `getValues` returns a value that `calcDisc` needs to do it's job. So you capture the return value of `getValues` and use it as input for `calcDisc`. That said, you'll probably also want to capture the return value of `calcDisc` and print the results or something. – DavidS Feb 10 '15 at 03:55
  • Actually, looking closer I don't think `calcDisc` will compile. The method signature says it returns a `double` but you never call `return` in the method body. – DavidS Feb 10 '15 at 03:58

1 Answers1

1

To get your current code to work, only a small change is required:

public static void main(String[] args) {

    double[] userInput = getValues();
    calcDisc(userInput);
}

Further these assignments are not actually used.

public static double[] getValues() {
    // ...

    double aValue = userInput[0];
    double bValue = userInput[1];
    double cValue = userInput[2];

    // ...
}

Some other improvements could be:

The result should not be printed by the method that calculates it. You already declared the method the right way by returning the value. Now you should use the returned value and print the result in the calling method.

public static void main(String[] args) {

    double[] userInput = getValues();
    double radicalValue = calcDisc(userInput);

    System.out.println(radicalValue);
}

// ...

    public static double calcDisc(double[] userInput) {
    double aValue = userInput[0];
    double bValue = userInput[1];
    double cValue = userInput[2];
    double radicalValue = (Math.pow(bValue, 2) - (4 * aValue * cValue));
    return radicalValue;
}

Printing the banner should probably not be mixed with requesting the user input. Imagine, you would want to repeat the read/evaluate/print cycle:

public static void main(String[] args) {

    while (true) {
        double[] userInput = getValues();
        double radicalValue = calcDisc(userInput);

        System.out.println(radicalValue);
    }
}

would print the banner text every time. Isolating the responsibilities enables you to alter behaviour without affecting unrelated code.

public static void main(String[] args) {

    printBanner();
    while (true) {
        double[] userInput = getValues();
        double radicalValue = calcDisc(userInput);

        System.out.println(radicalValue);
    }
}

private static void printBanner() {
    System.out.println("Fourth Assignment by MyNameHere");
    System.out.println("Welcome to the quadratic formula computation tool.");
    System.out.println("This tool will solve problems in the form of: a^x + bx + c.");
}

Scanner should be closed after use. Java 7 try with resources will do that for you.

public static double[] getValues() {
    double[] userInput;
    userInput = new double[3];
    try (Scanner kbd = new Scanner(System.in)) {
        System.out.println("Please enter the values you would like for a, b, and c.");
        for (int i = 0; i < userInput.length; i++) {
            userInput[i] = kbd.nextDouble();
        }
    }
    return userInput;
}
Max Fichtelmann
  • 3,366
  • 1
  • 22
  • 27
  • That is huge Max. I wasn't understanding that I need to have the variables I'm trying to assign the return value to in my main. I need to have them declared and equal to my method so I can pass off the information to the next method, am I understanding correctly now? This was suggested to me by the previous poster yesterday but it didn't click I guess. – B_Ran Feb 11 '15 at 06:23
  • that is absolutely correct! in most cases when a method returns a value, it should be assigned and evaluated in some way. i.e. `String test = "test"; test.substring(2);` does not modify the String, but returns the modified value; `String st = test.substring(2);` does the trick – Max Fichtelmann Feb 11 '15 at 06:35