Java OOP Practice Questions PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document contains examples of Java OOP programing questions relating to classes such as Student, Rectangle, BankAccount, and more. The provided code demonstrates object-oriented programming principles and solutions. It serves as a good reference for practicing Java programming concepts.
Full Transcript
Java OOP Practice Questions and Solutions 1. Create a class 'Student' with attributes name, age, and grade. Implement methods to display student information and set new values for each attribute. public class Student { private String name; private int age; private char grade;...
Java OOP Practice Questions and Solutions 1. Create a class 'Student' with attributes name, age, and grade. Implement methods to display student information and set new values for each attribute. public class Student { private String name; private int age; private char grade; public Student(String name, int age, char grade) { this.name = name; this.age = age; this.grade = grade; } public void displayInfo() { System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Grade: " + grade); } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setGrade(char grade) { this.grade = grade; } } 2. Create a class 'Rectangle' with attributes length and width. Include methods to calculate area and perimeter. public class Rectangle { private double length; private double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } public double calculateArea() { return length * width; } public double calculatePerimeter() { return 2 * (length + width); } } 3. Create a class 'BankAccount' with attributes accountNumber, balance, and accountHolderName. Implement methods for deposit, withdrawal, and balance inquiry. public class BankAccount { private String accountNumber; private double balance; private String accountHolderName; public BankAccount(String accountNumber, double initialBalance, String accountHolderName) { this.accountNumber = accountNumber; this.balance = initialBalance; this.accountHolderName = accountHolderName; } public void deposit(double amount) { if(amount > 0) balance += amount; } public void withdraw(double amount) { if(amount 0 && discount < price) price -= discount; } } 7. Implement a 'Employee' class with attributes name, ID, and salary. Add methods to raise salary by a given percentage and display information. public class Employee { private String name; private int id; private double salary; public Employee(String name, int id, double salary) { this.name = name; this.id = id; this.salary = salary; } public void raiseSalary(double percentage) { salary += salary * percentage / 100; } public void displayInfo() { System.out.println("Name: " + name + ", ID: " + id + ", Salary: $" + salary); } } 8. Create a 'Library' class with methods to add and remove books, search by title, and list all books. import java.util.ArrayList; public class Library { private ArrayList books = new ArrayList(); public void addBook(Book book) { books.add(book); } public void removeBook(String title) { books.removeIf(book -> book.getTitle().equals(title)); } public Book searchByTitle(String title) { for(Book book : books) { if(book.getTitle().equals(title)) return book; } return null; } public void listAllBooks() { for(Book book : books) { book.displayDetails(); } } } 9. Design a 'Shape' class hierarchy with classes like Circle, Rectangle, and Triangle inheriting from Shape, each with area calculation methods. abstract class Shape { public abstract double area(); } class Circle extends Shape { private double radius; public Circle(double radius) { this.radius = radius; } public double area() { return Math.PI * radius * radius; } } class Rectangle extends Shape { private double length, width; public Rectangle(double length, double width) { this.length = length; this.width = width; } public double area() { return length * width; } } class Triangle extends Shape { private double base, height; public Triangle(double base, double height) { this.base = base; this.height = height; } public double area() { return 0.5 * base * height; } } 10. Implement a 'Person' class with attributes name and age. Create subclasses 'Student' and 'Teacher' with additional attributes and methods specific to each. public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public void display() { System.out.println("Name: " + name + ", Age: " + age); } } class Student extends Person { private String studentID; public Student(String name, int age, String studentID) { super(name, age); this.studentID = studentID; } public void display() { super.display(); System.out.println("Student ID: " + studentID); } } class Teacher extends Person { private String subject; public Teacher(String name, int age, String subject) { super(name, age); this.subject = subject; } public void display() { super.display(); System.out.println("Subject: " + subject); } }