-5

I'm trying to send a one D array to a class. I want the mutator method to copy the array, save it and then send it back to my tester class...so far I keep getting errors from the compiler. I know how to write a method that copies the array in main but I can't seem to get it done with the class though. Here is the code:

public class Class1D {

private int degree;
private int [] coefficient;

    public Class1D(int degree){
    this.degree =degree;
    }

    public void setCoefficent( int[] a, int degree){

        this.coefficient[degree] = a[degree];

        for ( int i=0; i<=a.length-1; i++)
        {
            this.coefficient[i] = a[i];
        }
    }
      public int [] getCoefficient() {
          return coefficient;

    }

}


import javax.swing.JOptionPane;


public class copy1D{

    public static void main(String[]args)
    {

        String input;
        int degree;


        input = JOptionPane.showInputDialog(" what is the degree of the polynomial?");
        degree = Integer.parseInt(input);
        degree= degree+1;
        int [] array = new int[degree];

        for ( int i =0; i<=array.length-1; i++)
        {
            input = JOptionPane.showInputDialog(" Enter coefficients:");
            array[i] = Integer.parseInt(input);
        }

        for ( int i =0; i<=array.length-1; i++)
        {
            System.out.print(array[i] + " ");
        }



        Class1D array2 = new Class1D(degree);

    }
    }
}
theredfox24
  • 87
  • 1
  • 2
  • 11
  • 1
    You say you are getting errors from the compiler - can you be specific? – D M Jan 30 '17 at 17:56
  • it kept saying class interface or enum expected ...also I have no idea how to have a class copy an array. Have a little pity on new computer scientists please.... – theredfox24 Jan 30 '17 at 17:59
  • if I had no pity I'd ignore the question :) I notice that the very first line in that code, `public Class1D {`, doesn't seem to be valid. Shouldn't that be `public class Class1D {`? – D M Jan 30 '17 at 18:01
  • yes go on and make some fun of my ADHD, I fixed it thx in a way – theredfox24 Jan 30 '17 at 18:07
  • 1
    I'm seriously not making fun. Small things like that will make the whole thing not compile. – D M Jan 30 '17 at 18:15
  • its ok...sometimes I make small mistakes like that and it takes me 15 minutes or something to notice them...anyway this was really helpful especially the a.clone part. I'm hoping my question can help other beginners in Java with copying 1D arrays – theredfox24 Jan 30 '17 at 20:18
  • You may also want to see http://stackoverflow.com/questions/7179251/is-there-any-reason-to-prefer-system-arraycopy-over-clone – D M Jan 30 '17 at 20:28
  • I added that to my answer, thanks :) – Ofer Arial Jan 30 '17 at 21:13

2 Answers2

2

You should send the a array to the constructor of Class1D and set it to your member variable as follows:

public Class1D(int degree, int[] a){

    this.degree =degree;
    this.coefficient = a.clone();
    // You can also another way that is faster, use:
    // System.arraycopy(a, 0, this.coefficient, 0, a.length);

}

And your call to the construction will be:

Class1D c1d = new Class1D(degree, array);
Ofer Arial
  • 1,129
  • 1
  • 10
  • 25
0
import javax.swing.JOptionPane;


public class copy1D{

    public static void main(String[]args)
    {

        String input;
        int degree;


        input = JOptionPane.showInputDialog(" what is the degree of the polynomial?");
        degree = Integer.parseInt(input);
        degree= degree+1;
        int [] array = new int[degree];

        for ( int i =0; i<=array.length-1; i++)
        {
            input = JOptionPane.showInputDialog(" Enter coefficients:");
            array[i] = Integer.parseInt(input);
        }

        for ( int i =0; i<=array.length-1; i++)
        {
            System.out.print(array[i] + " ");
        }

        makecopy(array,degree);

        Class1D c1d = new Class1D(degree, array);



    }
        public static void makecopy(int[]a, int deg)
        {
            int [] b = new int [deg];
            for ( int i =0; i<=a.length-1; i++)
        {
               b[i] = a[i];
        }

        System.out.println(" I have copied the array ");
        for ( int i =0; i<=a.length-1; i++)
        {
               System.out.print(b[i]+ " ");
        }
    }
}

public class Class1D {

private int degree;
private int [] coefficient;

    public Class1D(int degree){
    this.degree =degree;
    }

    public Class1D(int degree, int[] a){

    this.degree =degree;
    this.coefficient = a.clone();
}

      public int []getCoefficient() {
          return coefficient;

    }

}
theredfox24
  • 87
  • 1
  • 2
  • 11