Starting from:
$30

$24

Lab 12 Solution

This lab will help you to continue practicing with classes and objects. All parts of this lab must be part of a single java program – do not turn in more than one .zip file.




1. Write a class to represent a university course.







The course should have the following data associated with it: title, credit hours, and the UIDs of all students registered for the course. There should be no limit on the number of students that can register for the course (i.e. use an ArrayList instead of an array to hold the UIDs).







The course should also methods to register and withdraw from the course. The register method should a student to the list of registered students and the withdraw method should remove a student from the list of registered students. If a student tries to register for a course that they are already in, the register method should throw an exception. Similarly, if a student tries to withdraw from a course they are not in, the withdraw method should throw an exception.







Finally, provide a toString method that return the course name and number of credit hours. A sample driver program, Lab12CourseDriver.java has been provided to you. Running this driver program (with no modifications) together with your class should produce the following output:




U0123456
is registering for CS 1160
U0123456
is registering for CS 1180
U0123456
is registering for CS 1160
Error: U0123456 is already registered for Computer Science I (4)
U0123456
is withdrawing from CS 1180
U0123456
is withdrawing from CS 1180
E ror: U0123456 is not registered for C mputer Science I (4)
Problem 1 will be graded according to the following rubric:


The class contains the requested data fields: 1 point


The data fields are all private: 1 point


A constructor correctly initializes the data fields: 1 point


The register method adds a student to the course: 1 point


The register method throws an exception if the student is already registered:


1 point
The withdraw method removes a student from the course: 1 point


The withdraw method throws an exception if the student isn’t in the course:



1 point







The toString method produces the requested output: 1 point

2. Add an equals method to your course class with the following signature:




The methodpublicshouldbooleanreturnequals(Coursetrueiftheotherother)course’s title and number of credit hours are the same as this one, and false otherwise. For instance, when called in this manner:




Course cs1160 = new Course("Introduction to Computer Science", 3); Course cs1180 = new Course("Computer Science I", 4);




Course cs160 = new Course("Introduction to Computer Science", 3);




if (cs1160.equals(cs160)) {

System.out.println("CS
1160
is equivalent to CS 160");
} else {




System.out.println("CS
1160
is distinct from CS 160");
}




if (cs1160.equals(cs1180))
{


System.out.println("CS
1160
is equivalent to CS 1180");
} else {




System.out.println("CS
1160
is distinct from CS 1180");
The result should be:




}
















CS 1160 is equivalent to CS 160

CS 1160 is distinct from CS 1180




ProblemComputes2willbe thegradedcorrectaccordinganswer:to2thepointfollowing rubric:




FollowsCompliesstylewithguidelinesalldirections:and commented1point as appropriate: 1 point






















3. Add a method to the driver program (Lab11CourseDriver.java) with the following signature:




The methodpublicshouldstaticreturnCoursethe coursegetSmaller(Coursewiththesmallerc1,numberCourseof studentsc2) enrolled. If the courses both have the same number of students enrolled, return the second one. For example, if the following lines are added to the end of the main method in Lab11CourseDriver.java:




Course smaller = getSmaller(cs1160, cs1180);




should Sydisplay:tem.out.println(smaller + " is the smaller course");




becauseComptrat SciencethatpointI in(4)theisproblemthesmallthereris courseonestudent enrolled in 1160 and zero in 1180. Note that you can add additional methods to your Course class if

necessary to make this work, but you cannot make the data fields within the




Course class public because that vi lates encapsulation.




QuestionComputes3willbethegradedcorrectaccordinganswer:to2 thispointrubric:




FollowsCompliesstylewithguidelinesalldirections:and commented1point as appropriate: 1 point













4. The method below sorts an array of integers. Modify this method to instead sort







an array of Courses and add it to Lab11CourseDriveraccording.java.Youto canthe usenumberyourofworkstudentsfromenrolledQuestionin 3themtoassist you.




public static void sort(int[] arr) {




for (int i=0; i<arr.length; i++) {




int smallest = arr[i];

int smallestIndex = i;




for (int j=i+1; j<arr.length; j++) {




if (arr[j] < smallest) {

smallest = arr[j];




smallestIndex = j;

}




}




if (i != smallestIndex) {

int temp = arr[i];




arr[i] = arr[smallestIndex];

arr[smallestIndex] = temp;




}

}




When} the method above is called as follows:










int[] testArray = {5, 2, 9, -5, 3, 1, 10}; System.out.println(Arrays.toString(testArray)); sort(testArray);




it producesSystemthisoutput:..println(Arrays.toString(testArray));










[5, 2, 9, -5, 3, 1, 10]




[-5, 1, 2, 3, 5, 9, 10]

Modify this test code to work with an array of Course objects rather than an array of







courses.integers. MakeYoucansureusetowhateverregistervaryingtitles,credits,numbersetc.ofthatstudentsyouwantineachfor yourcourse.testAdd your test code to the end of the main method in Lab11Driver.java.







QuestionComputes4willbethegradedcorrectaccordinganswer:to2 thispointrubric:




FollowsCompliesstylewithguidelinesalldirections:and commented1point as appropriate: 1 point

More products