50.5k views
5 votes
Methods : • constructor (): All of the fields are required as arguments to the constructor, except for the fee, which is computed at fee = credits * charge e.g., fee = 3 * $120 = $360). • The necessary set and get methods • display () method that displays the course data.

User Wynona
by
5.3k points

1 Answer

3 votes

Answer:

See explaination

Step-by-step explanation:

File: CollegeCourse.java

// CollegeCourse class implementation

public class CollegeCourse

{

// constant values

private final double FEE_PER_CREDIT_HOUR = 120.00;

// data fields

private String department;

private int courseNumber;

private int credits;

private double collegeCourseFee;

// constructor initializes all the data fields

public CollegeCourse(String newDepartment, int newCourseNumber, int newCredits)

{

department = newDepartment.toUpperCase();

courseNumber = newCourseNumber;

credits = newCredits;

collegeCourseFee = FEE_PER_CREDIT_HOUR * credits;

}

// getDepartment method returns the department of the course

public String getDepartment()

{

return department;

}

// getCourseNumber method returns the number of the course

public int getCourseNumber()

{

return courseNumber;

}

// getCredits method returns the credit hours of the course

public int getCredits()

{

return credits;

}

// getCollegeCourseFee method returns the fee of the college course

public double getCollegeCourseFee()

{

return collegeCourseFee;

}

// display method displays all the data of college course

public void display()

{

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

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

System.out.println("Department: " + this.getDepartment());

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

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

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

}

} // end of CollegeCourse class

File: LabCourse.java

// LabCourse class implementation

public class LabCourse extends CollegeCourse

{

// constant values

private final double LAB_FEE = 50.00;

// data fields

private double labCourseFee;

// constructor initializes all the data fields

public LabCourse(String newDepartment, int newCourseNumber, int newCredits)

{

super(newDepartment, newCourseNumber, newCredits);

labCourseFee = super.getCollegeCourseFee() + LAB_FEE;

}

// getCollegeCourseFee method returns the fee of the lab course

public double getLabCourseFee()

{

return labCourseFee;

}

// display method displays all the data of lab course

public void display()

{

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

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

System.out.println("Department: " + super.getDepartment());

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

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

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

}

} // end of LabCourse class

File: UseCourse.java

// UseCourse class implementation

import java.util.Scanner;

public class UseCourse

{

// start main method

public static void main(String[] args)

{

// create an object for Scanner class

Scanner keyboard = new Scanner(System.in);

// prompt the user to enter the department of the course

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

String dept = keyboard.nextLine();

// prompt the user to enter the number of the course

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

int number = keyboard.nextInt();

// prompt the user to enter the credit hours of the course

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"))

{

// if the user enters any of the lab departments (BIO, CHM, CIS, or PHY),

// then create a LabCourse and then display the course data

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

labCourse.display();

}

else

{

// if the user does not enter any of the lab departments (BIO, CHM, CIS, or PHY),

// then create a CollegeCourse and then display the course data

CollegeCourse collegeCourse = new CollegeCourse(dept, number, hours);

collegeCourse.display();

}

// close the Scanner object

keyboard.close();

}

} // end of UseCourse class

User Erasmortg
by
5.5k points