Results 1 to 5 of 5
  1. #1
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default Converting an array program to ArrayList

    Good morning world! I am trying to edit an array code and make it into an ArrayList code instead. I have both codes, and I am trying to finish it, but I am getting an error...... I am sorry for the long post - I will post the regular array code, then what I have, and then the errors. Long post, but if you could help me define the error, I would appreciate it. Here goes.....

    Java Code:
    public class Course
    {
    	public final int CAPACITY = 10;
    	
    	private String courseName;
    
    	private String[] students;
    
    	private int numberOfStudents;
    
    	/**
    	 * precondition courseName is not null
    	 * @param courseName
    	 * postcondition: empty course has been create
    	 * @throws IllegalArgumentException if courseName is null
    	 */
    	public Course(String courseName)
    	{
    		if (courseName == null)
    			throw new IllegalArgumentException ("null course name");
    		this.courseName = courseName;
    		students = new String[CAPACITY];
    		numberOfStudents = 0;
    	}
    
    	/**
    	 * precondition: course is not full
    	 * @param student
    	 * postcondition: student has been added to the course
    	 * @throws IllegalArgumentException if course is full
    	 */
    	public void addStudent(String student)
    	{
    		if (numberOfStudents == CAPACITY)
    			throw new IllegalArgumentException ("course is full");
    		students[numberOfStudents] = student;
    		numberOfStudents++;
    	}
    
    	/**
    	 * @return
    	 */
    	public String[] getStudents()
    	{
    		return students;
    	}
    
    	/**
    	 * @return
    	 */
    	public int getNumberOfStudents()
    	{
    		return numberOfStudents;
    	}
    
    	/**
    	 * @return
    	 */
    	public String getCourseName()
    	{
    		return courseName;
    	}
    
    	/**
    	 * precondition: student is in the course
    	 * @param student
    	 * postcondition: student has been dropped from the course
    	 * @throws IllegalArgumentException if student is not in the course
    	 */
    	public void dropStudent(String student)
    	{
    		
    	}
    }

    Now, MY CONVERSION SO FAR

    Java Code:
    import java.util.ArrayList;
    
    public class Course
    {
    	public final int CAPACITY = 10;
    	
    	private String courseName;
    	
    	private ArrayList<String> students  = new ArrayList();
    
    	private int numberOfStudents;
    
    	/**
    	 * precondition courseName is not null
    	 * @param courseName
    	 * postcondition: empty course has been create
    	 * @throws IllegalArgumentException if courseName is null
    	 */
    	public Course(String courseName)
    	{
    		if (courseName == null)
    			throw new IllegalArgumentException ("null course name");
    		
    		this.courseName = courseName;
    		
    		
    		ArrayList<String> CAPACITY = new ArrayList();
    		
    		numberOfStudents = 0;
    	}
    
    	
    
    	/**
    	 * @return
    	 */
    	public ArrayList<String> getStudents()
    	{
    		return students;
    	}
    
    	/**
    	 * @return
    	 */
    	public int getNumberOfStudents()
    	{
    		return numberOfStudents;
    	}
    
    	/**
    	 * @return
    	 */
    	public String getCourseName()
    	{
    		return courseName;
    	}
    
    	/**
    	 * precondition: student is in the course
    	 * @param student
    	 * postcondition: student has been dropped from the course
    	 * @throws IllegalArgumentException if student is not in the course
    	 */
    	public void dropStudent(String student)
    	{
    		
    	}
    
    	
    	/**
    	 * precondition: course is not full
    	 * @param student
    	 * postcondition: student has been added to the course
    	 * @throws IllegalArgumentException if course is full
    	 */
    	
    	public void addStudent(String string) 
    	{
    		if (numberOfStudents == CAPACITY)
    			throw new IllegalArgumentException ("course is full");
    		
    		
    		numberOfStudents++;
    		
    	}
    }
    and the errors.......

    Java Code:
    actual:                                 expected:
     new java.util.ArrayList:1(){}.......... new Object[10](){
                                              [0]  "bubba",
                                              [1]  "aaa",
                                              [2]  "bbb",
                                              [3]  "ccc",
                                              [4]  "ddd",
                                              [5]  "eee",
                                              [6]  "fff",
                                              [7]  "hhh",
                                              [8]  "iii",
                                              [9] null}

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    Don't expect anyone to correct your errors.

    Try your own .

    IOn ArrayList conversion code ,you have only empty methods..no logic.
    Ramya:cool:

  3. #3
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    Good morning Ramya. My first comment was that I am looking for help on seeing where the error was/ defining it, and I would work on it. I do not recall mentioning that I was asking for a free hand here. I do this every now and then (asking for input) - no one else seems to mind on giving me pointers (which is the point of a forum).

    Thanks for your input on the logic.

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default

    Adomini,

    Drop the following code from the Course constructor, it is conflicting with the final integer of the same name.
    Java Code:
    ArrayList<String> CAPACITY = new ArrayList();

    Regards.

  5. #5
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    Thank you! I will do that.

    Have a good rest of the day.

    Brandon

Similar Threads

  1. Converting value of array to log
    By monika in forum New To Java
    Replies: 4
    Last Post: 05-14-2010, 08:10 AM
  2. converting 1D array to 2D for JTable
    By phil128 in forum AWT / Swing
    Replies: 1
    Last Post: 03-12-2009, 12:08 PM
  3. Converting ArrayList to Array
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-23-2009, 02:34 PM
  4. Converting ArrayList to Array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-13-2007, 10:41 AM

Posting Permissions

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