Java Program to Manage Course Details and Online Course Features
Java OOP: Exercise-23 with Solution
Write a Java program to create a class called "Course" with attributes for course name, instructor, and credits. Create a subclass "OnlineCourse" that adds attributes for platform and duration. Implement methods to display course details and check if the course is eligible for a certificate based on duration.
Sample Solution:
Java Code:
Course.java
// Define the Course class
public class Course {
// Attributes for the course name, instructor, and credits
private String courseName;
private String instructor;
private int credits;
// Constructor to initialize the Course object
public Course(String courseName, String instructor, int credits) {
this.courseName = courseName;
this.instructor = instructor;
this.credits = credits;
}
// Method to display course details
public void displayCourseDetails() {
System.out.println("Course Name: " + courseName);
System.out.println("Instructor: " + instructor);
System.out.println("Credits: " + credits);
}
// Getter for course name
public String getCourseName() {
return courseName;
}
// Getter for instructor
public String getInstructor() {
return instructor;
}
// Getter for credits
public int getCredits() {
return credits;
}
}
Explanation:
Course Class:
- Attributes: courseName, instructor, and credits.
- Constructor: Initializes the attributes.
- displayCourseDetails(): Prints the course details.
- Getters: Methods to get the values of the attributes.
OnlineCourse.java
// Define the OnlineCourse subclass that extends the Course class
class OnlineCourse extends Course {
// Additional attributes for the platform and duration
private String platform;
private int duration; // duration in hours
// Constructor to initialize the OnlineCourse object
public OnlineCourse(String courseName, String instructor, int credits, String platform, int duration) {
super(courseName, instructor, credits); // Call the superclass constructor
this.platform = platform;
this.duration = duration;
}
// Method to display course details, including platform and duration
@Override
public void displayCourseDetails() {
super.displayCourseDetails(); // Call the superclass method to display common details
System.out.println("Platform: " + platform);
System.out.println("Duration: " + duration + " hours");
}
// Method to check if the course is eligible for a certificate based on duration
public boolean isEligibleForCertificate() {
// Assume that a course is eligible for a certificate if its duration is at least 10 hours
return duration >= 10;
}
// Getter for platform
public String getPlatform() {
return platform;
}
// Getter for duration
public int getDuration() {
return duration;
}
}
Explanation:
OnlineCourse Class:
- Extends Course.
- Additional Attributes: platform and duration.
- Constructor: Initializes the attributes, calling the superclass constructor for the common attributes.
- displayCourseDetails(): Overridden to include additional details specific to online courses.
- isEligibleForCertificate(): Checks if the course duration is at least 10 hours to be eligible for a certificate.
- Getters: Methods to get the values of the additional attributes.
Main.java
// Main class to test the Course and OnlineCourse classes
public class Main {
public static void main(String[] args) {
// Create a Course object
Course course = new Course("Java Programming", "Dr. Timaios Pliny", 4);
course.displayCourseDetails();
System.out.println();
// Create an OnlineCourse object
OnlineCourse onlineCourse = new OnlineCourse("Advanced Java", "Prof. Isacco Lyuba", 4, "Google", 10);
onlineCourse.displayCourseDetails();
System.out.println("Eligible for Certificate: " + onlineCourse.isEligibleForCertificate());
}
}
Explanation:
Main Class:
- Creates instances of Course and OnlineCourse and demonstrates the usage of their methods.
Output:
Course Name: Java Programming Instructor: Dr. Timaios Pliny Credits: 4 Course Name: Advanced Java Instructor: Prof. Isacco Lyuba Credits: 4 Platform: Google Duration: 10 hours Eligible for Certificate: true
Java Code Editor:
Improve this sample solution and post your code through Disqus.
Java OOP Previous: Java Program to Manage Customer Purchases with Discounts.
Java OOP Next: Java Program to Manage Electronics Products and Washing Machines.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-exercises/oop/java-oop-exercise-23.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics