Create a class named College Course that includes data fields that hold the depa
ID: 3828059 • Letter: C
Question
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.
Explanation / Answer
public class CollegeCourse { protected String department; protected int courseNumber; protected int credits; protected float fee; public CollegeCourse(String department, int courseNumber, int credits){ this.department = department; this.courseNumber = courseNumber; this.credits = credits; this.fee = 120 * credits; } protected void display(){ String info = "Department : " + department + " " + "Course Type : " + "College Course" + " " + "Course Number : " + courseNumber + " " + "Credits : " + credits + " " + "Fee : " + fee; System.out.println(info); } } public class LabCourse extends CollegeCourse { public LabCourse(String department, int courseNumber, int credits) { super(department, courseNumber, credits); fee = fee + 50; } @Override protected void display() { String info = "Department : " + department + " " + "Course Type : " + "Lab Course" + " " + "Course Number : " + courseNumber + " " + "Credits : " + credits + " " + "Fee : " + fee; System.out.println(info); } } import java.util.Scanner; public class UseCourse { public CollegeCourse getCourse(String department, int courseNum, int credits){ CollegeCourse course = null; if("BIO".equalsIgnoreCase(department) || "CHM".equalsIgnoreCase(department) || "CIS".equalsIgnoreCase(department) || "PHY".equalsIgnoreCase(department)){ course = new LabCourse(department, courseNum, credits); }else{ course = new CollegeCourse(department, courseNum, credits); } return course; } public static void main(String[] args){ Scanner scanner = new Scanner(System.in); System.out.println("Please enter department"); String department = scanner.next(); System.out.println("Please enter course number"); int courseNum = scanner.nextInt(); System.out.println("Please enter number of credits"); int credits = scanner.nextInt(); UseCourse useCourse = new UseCourse(); CollegeCourse course = useCourse.getCourse(department, courseNum, credits); course.display(); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.