Create a class named College Course that includes data field
Create a class named College Course 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 Lab Course 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.
Solution
LabCourse.java
public class LabCourse extends CollegeCourse{
public LabCourse(String department, int courseNo, int credits){
super(department, courseNo, credits);
super.setFee(50);
}
public void display(){
System.out.println(\"Lab Course Department \"+getDepartment()+\" Course Number : \"+getCourseNo()+\" Credits :\"+getCredits()+\" Fee :$\"+getFee());
}
}
CollegeCourse.java
public class CollegeCourse {
private String department;
private int courseNo;
private int credits;
private int fee = 120;
public CollegeCourse(String department, int courseNo, int credits){
this.department = department;
this.courseNo = courseNo;
this.credits = credits;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public int getCourseNo() {
return courseNo;
}
public void setCourseNo(int courseNo) {
this.courseNo = courseNo;
}
public int getCredits() {
return credits;
}
public void setCredits(int credits) {
this.credits = credits;
}
public int getFee() {
return fee * getCredits();
}
public void setFee(int fee) {
this.fee = fee;
}
public void display(){
System.out.println(\"Department : \"+getDepartment()+\" Course Number : \"+getCourseNo()+\" Credits :\"+getCredits()+\" Fee :$\"+getFee());
}
}
UseCourse.java
import java.util.Scanner;
public class UseCourse {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(\"Enter Department :\");
String department = scan.next();
System.out.println(\"Enter Course Number :\");
int courseNo = scan.nextInt();
System.out.println(\"Enter Credits :\");
int credits = scan.nextInt();
if(department.equalsIgnoreCase(\"BIO\") || department.equalsIgnoreCase(\"CHM\") || department.equalsIgnoreCase(\"CIS\") ||department.equalsIgnoreCase(\"PHY\")){
LabCourse lab = new LabCourse(department, courseNo, credits);
lab.display();
}
else{
CollegeCourse col = new CollegeCourse(department, courseNo, credits);
col.display();
}
}
}
Output:
Enter Department :
BIO
Enter Course Number :
101
Enter Credits :
5
Lab Course Department BIO Course Number : 101 Credits :5 Fee :$250

