Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Constructor and declaration issues

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    9
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Constructor and declaration issues

    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.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,511
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Constructor and declaration issues

    Your problem is that you have created the int array inside the SortedListArray constructor. This means it is local and cannot be accessed anywhere else in your program. Maybe you should create an int array in the main method and use that instead.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    9
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Constructor and declaration issues

    Ok, sounds good. I can still use the methods created at the bottom with the int array I'll create right? I guess I've only ever used methods on objects I've declared within the class, not variables within the main.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,511
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Constructor and declaration issues

    int[] array = new int[someValue];
    SortedListArray listObj= new SortedListArray(); // no longer need a constructor in the SortedListArray class
    listObj.process(array, 5, 1);
    Something like that.

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    9
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Constructor and declaration issues

    Oh man, so clear now. Thank you.

Similar Threads

  1. Copy Constructor
    By Rhyssa6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2011, 10:43 PM
  2. Variable declaration placement
    By 2nickpick in forum Java Theory & Questions
    Replies: 2
    Last Post: January 22nd, 2011, 10:34 AM
  3. Use variable in array declaration statement
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 24th, 2010, 03:42 PM
  4. Overriding methods declaration problems
    By TurboTuring in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 9th, 2010, 11:23 AM
  5. Very beginner - What's wrong in my applet declaration?
    By rforte in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 30th, 2010, 04:54 AM