Problem in my java class
Results 1 to 5 of 5

Thread: Problem in my java class

  1. #1
    Banned
    Join Date
    Jun 2004
    Posts
    154

    Problem in my java class

    Ok this is what i am supposed to do

    [QUOTE]

    Create an object class named students.java with the following object variables:

    String s[] = new String[30];
    int number;

    The constructor, students(), will set the value of num to zero.

    Create the following object methods:

    void fill() - this will prompt the user to enter names (string), one per line which will be placed in the string array s. When the user enters a null string (zero length), entry will cease and the number of valid strings in the array will be place in the variable number.

    void list() - this will list the valid strings in the array s, one per line. If the value of num is zero, it will inform the user that there are no entries to list.

    void add() - this will add one student to the array if there is room. If there is no more room, a message to that effect will be displayed. All of the processing shown in the sample run will be done here

    void search() - this will search the names in the array. If the name does not exist, a message will inform the user of that fact. If the name does exist, the user will be asked if they want to change it. If the answer is no, then nothing more happens. If the answer is yes, then the user will be prompt for the change.
    [\QUOTE]



    This is what i have so far, is it ok so far? and also how can i make a method that adds a string tot he list?
    Any suggestions would be extremely appreciated.
    Code:
    import java.io.* ;
    public class students
    {
            String s[] = new String[30];
    
            students();
    
    void fill()
            {
             for(int i=0;i<31;i++)
              {
                    System.out.println("Please print the names");
                    MyInput.readString(s);
              }
            }
    void list()
            {
             for(i=0;i<31;i++)
              {
                    System.out.println(s[i]);
                    System.out.println();
              }
            //Dont forget about the msg if there are 0 entries
            }  
             
    void add() 
    
             
               
                    
                    
               
            
               
             
    } //End of Class

  2. #2
    Banned
    Join Date
    Jun 2004
    Posts
    154
    this is the entire problem, forgot to post it earlier:


    Create an object class named students.java with the following object variables:

    String s[] = new String[30];
    int number;

    The constructor, students(), will set the value of num to zero.

    Create the following object methods:

    void fill() - this will prompt the user to enter names (string), one per line which will be placed in the string array s. When the user enters a null string (zero length), entry will cease and the number of valid strings in the array will be place in the variable number.

    void list() - this will list the valid strings in the array s, one per line. If the value of num is zero, it will inform the user that there are no entries to list.

    void add() - this will add one student to the array if there is room. If there is no more room, a message to that effect will be displayed. All of the processing shown in the sample run will be done here

    void search() - this will search the names in the array. If the name does not exist, a message will inform the user of that fact. If the name does exist, the user will be asked if they want to change it. If the answer is no, then nothing more happens. If the answer is yes, then the user will be prompt for the change. See execution example below

    Create a method class with the default name that will create one students object and will execute a loop that runs the following menu unto the user selects exit:

    1 - list students
    2 - fill students array
    3 - add an additional student
    4 - search/change students
    5 - exit

  3. #3
    Banned
    Join Date
    Jun 2004
    Posts
    154
    nvm i figured it out

  4. #4
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    I'm not sure if you're still watching this thread or expecting an answer, but I'd like to give you some tips on the code that you've posted.

    Firstly I'd add a constant (i.e. a final) int at the top that holds the size that the array of Strings is going to be (and therefore the max num of students):
    Code:
    private static final int MAX_STUDENTS = 30;
    Just in case you don't understand any of the above I'll go through it. private indicates that only members of this class are able to access the variable. static means that there will only be one occurrence of the variable no matter how many objects are instantiated (you don't really need it, but it should save on some memory). final means that the integer cannot be changed from 30 later on in the code.

    Next I'd figure out what's happening with MyInput. Is it an external class that you have to import, or is it an instatiated reader object. If it is, I'd suggest either having it declared at the top of the class or passing it into the constructor. If it is (as I suspect) an external class...add the import statement. Here's a basic start to the class which takes into account some assumptions I've made:
    Code:
    import java.io.*;
    import someclass.I.wanna.Import; // change this
    
    public class Students
    {
    	private static final int MAX_STUDENTS = 30;
    	private String s[];
    	private int numStudents;
    
    	public Students()
    	{
    		numStudents = 0;
    
    		s = new String[MAX_STUDENTS]; // should do any creation in the constructor
    	}
    
    	// ...
    }
    Your method fill() will give you an ArrayIndexOutOfBoundsException because of this:
    Code:
    for(int i=0; i<31; i++)
    You have an array size of 30, yet you are telling the program to go through 0-30. That means that you are attempting to access 31 elements in an array of size 30. I would replace it with:
    Code:
    for(int i=0; i<MAX_STUDENTS; i++)
    Where MAX_STUDENTS is the final I suggested you declare at the top of your code.

    Since you are reading in an unspecified amount of input that only ceases when the user enters a null string, I would replace your for loop with a while loop and make your fill method the following:
    Code:
    public void fill()
    {
    	System.out.println("Please enter student names ending with an empty line");
    
    	while(numStudents<s.length)
    	{
    		MyInput.readString(s[numStudents]);
    		if(s[numStudents]==null)
    			break; // if I had time, I'd think of a better way to do
    			       // this without using break. Almost defeats the purpose
    			       // of using a while loop rather than for
    		numStudents++;
    	}
    }
    You have an empty println after each String printed. It was meant to be one per line and println adds in a newline char for you.
    Code:
    public void list()
    {
    	if(numStudents==0)
    	{
    		System.out.println("There are no entries in the array");
    		return; // so we don't need an else
    	}
    
    	for(int x=0; x<numStudents; x++)
    		System.out.println(s[x]);
    }
    I won't give you any methods that you've not written code for, especially since with the code I've given you above, you should be able to do it.

    Lastly, please note that I used the keyword "private" before the variable names. This is because only methods inside your class that you are creating should be able to access these variables directly. This might not always be the case, but I can't see any reason to use "public", "protected" or make them friendly in this case.

    You had the right idea, though, and you had made a try before you posted (more than most people), so keep trying.

    ac

  5. #5
    Banned
    Join Date
    Jun 2004
    Posts
    154
    hey thanks a lot gothic, i had just finished it but what you put helped me understand and clear up some of the code. I appreciate it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides