Skip to content

Questions

1. What is:

  • Class
  • Object
  • Method
  • Constructor
  • Method Overloading
  • Method Overriding
  • Constructor Overloading
  • Encapsulation
  • Inheritance
  • Polymorphism

2. Analyze the Scenarios Below

k. Vehicle Maintenance Tracker (Java Program)

Design a Java program with the following requirements:

  • Create a base class Vehicle with encapsulated attributes:

    • modelName
    • manufactureYear
    • private vehicleId These must be initialized using a constructor and include appropriate getter and setter for vehicleId.
  • Create a subclass Car that:

    • Extends Vehicle
    • Adds attribute totalDistance (double), initialized to 0 in the constructor
    • Implements:
      • addDistance(double km)
      • showStatus() — shows model, age category (old/new), total distance
      • scheduleMaintenance() — prints maintenance message based on distance
  • Demonstrate polymorphism by creating:

    • A class MaintenanceService with method performService(Vehicle v)
    • Another class overriding this to handle Car-specific servicing
  • In main():

    • Create a Car object
    • Log two distance updates using addDistance()
    • Display status
    • Perform maintenance

l. Online Course Enrollment System (Java Program)

  • Create a base class Person with:

    • name
    • email
    • private userId (with encapsulated getter & setter)
  • Create subclass Student that:

    • Inherits Person
    • Adds completedCourses = 0
    • Implements:
      • completeCourse()
      • displayStudentInfo() — name, learning level (Beginner / Intermediate / Advanced), total courses completed
      • resetProgress()
  • Add polymorphism:

    • Base class: Course with method getCourseType()
    • Subclasses: ProgrammingCourse, MathCourse (override method)
  • In main():

    • Create a Student
    • Complete two courses
    • Display updated info
    • Show course type using polymorphism

m. Smart Pet Monitoring System (Java Program)

Design a Java program with the following requirements:

  • Create a base class Pet with encapsulated attributes:

    • petName
    • species
    • private petId These must be initialized using a constructor and include appropriate getter and setter for petId.
  • Create a subclass Dog that:

    • Extends Pet
    • Adds attribute totalActivities (int), initialized to 0 in the constructor
    • Implements:
      • logActivity(int count)
      • showDogProfile() — shows dog's name, species category (small/medium/large), activity count
      • resetActivities()
  • Demonstrate polymorphism by creating:

    • A base class Activity with method activityType()
    • Subclasses WalkingActivity, FeedingActivity that override activityType()
  • In main():

    • Create a Dog object
    • Record two activities using logActivity()
    • Display profile
    • Reset activity count
    • Show polymorphism using activityType()

n. Student Attendance Tracker (Java Program)

Design a Java program with the following requirements:

  • Create a base class Person with encapsulated attributes:

    • name (String)
    • yearOfAdmission (int)
    • private personId (String) These must be initialized using a constructor and include appropriate getter and setter for personId.
  • Create a subclass Student that:

    • Extends Person
    • Adds attributes totalClasses and attendedClasses (int), both initialized to 0 in the constructor
    • Implements:
      • addClass(int n) — increases totalClasses by n
      • attendClass(int n) — increases attendedClasses by n (but never beyond totalClasses)
      • getAttendancePercentage() — returns attendance percentage as double
      • showStatus() — prints student's name, admission category ("Senior" if yearOfAdmission <= 2022, otherwise "Junior"), attendance percentage, and eligibility ("Eligible for exam" if >= 75%, otherwise "Not eligible")
  • Demonstrate polymorphism by creating:

    • A class ReportService with method generateReport(Person p) that prints a general message
    • A subclass StudentReportService that extends ReportService and overrides generateReport() to downcast and print detailed attendance report if p is a Student, otherwise print a generic message
  • In main():

    • Create a Student object
    • Use addClass() and attendClass() at least twice to update totals
    • Call showStatus()
    • Create a StudentReportService object and call generateReport() with the student object to demonstrate polymorphism

o. Library Book Borrowing System (Java Program)

Design a Java program with the following requirements:

  • Create a base class LibraryItem with encapsulated attributes:

    • title (String)
    • publishYear (int)
    • private itemId (String) These must be initialized using a constructor and include appropriate getter and setter for itemId.
  • Create a subclass Book that:

    • Extends LibraryItem
    • Adds attributes borrowCount (int, initialized to 0) and isAvailable (boolean, initially true)
    • Implements:
      • borrow() — if isAvailable is true, increment borrowCount, set isAvailable to false, and print "Book borrowed successfully"; otherwise print "Book is currently unavailable"
      • returnBook() — sets isAvailable to true and prints "Book returned"
      • showStatus() — prints title, age category ("Classic" if publishYear < 2000, otherwise "Modern"), total borrowCount, and availability ("Available" / "Not Available")
      • needsReplacement() — prints "Replacement suggested" if borrowCount >= 50, otherwise "No replacement needed yet"
  • Demonstrate polymorphism by creating:

    • A class LibraryService with method processItem(LibraryItem item) that prints a general message
    • A subclass BookService that extends LibraryService and overrides processItem() to call showStatus() and needsReplacement() if item is a Book, otherwise print a generic message
  • In main():

    • Create a Book object
    • Call borrow(), returnBook(), and borrow() again to change state
    • Call showStatus() and needsReplacement()
    • Create a BookService object and call processItem() with the Book object to demonstrate polymorphism

p. Fitness Center Membership Tracker (Java Program)

Design a Java program with the following requirements:

  • Create a base class Member with encapsulated attributes:

    • memberName (String)
    • joinYear (int)
    • private memberId (String) These must be initialized using a constructor and include appropriate getter and setter for memberId.
  • Create a subclass GymMember that:

    • Extends Member
    • Adds attributes totalWorkoutHours (double, initialized to 0.0) and membershipType (String, e.g., "Regular" or "Premium")
    • Implements:
      • addWorkout(double hours) — increases totalWorkoutHours by hours (ignore invalid negatives)
      • showStatus() — prints member name, membership duration category ("Long-term" if (currentYear - joinYear) >= 2, else "New"; assume currentYear is 2025), membership type, and total workout hours
      • suggestPlan() — prints "Excellent engagement" if totalWorkoutHours >= 200, "Good, keep going" if between 100 and 199, "Need more regular workouts" otherwise
  • Demonstrate polymorphism by creating:

    • A class MembershipService with method handleMember(Member m) that prints "Handling generic member..."
    • A subclass GymMembershipService that extends MembershipService and overrides handleMember() to downcast and call showStatus() and suggestPlan() if m is a GymMember, otherwise print a simple message
  • In main():

    • Create a GymMember object with some initial data
    • Call addWorkout() at least twice with different hour values
    • Call showStatus() and suggestPlan()
    • Create a GymMembershipService object and call handleMember() with the GymMember object to demonstrate polymorphism

q. Online Course Progress Tracker (Java Program)

Design a Java program with the following requirements:

  • Create a base class Course with encapsulated attributes:

    • courseName (String)
    • totalModules (int)
    • private courseCode (String) These must be initialized using a constructor and include appropriate getter and setter for courseCode.
  • Create a subclass ProgrammingCourse that:

    • Extends Course
    • Adds attributes completedModules (int, initialized to 0) and difficultyLevel (String, e.g., "Beginner", "Intermediate", "Advanced")
    • Implements:
      • completeModule(int n) — increases completedModules by n, but never lets it exceed totalModules
      • getCompletionPercentage() — returns completion percentage as double
      • showStatus() — prints course name, difficulty level, completion percentage, and learning status ("On track" if percentage >= 60, "Needs attention" otherwise)
      • suggestRevision() — prints "Revise previous modules" if completion percentage is between 40 and 70, "Focus on practice problems" if > 70, "Start with basics again" if < 40
  • Demonstrate polymorphism by creating:

    • A class CourseService with method reviewCourse(Course c) that prints "Reviewing generic course..."
    • A subclass ProgrammingCourseService that extends CourseService and overrides reviewCourse() to downcast and call showStatus() and suggestRevision() if c is a ProgrammingCourse, otherwise print a generic message
  • In main():

    • Create a ProgrammingCourse object
    • Call completeModule() multiple times to simulate progress
    • Call showStatus() and suggestRevision()
    • Create a ProgrammingCourseService object and call reviewCourse() with the course object to demonstrate polymorphism

3. Solve the following problems using Java

(Use Class, Object, Constructor, Method, Single-Level Inheritance, Multi-Level Inheritance where appropriate.)

a. Program to check leap year b. Program to find largest among three numbers c. Program to print Fibonacci series up to N d. Program to calculate area of a circle e. Program to print all primes up to N

4. Identify the Errors and Show Output

java
public class Book {
    String tittle;
    string author;
    int pages;

    Book(String t, String a, int p {
        title = t;
        author = author;
        pages = pg;
    }

    void displayDetails() {
        System.out.println("Title: " + Title);
        System.out.println("Author: " + author)
        System.out.println("Pages: " + pages);
    }

    public boolean isBigBook() {
        if (pages > "300") {
            return "True";
        }
        return false;
    }
}

5. Comparison questions

  1. Class vs Objects
  2. Encapsulation vs Abstraction
  3. Method overloading vs overriding
  4. Inheritance vs Composition
  5. Compile-time vs Run-time Polymorphism

IUS Preps - Your Academic Success Partner

Educational resources for IUS students.