Appearance
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:
modelNamemanufactureYearprivate vehicleIdThese must be initialized using a constructor and include appropriate getter and setter forvehicleId.
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 distancescheduleMaintenance()— prints maintenance message based on distance
- Extends
Demonstrate polymorphism by creating:
- A class
MaintenanceServicewith methodperformService(Vehicle v) - Another class overriding this to handle Car-specific servicing
- A class
In
main():- Create a
Carobject - Log two distance updates using
addDistance() - Display status
- Perform maintenance
- Create a
l. Online Course Enrollment System (Java Program)
Create a base class Person with:
nameemailprivate 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 completedresetProgress()
- Inherits
Add polymorphism:
- Base class:
Coursewith methodgetCourseType() - Subclasses:
ProgrammingCourse,MathCourse(override method)
- Base class:
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:
petNamespeciesprivate petIdThese must be initialized using a constructor and include appropriate getter and setter forpetId.
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 countresetActivities()
- Extends
Demonstrate polymorphism by creating:
- A base class
Activitywith methodactivityType() - Subclasses
WalkingActivity,FeedingActivitythat overrideactivityType()
- A base class
In
main():- Create a
Dogobject - Record two activities using
logActivity() - Display profile
- Reset activity count
- Show polymorphism using
activityType()
- Create a
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 forpersonId.
Create a subclass Student that:
- Extends
Person - Adds attributes
totalClassesandattendedClasses(int), both initialized to 0 in the constructor - Implements:
addClass(int n)— increasestotalClassesby nattendClass(int n)— increasesattendedClassesby n (but never beyondtotalClasses)getAttendancePercentage()— returns attendance percentage asdoubleshowStatus()— prints student's name, admission category ("Senior" ifyearOfAdmission <= 2022, otherwise "Junior"), attendance percentage, and eligibility ("Eligible for exam" if >= 75%, otherwise "Not eligible")
- Extends
Demonstrate polymorphism by creating:
- A class
ReportServicewith methodgenerateReport(Person p)that prints a general message - A subclass
StudentReportServicethat extendsReportServiceand overridesgenerateReport()to downcast and print detailed attendance report ifpis aStudent, otherwise print a generic message
- A class
In
main():- Create a
Studentobject - Use
addClass()andattendClass()at least twice to update totals - Call
showStatus() - Create a
StudentReportServiceobject and callgenerateReport()with the student object to demonstrate polymorphism
- Create a
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 foritemId.
Create a subclass Book that:
- Extends
LibraryItem - Adds attributes
borrowCount(int, initialized to 0) andisAvailable(boolean, initiallytrue) - Implements:
borrow()— ifisAvailableistrue, incrementborrowCount, setisAvailabletofalse, and print "Book borrowed successfully"; otherwise print "Book is currently unavailable"returnBook()— setsisAvailabletotrueand prints "Book returned"showStatus()— prints title, age category ("Classic" ifpublishYear < 2000, otherwise "Modern"), totalborrowCount, and availability ("Available" / "Not Available")needsReplacement()— prints "Replacement suggested" ifborrowCount >= 50, otherwise "No replacement needed yet"
- Extends
Demonstrate polymorphism by creating:
- A class
LibraryServicewith methodprocessItem(LibraryItem item)that prints a general message - A subclass
BookServicethat extendsLibraryServiceand overridesprocessItem()to callshowStatus()andneedsReplacement()if item is aBook, otherwise print a generic message
- A class
In
main():- Create a
Bookobject - Call
borrow(),returnBook(), andborrow()again to change state - Call
showStatus()andneedsReplacement() - Create a
BookServiceobject and callprocessItem()with the Book object to demonstrate polymorphism
- Create a
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 formemberId.
Create a subclass GymMember that:
- Extends
Member - Adds attributes
totalWorkoutHours(double, initialized to 0.0) andmembershipType(String, e.g., "Regular" or "Premium") - Implements:
addWorkout(double hours)— increasestotalWorkoutHoursby hours (ignore invalid negatives)showStatus()— prints member name, membership duration category ("Long-term" if(currentYear - joinYear) >= 2, else "New"; assumecurrentYearis 2025), membership type, and total workout hourssuggestPlan()— prints "Excellent engagement" iftotalWorkoutHours >= 200, "Good, keep going" if between 100 and 199, "Need more regular workouts" otherwise
- Extends
Demonstrate polymorphism by creating:
- A class
MembershipServicewith methodhandleMember(Member m)that prints "Handling generic member..." - A subclass
GymMembershipServicethat extendsMembershipServiceand overrideshandleMember()to downcast and callshowStatus()andsuggestPlan()ifmis aGymMember, otherwise print a simple message
- A class
In
main():- Create a
GymMemberobject with some initial data - Call
addWorkout()at least twice with different hour values - Call
showStatus()andsuggestPlan() - Create a
GymMembershipServiceobject and callhandleMember()with the GymMember object to demonstrate polymorphism
- Create a
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 forcourseCode.
Create a subclass ProgrammingCourse that:
- Extends
Course - Adds attributes
completedModules(int, initialized to 0) anddifficultyLevel(String, e.g., "Beginner", "Intermediate", "Advanced") - Implements:
completeModule(int n)— increasescompletedModulesby n, but never lets it exceedtotalModulesgetCompletionPercentage()— returns completion percentage asdoubleshowStatus()— 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
- Extends
Demonstrate polymorphism by creating:
- A class
CourseServicewith methodreviewCourse(Course c)that prints "Reviewing generic course..." - A subclass
ProgrammingCourseServicethat extendsCourseServiceand overridesreviewCourse()to downcast and callshowStatus()andsuggestRevision()ifcis aProgrammingCourse, otherwise print a generic message
- A class
In
main():- Create a
ProgrammingCourseobject - Call
completeModule()multiple times to simulate progress - Call
showStatus()andsuggestRevision() - Create a
ProgrammingCourseServiceobject and callreviewCourse()with the course object to demonstrate polymorphism
- Create a
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
- Class vs Objects
- Encapsulation vs Abstraction
- Method overloading vs overriding
- Inheritance vs Composition
- Compile-time vs Run-time Polymorphism
IUS Preps - Your Academic Success Partner