In: Computer Science
Create a class named CollegeCourse that includes the following data fields:
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. For example, if dept is 'SE', id is 225, and credits is 3, the output from display() should be:
SE225 Non-lab course 3.0 credits Total fee is $360.0
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. For example, if dept is 'BIO', id is 201, and credits is 4, the output from display() should be:
BIO201 Lab course 4.0 credits Lab fee is $50 Total fee is $530.0
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.
If you have any doubts, please give me comment...
import java.util.*;
public class CollegeCourse {
// attributes
String dept;
int id;
double credits;
double price;
// constructor
public CollegeCourse(String dept, int id, double credits){
this.dept = dept;
this.id = id;
this.credits = credits;
}
// display()
public void display(){
System.out.println(dept+id+"\nNon-lab course\n"+credits+" credits");
System.out.println("Total fee is $"+credits*120);
}
}
LabCourse.java
import java.util.*;
public class LabCourse extends CollegeCourse{
// attributes
private double courseFee;
// constructor
public LabCourse(String dept, int id, double credits, double courseFee){
super(dept, id, credits);
this.courseFee = courseFee;
}
// override display()
public void display(){
System.out.println(dept+id+"\nLab course\n"+credits+" credits");
System.out.println("Lab fee is $"+courseFee);
System.out.println("Total fee is $"+((credits*120)+courseFee));
}
}
UseCourse.java
import java.util.*;
public class UseCourse {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String dept, inStr;
String[] labCourses = { "BIO", "CHM", "CIS", "PHY" };
int id, credits;
int found = 0;
int x;
System.out.print("Enter course: ");
inStr = input.next();
System.out.print("Enter id: ");
id = input.nextInt();
System.out.print("Enter credits: ");
credits = input.nextInt();
for(int i=0; i<labCourses.length; i++){
if(inStr.equalsIgnoreCase(labCourses[i])){
found = 1;
break;
}
}
if(found==1){
LabCourse labCourse = new LabCourse(inStr, id, credits, 50);
labCourse.display();
}
else{
CollegeCourse collegeCourse = new CollegeCourse(inStr, id, credits);
collegeCourse.display();
}
}
}