OOP Coding Questions v2
OOP Coding Questions v2
2. Write a Java program to create a class called "Dog" with a name and breed attribute. Create two instances of
the "Dog" class, set their attributes using the constructor and modify the attributes using the setter methods and
print the updated values.
// Constructor
public Dog(String name, String breed) {
this.name = name;
this.breed = breed;
}
// Setter methods
public void setName(String name) {
this.name = name;
}
dog1.printDetails();
dog2.printDetails();
}
}
3. Write a Java program to create a class called "Rectangle" with width and height attributes. Calculate the area
and perimeter of the rectangle.
// Constructor
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
4. Write a Java program to create a class called "Circle" with a radius attribute. You can access and modify this
attribute. Calculate the area and circumference of the circle.
import java.util.ArrayList;
// Constructor
public Book(String title, String author, String isbn) {
this.title = title;
this.author = author;
this.isbn = isbn;
}
// Adding books
Book book1 = new Book("1984", "George Orwell", "123456789");
Book book2 = new Book("To Kill a Mockingbird", "Harper Lee", "987654321");
library.add(book1);
library.add(book2);
// Removing a book
library.remove(book1);
6. Write a Java program to create a class called "Employee" with a name, job title, and salary attributes, and
methods to calculate and update salary.
import java.util.ArrayList;
class Account {
private String accountNumber;
private String accountHolder;
private double balance;
// Constructor
public Account(String accountNumber, String accountHolder, double balance) {
this.accountNumber = accountNumber;
this.accountHolder = accountHolder;
this.balance = balance;
}
bank.addAccount(acc1);
bank.addAccount(acc2);
acc1.deposit(500);
acc2.withdraw(300);
bank.printAccounts();
}
}
8. Write a Java program to create class called "TrafficLight" with attributes for color and duration, and methods
to change the color and check for red or green.
// Constructor
public TrafficLight(String color, int duration) {
this.color = color;
this.duration = duration;
}
9. Write a Java program to create a class called "Employee" with a name, salary, and hire date attributes, and a
method to calculate years of service.
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
// Constructor
this.name = name;
this.salary = salary;
this.hireDate = hireDate;
emp.printDetails();
10. Write a Java program to create a class called "Student" with a name, grade, and courses attributes, and
methods to add and remove courses.
import java.util.ArrayList;
// Constructor
public Student(String name, String grade) {
this.name = name;
this.grade = grade;
this.courses = new ArrayList<>();
}
import java.util.ArrayList;
class Book {
private String title;
private String author;
// Constructor
public Book(String title, String author) {
this.title = title;
this.author = author;
}
library.addBook(book1);
library.addBook(book2);
library.printBooks();
}
}
12. Write a Java program to create a class called "Airplane" with a flight number, destination, and departure
time attributes, and methods to check flight status and delay.
import java.time.LocalDateTime;
// Constructor
public Airplane(String flightNumber, String destination, LocalDateTime departureTime) {
this.flightNumber = flightNumber;
this.destination = destination;
this.departureTime = departureTime;
this.delayed = false;
}
13. Write a Java program to create a class called "Inventory" with a collection of products and methods to add
and remove products, and to check for low inventory.
import java.util.ArrayList;
class Product {
private String id;
private String name;
private int quantity;
// Constructor
public Product(String id, String name, int quantity) {
this.id = id;
this.name = name;
this.quantity = quantity;
}
@Override
public String toString() {
return "Product: " + name + ", Quantity: " + quantity;
}
}
inventory.addProduct(product1);
inventory.addProduct(product2);
inventory.checkLowInventory();
}
}
14. Write a Java program to create a class called "School" with attributes for students, teachers, and classes, and
methods to add and remove students and teachers, and to create classes.
import java.util.ArrayList;
class Student {
private String name;
public Student(String name) { this.name = name; }
public String getName() { return name; }
}
class Teacher {
private String name;
public Teacher(String name) { this.name = name; }
public String getName() { return name; }
}
15. Write a Java program to create a class called "MusicLibrary" with a collection of songs and methods to add
and remove songs, and to play a random song.
import java.util.ArrayList;
import java.util.Random;
class Song {
private String title;
public Song(String title) { this.title = title; }
public String getTitle() { return title; }
}
import java.util.ArrayList;
class Movie {
private String title;
private String director;
private ArrayList<String> reviews = new ArrayList<>();
import java.util.HashMap;
import java.util.ArrayList;
class ReservationSystem {
private ArrayList<String> flights = new ArrayList<>();
private ArrayList<String> hotels = new ArrayList<>();
20. Write a Java program to create a class called "BankAccount" with attributes for account number, account
holder's name, and balance. Include methods for depositing and withdrawing money, as well as checking the
balance. Create a subclass called "SavingsAccount" that adds an interest rate attribute and a method to apply
interest.
class BankAccount {
private String accountNumber;
private String holderName;
private double balance;
class Vehicle {
private String make;
private String model;
private int year;
@Override
public void printDetails() {
super.printDetails();
System.out.println("Trunk Size: " + trunkSize + " cu ft");
}
}
@Override
public void printDetails() {
super.printDetails();
System.out.println("Payload Capacity: " + payloadCapacity + " kg");
}
car.printDetails();
truck.printDetails();
}
}
22. Write a Java program to create a class called "Customer" with attributes for name, email, and purchase
history. Implement methods to add purchases to the history and calculate total expenditure. Create a subclass
"LoyalCustomer" that adds a discount rate attribute and a method to apply the discount.
import java.util.ArrayList;
class Customer {
private String name;
private String email;
private ArrayList<Double> purchaseHistory = new ArrayList<>();
23. 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.
class Course {
private String courseName;
private String instructor;
private int credits;
public OnlineCourse(String courseName, String instructor, int credits, String platform, int duration) {
super(courseName, instructor, credits);
this.platform = platform;
this.duration = duration;
}
@Override
public void displayCourseDetails() {
super.displayCourseDetails();
System.out.println("Platform: " + platform + ", Duration: " + duration + " hours");
}
class ElectronicsProduct {
private String productID;
private String name;
private double price;
25. Write a Java program to create a class called "Building" with attributes for address, number of floors, and
total area. Create subclasses "ResidentialBuilding" and "CommercialBuilding" that add specific attributes like
number of apartments for residential and office space for commercial buildings. Implement a method to
calculate the total rent for each subclass.
class Building {
private String address;
private int numberOfFloors;
private double totalArea;
26. Write a Java program to create a class called "Event" with attributes for event name, date, and location.
Create subclasses "Seminar" and "MusicalPerformance" that add specific attributes like number of speakers for
seminars and performer list for concerts. Implement methods to display event details and check for conflicts in
the event schedule.
class Building {
private String address;
private int numberOfFloors;
private double totalArea;
class CustomerOrder {
private String orderId;
private String customer;
private String orderDate;
public OnlineOrder(String orderId, String customer, String orderDate, String deliveryAddress, String
trackingNumber) {
super(orderId, customer, orderDate);
this.deliveryAddress = deliveryAddress;
this.trackingNumber = trackingNumber;
}
class Reservation {
private String reservationId;
private String customerName;
private String date;
class Pet {
private String name;
private String species;
private int age;
class Book {
private String title;
private String author;
private double price;
@Override
public void displayDetails() {
super.displayDetails();
System.out.println("Genre: " + genre);
}
}
@Override
public void displayDetails() {
super.displayDetails();
System.out.println("Field: " + field);
}
fiction.displayDetails();
nonFiction.displayDetails();
}
}