0% found this document useful (0 votes)
90 views8 pages

Unit 2 Programs Java

1. Inheritance allows classes to inherit attributes and behaviors from a parent class. The document defines a Book class and subclasses ReferenceBook and Magazine that inherit from Book and override its display method to add additional subclass-specific fields. 2. The document defines an Interest interface with simpleInterest and compoundInterest methods and an InterestCalculator class that implements the interface's methods. 3. An abstract Reservation class is created with a Reserve abstract method. Subclasses ReserveTrain and ReserveBus implement Reserve for reserving train and bus tickets.

Uploaded by

Moon Nila
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
90 views8 pages

Unit 2 Programs Java

1. Inheritance allows classes to inherit attributes and behaviors from a parent class. The document defines a Book class and subclasses ReferenceBook and Magazine that inherit from Book and override its display method to add additional subclass-specific fields. 2. The document defines an Interest interface with simpleInterest and compoundInterest methods and an InterestCalculator class that implements the interface's methods. 3. An abstract Reservation class is created with a Reserve abstract method. Subclasses ReserveTrain and ReserveBus implement Reserve for reserving train and bus tickets.

Uploaded by

Moon Nila
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 8

UNIT-II

PART-C
2. What is inheritance? How inheritance is implemented in java? Create a class
Book and define display method to display book information. Inherit
Reference_Book and Magazine classes from Book class and override display
method of Book class in Reference_Book and Magazine classes.
Make necessary assumptions required.(
// Base class: Book
class Book {
private String title;
private String author;
private String publisher;
private int publicationYear;

public Book(String title, String author, String publisher, int publicationYear) {


this.title = title;
this.author = author;
this.publisher = publisher;
this.publicationYear = publicationYear;
}

public void display() {


System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("Publisher: " + publisher);
System.out.println("Publication Year: " + publicationYear);
}
}

// Subclass 1: Reference_Book
class Reference_Book extends Book {
private int edition;

public Reference_Book(String title, String author, String publisher, int publicationYear, int
edition) {
super(title, author, publisher, publicationYear);
this.edition = edition;
}

// Override the display method to include edition


@Override
public void display() {
super.display(); // Call the parent class display method
System.out.println("Edition: " + edition);
}
}

// Subclass 2: Magazine
class Magazine extends Book {
private int issueNumber;
public Magazine(String title, String author, String publisher, int publicationYear, int
issueNumber) {
super(title, author, publisher, publicationYear);
this.issueNumber = issueNumber;
}

// Override the display method to include issue number


@Override
public void display() {
super.display(); // Call the parent class display method
System.out.println("Issue Number: " + issueNumber);
}
}

public class Main {


public static void main(String[] args) {
Book book = new Book("The Great Gatsby", "F. Scott Fitzgerald", "Scribner", 1925);
System.out.println("Book Information:");
book.display();
System.out.println();

Reference_Book referenceBook = new Reference_Book("Java: The Complete


Reference", "Herbert Schildt", "McGraw-Hill Education", 2017, 10);
System.out.println("Reference Book Information:");
referenceBook.display();
System.out.println();

Magazine magazine = new Magazine("National Geographic", "Various", "National


Geographic Partners", 2023, 10);
System.out.println("Magazine Information:");
magazine.display();
}
}

OUTPUT:
Book Information:
Title: The Great Gatsby
Author: F. Scott Fitzgerald
Publisher: Scribner
Publication Year: 1925

Reference Book Information:


Title: Java: The Complete Reference
Author: Herbert Schildt
Publisher: McGraw-Hill Education
Publication Year: 2017
Edition: 10
Magazine Information:
Title: National Geographic
Author: Various
Publisher: National Geographic Partners
Publication Year: 2023
Issue Number: 10
3. Develop an Interest interface which containssimpleInterest and compInterest
methods and static final field of Rate 25%. Write a class toimplement those
methods.
// Define an interface for interest calculations
interface Interest {
double simpleInterest(double principal, double rate, double time);
double compoundInterest(double principal, double rate, double time);
}

// Create a class that calculates interest based on the interface


class InterestCalculator implements Interest {
@Override
public double simpleInterest(double p, double r, double t) {
// Simple Interest formula: SI = P * R * T / 100
return (p * r * t) / 100.0;
}

@Override
public double compoundInterest(double p, double r, double t) {
// Compound Interest formula: CI = P * (1 + R/100)^T - P
return p * (Math.pow(1 + r / 100, t)) - p;
}
}

public class Main {


public static void main(String[] args) {
// Create an instance of the InterestCalculator class
InterestCalculator calc = new InterestCalculator();

// Set initial amount, annual interest rate (in percentage), and time period (in years)
double principal = 1000.0;
double rate = 5.0;
double time = 2.5;

// Calculate and display simple interest


double simple = calc.simpleInterest(principal, rate, time);
System.out.println("Simple Interest: " + simple);

// Calculate and display compound interest


double compound = calc.compoundInterest(principal, rate, time);
System.out.println("Compound Interest: " + compound);
}
}
OUTPUT:
Simple Interest: 125.0
Compound Interest: 128.015625
4. Create an abstract Reservation class which has Reserve() abstract
method. Implement the sub-classes like ReserveTrain and ReserveBus
classes and implement the same. Use necessary variables, methods for
reserving tickets
abstract class Reservation {
abstract void Reserve();
}

class ReserveTrain extends Reservation {


String trainName;
int numberOfSeats;

public ReserveTrain(String trainName, int numberOfSeats) {


this.trainName = trainName;
this.numberOfSeats = numberOfSeats;
}

@Override
void Reserve() {
System.out.println(numberOfSeats + " seats reserved in " + trainName + " train.");
}
}

class ReserveBus extends Reservation {


String busName;
int numberOfSeats;

public ReserveBus(String busName, int numberOfSeats) {


this.busName = busName;
this.numberOfSeats = numberOfSeats;
}

@Override
void Reserve() {
System.out.println(numberOfSeats + " seats reserved in " + busName + " bus.");
}
}

public class Main {


public static void main(String[] args) {
Reservation trainReservation = new ReserveTrain("Rajdhani Express", 2);
trainReservation.Reserve();

Reservation busReservation = new ReserveBus("Volvo", 4);


busReservation.Reserve();
}
}
OUTPUT:
2 seats reserved in Rajdhani Express train.
4 seats reserved in Volvo bus.

PART-B
17. Declare an abstract class to represent a bank account with data members
name, account number, address and abstract methods withdraw and
deposit. Method display() is needed to show balance. Derive a subclass
Savings Account and add the following details: return on investment and
the method calcAmt() to show the amount in the account after 1
year.Create instance of Savings Account and show the use of withdraw
and deposit abstract methods

abstract class BankAccount {


String name;
String accountNumber;
String address;
double balance;

public BankAccount(String name, String accountNumber, String address, double balance)


{
this.name = name;
this.accountNumber = accountNumber;
this.address = address;
this.balance = balance;
}

abstract void withdraw(double amount);


abstract void deposit(double amount);

public void display() {


System.out.println("Balance: " + balance);
}
}

class SavingsAccount extends BankAccount {


double returnOnInvestment;

public SavingsAccount(String name, String accountNumber, String address, double


balance, double returnOnInvestment) {
super(name, accountNumber, address, balance);
this.returnOnInvestment = returnOnInvestment;
}

public void calcAmt() {


double interest = balance * returnOnInvestment;
balance += interest;
System.out.println("Amount after 1 year: " + balance);
}

@Override
void withdraw(double amount) {
balance -= amount;
}

@Override
void deposit(double amount) {
balance += amount;
}
}

public class Main {


public static void main(String[] args) {
SavingsAccount savingsAccount = new SavingsAccount("John Doe", "123456789",
"123 Main St", 1000, 0.05);
savingsAccount.deposit(500);
savingsAccount.withdraw(200);
savingsAccount.display();
savingsAccount.calcAmt();
}
}

OUTPUT:
Balance: 1300.0
Amount after 1 year: 1365.0
UNIT-II
PART-B
12.

class Calculator {
int num1, num2;

Calculator(int a, int b) {
num1 = a;
num2 = b;
}
void add() {
System.out.println("Addition result: " + (num1 + num2));
}
}

class SubtractionCalculator extends Calculator {


SubtractionCalculator(int a, int b) {
super(a, b);
}

void subtract() {
System.out.println("Subtraction result: " + (num1 - num2));
}
}

class MultiplicationCalculator extends SubtractionCalculator {


MultiplicationCalculator(int a, int b) {
super(a, b);
}

void multiply() {
System.out.println("Multiplication result: " + (num1 * num2));
}
}

public class Main {


public static void main(String[] args) {
MultiplicationCalculator calculator = new MultiplicationCalculator(10, 5);
calculator.add();
calculator.subtract();
calculator.multiply();
}
}
OUTPUT:
Addition result: 15
Subtraction result: 5
Multiplication result: 50

You might also like