0

HOW CAN I create two functions that can insert and remove a Course in a Array?

I have 4 classes: 1.Student class 2.Course class 3.Courselist Class (*where I'll define the Insert Course and Remove course methods) 4.StudentCourseTest(main method) I'll just show the skeleton code so you'll know what I'm tryna figure out. Don't mind the other classes they are just there so we know how they relate to each other... I need help with creating the Methods within the CourseList class. public class Student { private String StudentId; private String FirstName; private String LastName; private CourseList courseList; public Student(String StudentId,String FirstName,String LastName,CourseList courseList) { // in here I'll just store the fields above with the passed in parameters.. } public class Course { public String CourseCode; public String CourseName; public String CreditPoint; { //I'll just set The Getter Methods here // And The setter Methods here } That is the skeleton of the two other classes.The actual class that will have the two methods for insertion&removal is 👇

11th Mar 2020, 3:07 PM
Nica STRAIT
Nica STRAIT - avatar
6 Antworten
+ 2
array size cannot be change so. 1 initiate the array with large size Course[]courses = new Course[99] 2 re create the array each time you modified. Course[]c = new Course[courses.length+1]; //copy the content from courses to c // add new value to last index of c courses = c; or the best way (imo) use arrayList
11th Mar 2020, 3:17 PM
Taste
Taste - avatar
+ 1
so 2nd solution from my previous answer then. recreate the array each time you need to modifed it
11th Mar 2020, 3:21 PM
Taste
Taste - avatar
+ 1
Taste Thanks a lot.I would initiate the array with a large size but my Lecturer specifically told us to Stick with it being initialized to 1.And then if we added a course record it will increment by 1 or if we removed a course it will decrement by 1. Thanks for the tip.I'll try ArrayList
11th Mar 2020, 3:26 PM
Nica STRAIT
Nica STRAIT - avatar
0
//That was my task // Task 1: Create a class called Course and define the data fields for each of the properties specified. For each data field create a getter and setter methods. The manipulation of data fields Should be done only through the setter methods. Task 2: Create a class called Student and define the data fields for each of the properties specified. For each data field create a getter method. The data fields should be initialized through the constructormethod //Below is the Courselist class that I need help with.I need to put in the right codes for my methods RemoveCourse(),setCourse() and getCourseList() public class CourseList { private Course[] courses=new Course[1]; private int counter=0; //Setters public void setCourse(Course course){ //Put your logic here to add a course } public Course removeCourse(String CourseCode){ //Put your logic here to remove a course //based on CourseCode } public Course[] getCourseList(){ //Put your code here } }
11th Mar 2020, 3:13 PM
Nica STRAIT
Nica STRAIT - avatar
0
My tasks were: Task 1: Create a class called Course and define the data fields for each of the properties specified. For each data field create a getter and setter methods. The manipulation of data fields Should be done only through the setter methods. Task 2: Create a class called Student and define the data fields for each of the properties specified. For each data field create a getter method. The data fields should be initialized through the constructor method. Task 3: Since a student can take more than one course, the course list property for Student class should be able to handle the insertion of course records dynamically when adding and removing courses. To achieve this, create a class called CourseList. In this class define the following properties and members: A Course array and initialize the array with length 1 o A counter to keep track of number of courses that are inserted into the array. Whenever, there is an insertion of course to the array, increment the counter by 1.
11th Mar 2020, 3:19 PM
Nica STRAIT
Nica STRAIT - avatar
0
o Add the following method to insert course to the array: public void setCourse(Course course) { //Put your logic here } o Add the following method to remove a course record: public Course removeCourse(String CourseCode) { //Put your logic here } Note that a course record must be removed by specifying the course code. o Add the following method to return an array of courses: public Course[] getCourseList(){ //Put your code here } //This is where I'm stuck.I need help to implement these 3 functions above... Any help or tip would be much appreciated Thankyou in advance
11th Mar 2020, 3:21 PM
Nica STRAIT
Nica STRAIT - avatar