javaarraysclassinstances

How do you obtain a value from the main method to use in another class?


I am currently writing a program to obtain user input for the coefficients and degree of a polynomial and then converting those inputs into a polynomial. In my main method I use a scanner to obtain a line of string that is then converted into a double array. However, I need to use the values of this double array in another class but cannot seem to do it. I have been stuck on this for awhile and even after researching I still seem unable to get it.

PolynomialDemo.java

public class PolynomialDemo  {
public static int degree;
public static double[] numbers;

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter the degree of the first polynomial -> ");
    degree = scan.nextInt();

    System.out.print("Enter its coefficients in order of descending powers -> ");
    String deg = scanner.nextLine();
    String[] numbersStr = deg.split(" ");
    double[] numbers = new double[ numbersStr.length ];
    for ( int i = 0; i < numbersStr.length; i++ )
    {
        numbers[i] = Integer.parseInt( numbersStr[i] );
    }

Polynomial.java

public class Polynomial implements PolyEval  {

private double[] coeffs;
private int degree;


public Polynomial() {
//creates polynomial 0
}

public Polynomial(double[] c) throws IllegalArgumentException{
//obtain coefficients
}

@Override
public int degree() {
 //return degree
}

@Override
public String toString() {
    //build polynomial

}}

Polynomial.java is the class I need to obtain the value in. When I have tried to do it, the value always returns 'null' when it gets to the Polynomial class. I'm stuck and not sure where to go from here. Thanks for the help!


Solution

  • You should instanciate a Polinomial object and set the values that you need.

    public class PolynomialDemo  {
    public static int degree;
    public static double[] numbers;
    
    public static void main(String[] args) {
    
        //Create a Polynomial object
        Polynomial pol = new Polynomial();
    
        Scanner scan = new Scanner(System.in);
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the degree of the first polynomial -> ");
        degree = scan.nextInt();
    
        System.out.print("Enter its coefficients in order of descending powers -> ");
        String deg = scanner.nextLine();
        String[] numbersStr = deg.split(" ");
        double[] numbers = new double[ numbersStr.length ];
        for ( int i = 0; i < numbersStr.length; i++ )
        {
            numbers[i] = Integer.parseInt( numbersStr[i] );
        }
    
    
        //Set values
        pol.setDegree(degree);
        pol.setCoefficients(numbers);
    

    }

    You need to define setDegree(int degree) and setCoefficients(double [] coeff) in you Polynomial class.

    Setters, constructors an so on are the basics of Java. Try to look for a tutorial or read the documentation. Also, try to modularize your code.