Java: Calling variables of user input from one method to another

B_Ran

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;
}

}

Max Fichtelmann

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;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling variables from one method to another in Python

From Dev

Return the user input in another method from another class in Java

From Dev

Calling one method from another method in Python

From Java

Calling a method in one fragment from another

From Dev

Calling a method from one class in another class

From Java

calling another method from the main method in java

From Dev

How to access user input from another method

From Dev

How to pass variables from one method to another?

From Java

java calling a method from another class

From Dev

Calling one constructor from another, overloaded in Java

From Dev

Calling an instance declared in one method from another method

From Dev

How can I use a user input String from one method in another?

From Dev

Calling one bash function from inside another as a specified user

From Java

How to take user input from main method to another class and method?

From Java

How to make my method read user input from another method?

From Dev

How to access variables from one method into another method in ruby?

From Dev

Calling a list from one method to another in the same class in Python

From Dev

Django - Calling one class method from another in Class Based View

From Dev

Python Inherit from one class but override method calling another class?

From Java

Calling one method from another within same class in Python

From Dev

calling method from one controller to another controller in angular js

From Dev

Undefined Method - Calling a class in one file from another

From Dev

Kivy - Calling a pulsing method from one class to another

From Dev

Trouble calling one method into another

From Java

Calling a method from another method

From Dev

Calling method of class one to another class in android java?

From Dev

Calling Method of another class from Recursion Method: Java

From Dev

Java – repeating a method from user-input

From Dev

Java Check user input against variables in another class

Related Related

  1. 1

    Calling variables from one method to another in Python

  2. 2

    Return the user input in another method from another class in Java

  3. 3

    Calling one method from another method in Python

  4. 4

    Calling a method in one fragment from another

  5. 5

    Calling a method from one class in another class

  6. 6

    calling another method from the main method in java

  7. 7

    How to access user input from another method

  8. 8

    How to pass variables from one method to another?

  9. 9

    java calling a method from another class

  10. 10

    Calling one constructor from another, overloaded in Java

  11. 11

    Calling an instance declared in one method from another method

  12. 12

    How can I use a user input String from one method in another?

  13. 13

    Calling one bash function from inside another as a specified user

  14. 14

    How to take user input from main method to another class and method?

  15. 15

    How to make my method read user input from another method?

  16. 16

    How to access variables from one method into another method in ruby?

  17. 17

    Calling a list from one method to another in the same class in Python

  18. 18

    Django - Calling one class method from another in Class Based View

  19. 19

    Python Inherit from one class but override method calling another class?

  20. 20

    Calling one method from another within same class in Python

  21. 21

    calling method from one controller to another controller in angular js

  22. 22

    Undefined Method - Calling a class in one file from another

  23. 23

    Kivy - Calling a pulsing method from one class to another

  24. 24

    Trouble calling one method into another

  25. 25

    Calling a method from another method

  26. 26

    Calling method of class one to another class in android java?

  27. 27

    Calling Method of another class from Recursion Method: Java

  28. 28

    Java – repeating a method from user-input

  29. 29

    Java Check user input against variables in another class

HotTag

Archive