below is the lab12coursedriver.java file package lab12; below is the lab12course
ID: 3690120 • Letter: B
Question
below is the lab12coursedriver.java file
package lab12;
below is the lab12coursedriver.java file.
package lab12;
import java.util.Arrays;
public class Lab12CourseDriver {
public static void main(String[] args) {
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");
}
String student = "U0123456";
try {
System.out.println(student + " is registering for CS 1160");
cs1160.register(student);
} catch (Exception e) {
System.out.println("Error: " + student +
" is already registered for " + cs1160);
}
try {
System.out.println(student + " is registering for CS 1180");
cs1180.register(student);
} catch (Exception e) {
System.out.println("Error: " + student +
" is already registered for " + cs1180);
}
try {
System.out.println(student + " is registering for CS 1160");
cs1160.register(student);
} catch (Exception e) {
System.out.println("Error: " + student +
" is already registered for " + cs1180);
}
try {
System.out.println(student + " is withdrawing from CS 1180");
cs1180.withdraw(student);
} catch (Exception e) {
System.out.println("Error: " + student +
" is not registered for " + cs1180);
}
try {
System.out.println(student + " is withdrawing from CS 1180");
cs1180.withdraw(student);
} catch (Exception e) {
System.out.println("Error: " + student +
" is not registered for " + cs1180);
}
}
public static Course getSmaller(Course c1, Course c2) {
if (c1.getNumStudents() < c2.getNumStudents()) {
return c1;
} else {
return c2;
}
}
}
× wa sier eris Dave se /viewContent/1504658/View e Mai1-Siemens, Dav Getting Started with Getting State with ×te hegg Study s- X x 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 file. one.zip 1. Write a class to represent a university course. The course should have the following data associated with it tle, 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: 00123456 is registering for Cs 1160 U0123456 is registering for cs 1180 U0123456 is registering for cs 1160 Brror: U0123456 is already registered for computer Science I (4) U012 3456 is withdrawing from CS 1180 U0123456 is withdrawing from CS 1180 Brror: 00123456 is not registered for Computer Science I (4) Problem 1 will be graded according to the following rubric: The class contains the requested data fields: 1 point is registering for Cs 1160Explanation / Answer
Course.java
package lab12;
import java.util.*;
public class Course {
@SuppressWarnings("rawtypes")
ArrayList<Comparable> course_1 = new ArrayList<Comparable>();
//ArrayList<Comparable> course_2 = new ArrayList<Comparable>();
String course_name;
@SuppressWarnings("unchecked")
public Course(String cname) {
course_name = cname;
//int course_id = i;
course_1.add(cname);
}
@SuppressWarnings("unchecked")
public boolean register(String student, String cname) {
if(cname.equals("CS 1160")){
if(course_1.equals(student)){
return false;
}
else
course_1.add(student);
return true;
}
else if(cname.equals("CS 1180")){
if(course_1.equals(student)){
return false;
}
else
course_1.add(student);
return true;
}
else
return false;
}
public void withdraw(String student, String cname) {
if(cname.equals("CS 1160"))
course_1.remove(student);
}
public int getNumStudentsCourse1() {
int course_1_num = course_1.size();
return course_1_num;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.