Java Lab Report (2)
Java Lab Report (2)
Id: CS-2203068
Reg. No : 774
Department of Computer Science and Engineering
(CSE)
Encapsulation
1. Create a Java class representing a bank account with private attributes
accountNumber, balance, and ownerName. Implement methods to deposit money,
withdraw money, and display the current balance. Ensure proper
encapsulation.
ANS:
public class BankAccount {
private String accountNumber;
private double balance;
private String ownerName;
Ans:
public String getOwnerName() {
return ownerName;
}
3. Create a Person class with private attributes name and age. Implement
encapsulated methods to get and set these attributes.
Ans:
public class Person {
private String name;
private int age;
4. Create a Person class with private attributes name and age. Implement
encapsulated methods to get and set these attributes.
Ans:
public void setAge(int age) {
if (age >= 0) {
this.age = age;
} else {
System.out.println("Age cannot be negative");
}
}
5. Create a Circle class with private attributes radius and color. Implement
encapsulated methods to calculate the area of the circle and to get and set
the color.
Ans:
public class Circle {
private double radius;
private String color;
Ans:
public double getCircumference() {
return 2 * Math.PI * radius;
}
7. Create a Student class with private attributes name, rollNumber, and grade.
Implement encapsulated methods to get and set these attributes.
Ans:
public class Student {
private String name;
private int rollNumber;
private char grade;
8. Extend the Student class to include validation in the setter method for the
grade attribute, ensuring that it can only be set to 'A', 'B', 'C', 'D', or 'F'.
Ans:
public void setGrade(char grade) {
if (grade == 'A' || grade == 'B' || grade == 'C' || grade == 'D' || grade == 'F') {
this.grade = grade;
} else {
System.out.println("Invalid grade");
}
}
9. Create a Book class with private attributes title, author, and year. Implement
encapsulated methods to get and set these attributes.
Ans:
public class Book {
private String title;
private String author;
private int year;
10. Extend the Book class to include validation in the setter method for the year
attribute, ensuring that it cannot be set to a future year.
Ans:
public void setYear(int year) {
if (year <= 2024) {
this.year = year;
} else {
System.out.println("Invalid year");
}
}
Abstraction
Ans:
public abstract class Shape {
public abstract double calculateArea();
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
@Override
public double calculateArea() {
return length * width;
}
}
Ans:
public abstract class Animal {
public abstract void makeSound();
public abstract void move();
}
@Override
public void move() {
System.out.println("Running");
}
}
@Override
public void move() {
System.out.println("Flying");
}
}
3. Create an abstract class BankAccount with abstract methods deposit() and
withdraw(). Implement concrete subclasses SavingsAccount and
CheckingAccount that extend BankAccount and provide implementations for the
abstract methods.
Ans:
public abstract class BankAccount {
public abstract void deposit(double amount);
public abstract void withdraw(double amount);
}
@Override
public void withdraw(double amount) {
}
}
@Override
public void withdraw(double amount) {
}
}
Ans:
public interface Shape {
double calculateArea();
}
public class Circle implements Shape {
private double radius;
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
@Override
public double calculateArea() {
return length * width;
}
}
Ans:
public interface Animal {
void makeSound();
void move();
}
@Override
public void move() {
System.out.println("Running");
}
}
@Override
public void move() {
System.out.println("Flying");
}
}
@Override
public void withdraw(double amount) {
}
}
public class CheckingAccount implements BankAccount {
@Override
public void deposit(double amount) {
}
@Override
public void withdraw(double amount) {
}
}
@Override
public void displayInfo() {
}
}
@Override
public void displayInfo() {
}
}
8. Create an interface Vehicle with methods start() and stop(). Implement
classes Car and Motorcycle that implement the Vehicle interface and provide
implementations for the methods.
Ans:
public interface Vehicle {
void start();
void stop();
}
@Override
public void stop() {
}
}
@Override
public void stop() {
}
}
@Override
public void withdraw(double amount) {
}
}
@Override
public void withdraw(double amount) {
}
}
10. Create an interface Phone with methods call() and sendMessage(). Implement
classes SmartPhone and BasicPhone that implement the Phone interface and
provide implementations for the methods.
Ans:
public interface Phone {
void call(String number);
void sendMessage(String message, String recipient);
}
@Override
public void sendMessage(String message, String recipient) {
}
}
Exception Handling
Polymorphism
1. Create a superclass Shape with a method draw() and two subclasses Circle
and Rectangle that override the draw() method to display "Drawing a Circle"
and "Drawing a Rectangle", respectively.
Ans:
class Shape {
void draw() {
System.out.println("Drawing a Shape");
}
}
4. Create a superclass Vehicle with a method move() and two subclasses Car
and Bike that override the move() method to display "Car is moving" and "Bike
is moving", respectively.
Ans:
class Vehicle {
void move() {
System.out.println("Vehicle is moving");
}
}
8. Create a superclass Food with a method eat() and two subclasses Pizza and
Sushi that override the eat() method to display "Eating Pizza" and "Eating
Sushi", respectively.
Ans:
class Food {
void eat() {
System.out.println("Eating Food");
}
}
10. Create a superclass Person with a method displayInfo() and two subclasses
Student and Teacher that override the displayInfo() method to display
information specific to each subclass.
Ans:
class Person {
void displayInfo() {
System.out.println("Person Information");
}
}