Exercise 13: Course Class Project Name: A1 E13 YourLastName YourFirstName Write
ID: 3814867 • Letter: E
Question
Exercise 13: Course Class Project Name: A1 E13 YourLastName YourFirstName Write the implementation of the class Course according to the requirements listed below. Data fields o A String courseDept that stores the course department ("CS", "Math etc.. o An int courseNo that stores the course number (170, 150, etc.). o An int courseUnits that stores the number of units for the course. o Remember that data fields are always private. Default constructor o Initializes all data fields to default values. overloaded constructor o Parameters a string storing the course department, an int storing the course number, and an int storing the number of units. o Initializes all data fields to the given values. Method getCourseDept o Returns the course department. Method getCourseNumber o Returns the course number. Method get CourseUnits o Returns the number of units for the course. Method setCourseDept o Parameter: a String storing a course department. o Re-sets the course department of the calling object with the value passed by the parameter. Method set Course Number o Parameter: an int storing a course number. o Re-sets the course number of the calling object with the value passed by the parameter. Method setCourseUnits o Parameter an int storing the units for the course o Re-sets the course units of the calling object with the value passed by the parameter. Method compareUnits o Parameter: an object of the Course class. o A Boolean method that returns true if the calling object has the same number of units of the parameter object, and false otherwise. Method printcourselnfo o Prints the course information in the following format: CS 170 (4 units)Explanation / Answer
Course.java
public class Course {
//Declaring instance variables
private String courseDept;
private int courseNo;
private int courseUnits;
//Zero arguments constructor
public Course() {
super();
this.courseDept = "N/A";
this.courseNo = 0;
this.courseUnits = 0;
}
//Parameterized constructor
public Course(String courseDept, int courseNo, int courseUnits) {
super();
this.courseDept = courseDept;
this.courseNo = courseNo;
this.courseUnits = courseUnits;
}
//getters and setters
public String getCourseDept() {
return courseDept;
}
public void setCourseDept(String courseDept) {
this.courseDept = courseDept;
}
public int getCourseNo() {
return courseNo;
}
public void setCourseNo(int courseNo) {
this.courseNo = courseNo;
}
public int getCourseUnits() {
return courseUnits;
}
public void setCourseUnits(int courseUnits) {
this.courseUnits = courseUnits;
}
//This method comparing two courses
boolean compareUnits(Course cs)
{
if(this.courseUnits==cs.getCourseUnits())
return true;
else
return false;
}
//this method will print the course class object info
public void printCourseInfo()
{
System.out.println("The "+courseNo+" course, "+courseDept+" department, has "+courseUnits+" units.");
}
}
____________________
CourseTester.java
public class CourseTester {
public static void main(String[] args) {
//Creating the Course class objects by passing values as arguments
Course course1=new Course("CS",170,4);
Course course2=new Course("CS",150,3);
Course course3=new Course();
//Displaying the Course class info
course1.printCourseInfo();
course2.printCourseInfo();
course3.printCourseInfo();
//Creating the Course class objects by passing values as arguments
Course course4=new Course("Math",200,5);
//Comparing the two Course class units
boolean bool=course1.compareUnits(course2);
if(bool)
System.out.println("The Two "+course1.getCourseDept()+" have the same units");
else
System.out.println("The Two "+course1.getCourseDept()+" do not have the same units");
}
}
_____________________
Output:
The 170 course, CS department, has 4 units.
The 150 course, CS department, has 3 units.
The 0 course, N/A department, has 0 units.
The Two CS do not have the same units
____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.