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.

Respuesta :

Answer:

File: clgCourse.java

public class clgCourse

{

  private final double CREDIT_HOUR_FEE = 120.00;

  private String dpt;

  private int courseNo;

  private int credits;

  private double courseFee;

  public clgCourse(String newdpt, int newcourseNo, int newCredits)

  {

      dpt = newdpt.toUpperCase();

      courseNo = newcourseNo;

      credits = newCredits;

      courseFee = CREDIT_HOUR_FEE * credits;

  }

  public String getdpt()

  {

      return dpt;

  }

  public int getcourseNo()

  {

      return courseNo;

  }

  public int getCredits()

  {

      return credits;

  }

  public double getcourseFee()

  {

      return courseFee;

  }

  public void display()

  {

      System.out.println("\n Course Data");

      System.out.println("Course: " + "College Course");

      System.out.println("dpt: " + this.getdpt());

      System.out.println("Course Number: " + this.getcourseNo());

      System.out.println("Credit Hours: " + this.getCredits());

      System.out.println("Course Fee: $" + this.getcourseFee());

  }

}

File: LabCourse.java

public class LabCourse extends clgCourse

{

  private final double LAB_FEE = 50.00;

  private double labCourseFee;

  public LabCourse(String newdpt, int newcourseNo, int newCredits)

  {

      super(newdpt, newcourseNo, newCredits);

      labCourseFee = super.getcourseFee() + LAB_FEE;

  }

  public double getLabCourseFee()

  {

      return labCourseFee;

  }

  public void display()

  {

      System.out.println("\n Course Data");

      System.out.println("Course: " + "Lab Course");

      System.out.println("dpt: " + super.getdpt());

      System.out.println("Course Number: " + super.getcourseNo());

      System.out.println("Credit Hours: " + super.getCredits());

      System.out.println("Course Fee: $" + this.getLabCourseFee());      

  }

}  

File: UseCourse.java

import java.util.Scanner;

public class UseCourse

{

  public static void main(String[] args)

  {

      Scanner keyboard = new Scanner(System.in);

      System.out.print("Enter the dpt of the course: ");

      String dept = keyboard.nextLine();

     

      System.out.print("Enter the number of the course: ");

      int number = keyboard.nextInt();

      System.out.print("Enter the credit hours of the course: ");

      int hours = keyboard.nextInt();

      if(dept.equalsIgnoreCase("BIO") || dept.equalsIgnoreCase("CHM")

              || dept.equalsIgnoreCase("CIS") || dept.equalsIgnoreCase("PHY"))

      {

          LabCourse labCourse = new LabCourse(dept, number, hours);

          labCourse.display();

      }

      else

      {

          clgCourse clgCourse = new clgCourse(dept, number, hours);

          clgCourse.display();

      }

      keyboard.close();

  }

}

Explanation:

  • Create a getdpt method that returns the dpt of the course .
  • Create a getcourseNo method that returns the number of the course.
  • Create a display method that displays all the data of college course .
  • Check if the user enters any of the lab dpts (BIO, CHM, CIS, or PHY),  then create a LabCourse and then display the course data .
  • Check if the user does not enter any of the lab dpts (BIO, CHM, CIS, or PHY),  then create a clgCourse and then display the course data .