Results 1 to 5 of 5
- 10-13-2010, 03:07 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
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
and the errors.......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++; } }
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}
- 10-13-2010, 03:57 PM #2
Don't expect anyone to correct your errors.
Try your own .
IOn ArrayList conversion code ,you have only empty methods..no logic.Ramya:cool:
- 10-13-2010, 04:36 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
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.
- 10-13-2010, 06:54 PM #4
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
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.
- 10-13-2010, 07:22 PM #5
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
Similar Threads
-
Converting value of array to log
By monika in forum New To JavaReplies: 4Last Post: 05-14-2010, 08:10 AM -
converting 1D array to 2D for JTable
By phil128 in forum AWT / SwingReplies: 1Last Post: 03-12-2009, 12:08 PM -
Converting ArrayList to Array
By vasavi.singh in forum New To JavaReplies: 1Last Post: 02-23-2009, 02:34 PM -
Converting ArrayList to Array
By Java Tip in forum Java TipReplies: 0Last Post: 11-13-2007, 10:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks