more jave help needed!

dude_se

Proper Legend
Messages
8,634
Location
UK
more java help needed!

here is the brief

293u63s.jpg


im not sure how to do it lol. im guessing i have to create an array at the beginning...

this is the code we were given to start with. its meant to print the list.
Code:
public static void printList(int[] myArray, int next)
{
	System.out.println("List follows: ");
	for (int i=0; i < next; i++)
  { 
		System.out.println(i+": "+ myArray[i]);
	}
}

any ideas? the code doesnt need to be fancy at all.
 
You need to create a class, you look to be just making a function.
The array should be a class level variable and should not have to be passed into the class, the constructor will instantiate an empty class level array. This array can be accessed by functions in the class, but not directly by the user. The print function you have above should not have anything passed to it, the class already has the array and knows the array, and having the array you can get the length. I don't remember the syntax for classes in java, but if I was doing it in c# it would be something like this.

Code:
    public class ListClass
    {
         int[] myList;   //the list
         int cnt;  //How many items are in the arrray.
         
         //Constructor
         public void ListClass() 
         {
              //Initialize Class variables
              int[] myList = new int[5];
              int cnt = 0;  
         }
         public void printList()
         {
	      System.out.println("List follows: ");
	      for (int i=0; i < cnt; i++)
              { 
		   System.out.println(i+": "+ myList[i]);
	      }
         }
         //I'll let you figure out the rest of the functions. But here are the ones it looks
         //like would be useful.

         public void add() {} //could possibly pass an index into this for placement, but looks like
                                    //you could just add to the end.
         public void remove(int index){} same as above. could just remove last.

         //These 2 below would be considered accessors (assuming your teacher has brought up
         //the concept of accessors and mutators).

         public int count() {} //this one is easy, just return your count variable.

         private bool isFull() {} //this would be a good private function to have to check if the
                                       //could also be public if you want the user to be able to check
                                       //array is full (in this case user is a programmer using your class.
    }

Hopefully that will kind of illustrate what I'm getting at without giving away the whole project and take away the learning experience. Classes are very important in object oriented programming, hopefully your java teacher explains them better than mine did.
 
it wont let me rep you, but cheers anyway :)
ill have another go at re writing it later.
any other advice is also welcomed.
 
Glad I could help, let me know how that works out for you and if you have any problems, remember the above is actually C# for the most part aside from your code that I dropped in, just think of it as psuedocode for the most part, but the layout should be quite similar. Like I said, classes are a very important part of OOP so make sure you understand this thoroughly to make you life and transitions to other languages easier. If you have problems or questions just let me know, I'd be glad to help. :D
 
Back
Top Bottom