In Java create a class named College Course that includes da
In Java 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
UseCourse:
import javax.swing.JOptionPane;
public class UseCourse
{
public UseCourse()
{
CollegeCourse college;
// Collect all the necessary data first to determine whether it is a lab course or not.
String dept = JOptionPane.showInputDialog(null, \"Enter Department\");
String courseNumString = JOptionPane.showInputDialog(null, \"Enter Course Number\");
int courseNum = Integer.parseInt(courseNumString);
String credString = JOptionPane.showInputDialog(null, \"Enter Credits\");
int cred = Integer.parseInt(credString);
// Now that we have the info we can ask the question.
if (dept.equals(\"BIO\") | dept.equals(\"CHM\") | dept.equals(\"CIS\") | dept.equals(\"PHY\"))
{college = new LabCourse(dept, courseNum, cred);}
else {college = new LabCourse(dept, courseNum, cred);}
college.display();
}
public static void main(String[] args)
{new UseCourse();}
CollegeCourse:
import javax.swing.JOptionPane;
public class CollegeCourse {
protected String dept;
protected int courseNum;
protected int cred;
protected int fee;
protected int Charge = 120;
protected int labfee;
public CollegeCourse(String dept, int courseNum, int cred)
{
this.dept = dept;
this.courseNum = courseNum;
this.cred = cred;
fee = cred * Charge;
}
public void display(){
JOptionPane.showMessageDialog(null, dept + courseNum +
\"\ Non-lab Course\" + \"\ \" + cred +\" Credits\" + \"\ Total fee is $\" + fee);
}
}
LabCourse:
import javax.swing.JOptionPane;
public class LabCourse extends CollegeCourse
{
public LabCourse(String dept, int courseNum, int cred)
{super(dept, courseNum, cred);}
public void display()
{
fee += 50;
JOptionPane.showMessageDialog(null, dept + courseNum +
\"\ Lab Course\" + \"\ \" + cred +\" Credits\" + \"\ Total fee is $\" + fee);
}
}

