In: Computer Science
Create a class named CollegeCourse that includes data fields that hold the department (for example, ENG), the course number (for example, 101), the credits (for example, 3), and the fee for the course (for example, $360). All of the fields are required as arguments to the constructor, except for the fee, which is calculated at $120 per credit hour. Include a display() method that displays the course data. Create a subclass named LabCourse that adds $50 to the course fee. Override the parent class display() method to indicate that the course is a lab course and to display all the data. Write an application named UseCourse that prompts the user for course information. If the user enters a class in any of the following departments, create a LabCourse: BIO, CHM, CIS, or PHY. If the user enters any other department, create a CollegeCourse that does not include the lab fee. Then display the course data. Save the files as CollegeCourse.java, LabCourse.java, and UseCourse.java.
PLEASE CODE IN JAVA!!
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// CollegeCourse.java
public class CollegeCourse {
// attributes
private String department;
private int courseNum;
private int credits;
protected int courseFee; // made protected, because it needs to be accessed
// in LabCourse
// constant for storing fee per credit hour
private static final int FEE_PER_CREDIT_HOUR = 120;
// constructor taking all values except course fee
public CollegeCourse(String department, int courseNum, int credits) {
this.department = department;
this.courseNum = courseNum;
this.credits = credits;
// finding course fee and assigning
this.courseFee = credits * FEE_PER_CREDIT_HOUR;
}
//method to display all data
public void display() {
System.out.println("Course Details:");
System.out.println("Department: " + department);
System.out.println("Course Number: " + courseNum);
System.out.println("Credits: " + credits);
System.out.println("Course Fee: $" + courseFee);
}
}
// LabCourse.java
public class LabCourse extends CollegeCourse {
// constant for storing lab fee
private static final int LAB_FEE = 50;
// constructor taking all values except course fee
public LabCourse(String department, int courseNum, int credits) {
// passing values to super class constructor
super(department, courseNum, credits);
// adding 50 to course fee as lab fee
courseFee += LAB_FEE;
}
@Override
public void display() {
// displaying 'This is a Lab Course'
System.out.println("This is a Lab Course");
// invoking super class display() method to display all data
super.display();
}
}
// UseCourse.java
import java.util.Scanner;
public class UseCourse {
public static void main(String[] args) {
// scanner for input
Scanner scanner = new Scanner(System.in);
// initializing a CollegeCourse (super class reference) to null
CollegeCourse course = null;
// prompting and reading inputs
System.out.print("Enter course department: ");
String dept = scanner.next();
System.out.print("Enter course number: ");
int courseNum = scanner.nextInt();
System.out.print("Enter credits: ");
int credits = scanner.nextInt();
// finding category of department
if (dept.equalsIgnoreCase("BIO") || dept.equalsIgnoreCase("CHM")
|| dept.equalsIgnoreCase("CIS") || dept.equalsIgnoreCase("PHY")) {
// creating a LabCourse and assigning to course
course = new LabCourse(dept, courseNum, credits);
} else {
// creating a CollegeCourse and assigning to course
course = new CollegeCourse(dept, courseNum, credits);
}
// displaying info
course.display();
}
}
/*OUTPUT (two runs)*/
Enter course department: PHY
Enter course number: 101
Enter credits: 3
This is a Lab Course
Course Details:
Department: PHY
Course Number: 101
Credits: 3
Course Fee: $410
Enter course department: HUI
Enter course number: 187
Enter credits: 2
Course Details:
Department: HUI
Course Number: 187
Credits: 2
Course Fee: $240