So I am working on an assignment for my Data Structures class and am getting all confused by how to correctly use an object I created. This object listObj of the SortedListArray class is basically just an integer array that would implement some methods I wrote out (Sort, process, and toString). I just can't seem to pass the array as an argument whenever I call a method.
Errors given whenever I pass intArray as an argument when calling methods:
intArray cannot be resolved to a variable
cannot make a static reference to a non-static method
In the constructor for the object I've just created an int[] with an int argument for the number of indices. Whenever I want to call a method of that object that needs a reference to that array shouldn't I just reference the int array that I created within the constructor?
import java.io.*; import java.util.Scanner; public class SortedListArray implements SortedListInterface { public static void main(String[] args) { int i=0; int noOfInputs = 0; int submission = 0; int n = 1; int num = 0; Scanner kybd = new Scanner(System.in); System.out.println("Please enter the number of integers you will put into the array: "); noOfInputs = kybd.nextInt(); //holds the user-defined no. of integers to be held within the array SortedListArray listObj= new SortedListArray(noOfInputs); for(i = 0; i < noOfInputs; i++) { kybd.nextLine(); System.out.println("Please enter integer" + n); submission = kybd.nextInt(); listObj.process(intArray, submission, n); n++; } System.out.println(listObj.toString(intArray, noOfInputs)); listObj.sort(intArray, noOfInputs); System.out.println ("Here are the newly sorted entries:\n"); System.out.println(listObj.toString(intArray, noOfInputs)); } public SortedListArray(int amount) { int[] intArray = new int[amount]; } public void sort(int[] intArray, int inputs) {// pre-conditions: passed arguments must be a valid integer array and integer value // compares an index value at the beginning of the array to the next index value and switches their order // from smallest to largest in an ascending manner // this is continued for the entire array int temp; for(int i = 0; i < inputs; i++) { for(int j = i + 1; j < inputs; j++) if(intArray[i] > intArray[j]) { temp = intArray[i]; intArray[i] = intArray[j]; intArray[j] = temp; } } } public String toString(int[] intArray, int inputs) { //preconditions: Must pass a valid integer array and integer value //returns a nicely formatted String value of all of the indices and the values contained within String stringFormat = null; System.out.println ("Current contents of the array are: \n\n"); for (int i = 0; i < inputs; i++) { stringFormat =(stringFormat + "Index " + i + ": " + intArray[i] + "/n"); } return stringFormat; } public void process(int[] intArray, int inputs, int num) { //pre conditions: Passed argument must be a valid int array //processes the user input and moves them into the array indices intArray[num] = inputs; num++; } }
and the interface that it is implementing...
public interface SortedListInterface { public void sort(int[] intArray, int inputs); // Pre condition: Passed argument is an integer array // method reorganizes the contents of the array from smallest to largest public String toString (int[] intArray, int inputs); // pre conditions: Passed arguments must be a valid int array and int variable // returns a well-formatted print out of the current contents of the array public void process(int[] intArray, int inputs, int num); //pre conditions: Passed arguments must be a valid int array and int variable //processes the user input and moves them into the array indices }
By the way, this program just reorganizes integers from lowest to highest within an array, but our teacher wants us to use an ADT to complete it.