0% found this document useful (0 votes)
2 views

AZ OOP 4

This document outlines a lab exercise focused on creating Java applications using object-oriented programming concepts, specifically inheritance, method overriding, and polymorphism. It includes examples of classes and methods for calculating employee salaries, managing train bookings, and library management. The exercises aim to demonstrate the practical application of these concepts through menu-driven interfaces and real-world scenarios.

Uploaded by

rohith07b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

AZ OOP 4

This document outlines a lab exercise focused on creating Java applications using object-oriented programming concepts, specifically inheritance, method overriding, and polymorphism. It includes examples of classes and methods for calculating employee salaries, managing train bookings, and library management. The exercises aim to demonstrate the practical application of these concepts through menu-driven interfaces and real-world scenarios.

Uploaded by

rohith07b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

23DC2002 - Object Oriented Programming Lab Reg.

No URK23CO2018

Ex. No. 4 CREATING APPLICATION USING INHERITANCE

Date of Exercise 20.08.2024

Aim

The main objective of this experiment is to apply object oriented concepts,

and build java application using Java’s Inheritance Concepts such as Super class, Sub classes,

Method Overriding, and Polymorphism

Description

Inheritance in java is a mechanism in which one object acquires all the properties and

behaviors of parent object.

Syntax:

Class Subclass-name extends Superclass-name

//method and fields}

Example

class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class TestInheritance{
public static voidmain(Stringargs[]){
Dog d=newDog();
d.bark();
d.eat();
}}}

1
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018

Method Overriding

If subclass(child class) has the same method as declared in the parent class,it is known

as method overriding in java.

Example

class Bank{
int getRateOfInterest(){return 0;}
}
class SBI extends Bank{
int getRateOfInterest(){return 8;}
}
class ICICI extends Bank{
int getRateOfInterest(){return 7;}
}
class AXIS extends Bank{
int getRateOfInterest(){return 9;}
}
class Test2{
public static void main(String args[]){
SBI s=new
SBI(); ICICI
i=new ICICI();
AXIS a=new
AXIS();
System.out.println("URK23CO2018 SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
}
}

2
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018

Program

1. Develop a java application using Inheritance concept to automate the salary calculation of
employee in an organization as per the salary band given below. Create a super class called
Employee and derive sub classes as per the given table. Apply method overriding (Run time
Polymorphism) to implement the following services via menu driven interface.

a) Calculate Gross Salary

b) Calculate Net Salary

c) Calculate Tax

d) Print the Pay Details

import java.util.Scanner;
class Employee {
double Basic_salary;
double DA ;
double hra;
double tax;
double EPF;
double gross;
double net;
Employee(double basic,int da,int Hra,int TAX,int epf){
this.Basic_salary = basic;
this.DA = Basic_salary * da/100;
this.hra = Basic_salary * Hra/100;
this.tax = Basic_salary * TAX/100;
this.EPF = epf;
this.gross = Basic_salary + DA + hra;
this.net = gross - tax - EPF ;
}
void print() {
System.out.println("Salary details");
System.out.println("Basic pay"+ Basic_salary);
System.out.println("Dearance allowance"+DA);
System.out.println("House rent allowance"+hra);
System.out.println("TAX"+tax);
System.out.println("EPF"+EPF);
System.out.println("Gross Salary"+gross);
System.out.println("Net Salary"+ net);

3
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018

}
}
class Manager extends Employee{
Manager(){
super(100000,95,20,25,3500);
}
}
class Engineer extends Employee{
Engineer(){
super(50000,80,15,15,2500);}
}
public class Employer {
public static void main(String []args) {
Manager m = new Manager();
Engineer b = new Engineer();
Scanner s = new Scanner(System.in);
System.out.println(" URK23CO2018 Calculate the salary of \n 1. Manager \n 2. Engineer
\n Choose your option");
int a = s.nextInt();
if (a == 1) {
m.print();}
else {
b.print();}
s.close();}
}
Output Screenshot

4
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018

4. Develop a java application using Inheritance as per the following. Create a super class

called Train which has attributes like name, trainno, source, destination, no. of tickets,

cost. Add methods in super class such as Check_SeatAvailablity, BookTicket. Derive two

sub classes such as ChennaiExpress and CoimbatoreExpress. Override all the super class

functions in the derived classes. Implement the services such as check seat availability and

booking ticket using run time polymorpshim with menu driven options. Initialize few train

details using array of objects to make it those functions work for any source and

destination (atleast three). Finally print the booked ticket details.

Program

import java.util.Scanner;
class Train {
String name;
int tno;
String source;
String destination;
int nt;
int available;
int cost;
Train(String name, int tno, String source, String destination, int nt, int cost) {
this.name = name;
this.tno = tno;
this.source = source;
this.destination = destination;
this.nt = nt;
this.available = nt;
this.cost = cost;
}

void CheckSeatAvailable() {
System.out.println("Seats Available: " + available);
}

void Bookticket(int n) {

5
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018

if (available >= n) {
available -= n;
System.out.println("Ticket(s) Booked Successfully");
} else {
System.out.println("Not Enough Tickets Available. Availability: " + available);
}
}

void print() {
System.out.printf("Train Name: %s%n", name);
System.out.printf("Train Number: %d%n", tno);
System.out.printf("Starting Location: %s%n", source);
System.out.printf("Destination Location: %s%n", destination);
System.out.printf("Number of Tickets Available: %d%n", available);
System.out.printf("Train Cost: %d%n", cost);
}
}

class ChennaiExpress extends Train {


ChennaiExpress(int tno, String source, String destination, int nt, int cost) {
super("Chennai Express", tno, source, destination, nt, cost);
}
}

class CoimbatoreExpress extends Train {


CoimbatoreExpress(int tno, String source, String destination, int nt, int cost) {
super("Coimbatore Express", tno, source, destination, nt, cost);
}
}

public class Traindetails {


public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Train[] trains = new Train[4];
trains[0] = new ChennaiExpress(101, "Chennai", "Kanyakumari", 12610, 3718);
trains[1] = new ChennaiExpress(102, "Chennai", "Pune", 24220, 5436);
trains[2] = new CoimbatoreExpress(103, "Coimbatore", "Chennai", 11013, 4105);
trains[3] = new CoimbatoreExpress(104, "Coimbatore", "Bangalore", 11013, 8210);

System.out.println("URK23CO2018");
System.out.println("101. Chennai Express (Chennai-Kanyakumari)");

6
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018

System.out.println("102. Chennai Express (Chennai-Pune)");


System.out.println("103. Coimbatore Express (Coimbatore-Chennai)");
System.out.println("104. Coimbatore Express (Coimbatore-Bangalore)");
System.out.print("Choose your Train (Enter Train Number): ");
int trainNo = s.nextInt();

Train selectedTrain = null;


for (Train train : trains) {
if (train.tno == trainNo) {
selectedTrain = train;
break;
}
}

if (selectedTrain != null) {
System.out.println("1. Check Seat Availability");
System.out.println("2. Book Ticket");
System.out.println("3. Print Ticket Details");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = s.nextInt();

switch (choice) {
case 1:
selectedTrain.CheckSeatAvailable();
break;
case 2:
System.out.print("Enter number of tickets to book: ");
int tickets = s.nextInt();
selectedTrain.Bookticket(tickets);
break;
case 3:
selectedTrain.print();
break;
default:
System.out.println("Invalid choice");
}
} else
{
System.out.println("Train not found.");
}

7
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018

s.close();
}
}

Output Screenshot

8
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018

8. Write a menu driven application for Library Management using Java to demonstrate the

concept of Inheritance. Your application must contain the following functionalities along

with the use of method overriding, and super keyword.

a. The following details must be there for each Book

i. bookTitle

ii. bookAuthor

iii. bookNoOfCopies

iv. bookAvailability

v. bookEdition

vi. bookPublisher

b. Get the Book details from librarian

c. In the menu give options to borrow, return and Search options

Program

import java.util.Scanner;
class Book{
String bookTitle;
String bookAuthor;
int bookNoOfCopies;
boolean bookAvailability;
int bookEdition;
String bookPublisher;

Book(String bookTitle,String bookAuthor,int bookNoOfCopies,boolean bookAvailability,int


bookEdition,String bookPublisher){
this.bookTitle = bookTitle;
this.bookAuthor = bookAuthor;
this.bookNoOfCopies = bookNoOfCopies;
this.bookAvailability = bookNoOfCopies>0;
this.bookEdition = bookEdition;
this.bookPublisher = bookPublisher;

9
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018

}
void print(){
System.out.println("bookTitle:"+bookTitle);
System.out.println("bookAuthor:"+bookAuthor);
System.out.println("bookNoOfCopies:"+bookNoOfCopies);
System.out.println("bookAvailability:"+bookAvailability);
System.out.println("bookEdition:"+bookEdition);
System.out.println("bookPublisher:"+bookPublisher);
}

void borrowBook(){
if (this.bookAvailability){
this.bookNoOfCopies--;
System.out.println("The book has been successfully delivered");
}
else{
System.out.println("Book not available");
}
}

void ReturnBook(){
this.bookNoOfCopies +=1;
this.bookAvailability = true;
System.out.println("The booked has been returned successfully");
}
}

class library extends Book{


library (String bookTitle,String bookAuthor,int bookNoOfCopies,boolean bookAvailability,int
bookEdition,String bookPublisher){
super(bookTitle, bookAuthor, bookNoOfCopies, bookAvailability, bookEdition, bookPublisher);
}
void print(){
System.out.println("Searching for Book.....");
super.print();
}
}

public class Expt48{


public static void main(String []args){
//library l = new library();

10
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018

Scanner s = new Scanner(System.in);


System.out.println("Enter the book name");
String sa = s.nextLine();
System.out.println("Enter the book Author");
String author = s.nextLine();
System.out.println("Enter the no of books");
int copy = s.nextInt();
System.out.println("Enter the book edition");
int edition = s.nextInt();
System.out.println("Enter the book Publisher");
String pub = s.nextLine();
library l = new library(sa,author,copy,true,edition,pub);

System.out.println("URK23CO2018 MENU\n ---------------------------------------\n1. Borrow Book


\n2. Return Book\n3. Search book\n4. Exit\nEnter your choice");
int choice = s.nextInt();

switch(choice){
case 1:
l.borrowBook();
break;
case 2:
l.ReturnBook();
break;
case 3:
l.print();

case 4:
break;
default:
System.out.println("Wrong input");
}s.close();
}

11
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018

Output Screenshot

Result

Successfully implemented and executed the creation and manipulation of classes and objects in
Java to solve real-world problems using inheritance, method overriding, and runtime
polymorphism

12

You might also like