Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 11-25-2008, 07:35 AM   #1
Golden Master
 
dude_se's Avatar
 
Join Date: Nov 2004
Posts: 8,632
Send a message via AIM to dude_se Send a message via MSN to dude_se
Default more java help needed!

here is the brief



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.
__________________

__________________
Laptop spec: ASUS X53E, i5 2430m 2.4ghz, 3gb ram, 320gb hdd, intel hd graphics, usb 3.0
dude_se is offline   Reply With Quote
Old 11-25-2008, 08:28 AM   #2
Daemon Poster
 
xtremezx2's Avatar
 
Join Date: Sep 2006
Posts: 915
Default Re: more jave help needed!

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.
__________________

xtremezx2 is offline   Reply With Quote
Old 11-25-2008, 11:40 AM   #3
Golden Master
 
dude_se's Avatar
 
Join Date: Nov 2004
Posts: 8,632
Send a message via AIM to dude_se Send a message via MSN to dude_se
Default Re: more jave help needed!

it wont let me rep you, but cheers anyway
ill have another go at re writing it later.
any other advice is also welcomed.
__________________
Laptop spec: ASUS X53E, i5 2430m 2.4ghz, 3gb ram, 320gb hdd, intel hd graphics, usb 3.0
dude_se is offline   Reply With Quote
Old 11-25-2008, 12:37 PM   #4
Daemon Poster
 
xtremezx2's Avatar
 
Join Date: Sep 2006
Posts: 915
Default Re: more jave help needed!

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.
xtremezx2 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 01:33 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0