STUDENT, TEST, AND RESULT (MULTILEVEL INHERITANCE)
EX. No:1
Date:
AIM:
To demonstrate multilevel inheritance and calculation of student results.
ALGORITHM:
1. Create a Student class with rollno as a data member and setter/getter methods.
2. Create a Test class inheriting Student, adding marks1 and marks2.
3. Create a Result class inheriting Test, adding total marks calculation.
4. Define a display() method to show all details.
5. Create a main() method to instantiate and display results.
6. Print the student’s details and total marks.
CODING:
class Student
{
int rollno;
void setRollNo(int roll)
{
rollno = roll;
}
int getRollNo()
{
return rollno;
}
}
class Test extends Student
{
int marks1, marks2;
void setMarks(int m1, int m2)
{
marks1 = m1;
marks2 = m2;
int getTotalMarks()
{
return marks1 + marks2;
}
}
class Result extends Test
{
void display()
{
System.out.println("Roll No: " + getRollNo());
System.out.println("Marks1: " + marks1);
System.out.println("Marks2: " + marks2);
System.out.println("Total: " + getTotalMarks());
}
}
public class StudentTest
{
public static void main(String[] args)
{
Result r = new Result();
r.setRollNo(101);
r.setMarks(85, 90);
r.display();
System.out.println(SANJAY S);
}
}
OUTPUT:
RESULT:
Thus, the java program executed successfully using JDK.
Media, Book, and Tape
(Hierarchical Inheritance)
EX. No:2
Date:
AIM:
To showcase hierarchical inheritance with different media types like books and tapes.
ALGORITHM:
1. Create a Media class with title and price.
2. Create Book class inheriting Media, adding pages.
3. Create Tape class inheriting Media, adding time.
4. Implement a display() method in all classes.
5. Instantiate objects in main() and display details.
6. Print book and tape details.
CODING:
class Media
{
String title;
double price;
Media(String title, double price)
{
this.title = title;
this.price = price;
}
void display()
{
System.out.println("Title: " + title);
System.out.println("Price: $" + price);
}
}
class Book extends Media
{
int pages;
Book(String tile, double price, int pages)
{
super(title, price);
this.pages = pages;
}
void display()
{
super.display();
System.out.println("Pages: " + pages);
}
}
class Tape extends Media
{
float time;
Tape(String title, double price, float time)
{
super(title, price);
this.time = time;
}
void display()
{
super.display();
System.out.println("Time: " + time + " mins");
}
}
public class MediaTest
{
public static void main(String[] args)
{
Book b = new Book("Java Programming", 29.99, 500);
Tape t = new Tape("Learning Java", 19.99, 90);
System.out.println("Book Details:");
b.display();
System.out.println("\nTape Details:");
t.display();
System.out.println(“SANJAY S”)
}
}
OUTPUT:
RESULT:
Thus, the java program executed successfully using JDK.
Bike and MountainBike (Single Inheritance)
EX. No: 3
Date:
AIM:
To illustrate single inheritance using a bike and a mountain bike.
ALGORITHM:
1. Create a Bike class with gear and speed.
2. Implement applyBrake() and speedUp().
3. Create MountainBike inheriting Bike, adding seatHeight.
4. Implement setHeight().
5. Override toString() to display bike details.
6. Create objects in main() and display details.
CODING:
class Bike
{
int gear, speed;
Bike(int gear, int speed)
{
this.gear = gear;
this.speed = speed;
}
void applyBrake(int decrement)
{
speed -= decrement;
}
void speedUp(int increment)
{
speed += increment;
}
public String toString()
{
return "Gear: " + gear + ", Speed: " + speed;
}
}
class MountainBike extends Bike
{
int seatHeight;
MountainBike(int gear, int speed, int seatHeight)
{
super(gear, speed);
this.seatHeight = seatHeight;
}
void setHeight(int newHeight)
{
seatHeight = newHeight;
}
public String toString()
{
return super.toString() + ", Seat Height: " + seatHeight;
}
}
public class BikeTest
{
public static void main(String[] args)
{
MountainBike mb = new MountainBike(3, 20, 5);
System.out.println("Before applying brake: " + mb);
mb.applyBrake(5);
System.out.println("After applying brake: " + mb);
System.out.println();
}
}
OUTPUT:
RESULT:
Thus, the java program executed successfully using JDK.
Train and Reservation System (Multilevel Inheritance)
EX. No: 4
Date:
AIM:
To demonstrate multilevel inheritance in train reservation systems.
ALGORITHM:
Create a Train class with seat details.
Implement methods to set and display seat data.
Create Reservation class inheriting Train, adding seatsBooked.
Implement methods to book and cancel seats.
Display seat availability in main().
CODING:
class Train
{
int firstTier, secondTier, thirdTier;
Train(int first, int second, int third)
{
firstTier = first;
secondTier = second;
thirdTier = third;
}
void displaySeats()
{
System.out.println("First Tier Seats: " + firstTier);
System.out.println("Second Tier Seats: " + secondTier);
System.out.println("Third Tier Seats: " + thirdTier);
}
}
class Reservation extends Train
{
int bookedFirst, bookedSecond, bookedThird;
Reservation(int first, int second, int third) {
super(first, second, third);
}
void bookSeats(int first, int second, int third)
{
bookedFirst += first;
bookedSecond += second;
bookedThird += third;
}
void cancelSeats(int first, int second, int third)
{
bookedFirst -= first;
bookedSecond -= second;
bookedThird -= third;
}
void displayStatus()
{
System.out.println("Seats booked: First Tier - " + bookedFirst + ", Second Tier - " +
bookedSecond + ", Third Tier - " + bookedThird);
System.out.println("Remaining Seats: First Tier - " + (firstTier - bookedFirst) + ",
Second Tier - " + (secondTier - bookedSecond) + ", Third Tier - " + (thirdTier -
bookedThird));
}
}
public class TrainTest
{
public static void main(String[] args)
{
Reservation res = new Reservation(50, 100, 150);
res.bookSeats(10, 20, 30);
res.displayStatus();
res.cancelSeats(5, 10, 15);
res.displayStatus();
System.out.println()
}
}
OUTPUT:
RESULT:
Thus, the java program executed successfully using JDK.
PlayerRating System (Decision Making & Inheritance)
EX. No: 5
Date:
AIM:
To compute and categorize player ratings using inheritance.
ALGORITHM:
1. Create a PlayerRating class with playerName and ratings array.
2. Implement calculateAverageRating() to determine player category.
3. Define a displayRating() method to print details.
4. Create a main() method to input and process ratings.
5. Calculate the player's category based on average rating.
6. Display the player's details and category.
CODING:
class PlayerRating
{
String playerName;
double[] ratings;
double average;
String category;
PlayerRating(String name, double[] ratings) {
this.playerName = name;
this.ratings = ratings;
this.average = calculateAverageRating();
this.category = assignCategory();
}
double calculateAverageRating()
{
double sum = 0;
for (double r : ratings)
{
sum += r;
}
return sum / ratings.length;
}
String assignCategory()
{
if (average >= 8) return "Excellent";
else if (average >= 6) return "Good";
else return "Average";
}
void displayRating()
{
System.out.println("Player Name: " + playerName);
System.out.println("Average Rating: " + average);
System.out.println("Category: " + category);
}
}
public class PlayerRatingTest
{
public static void main(String[] args)
{
double[] ratings = {7.5, 8.2, 7.9};
PlayerRating player = new PlayerRating("John Doe", ratings);
player.displayRating();
System.out.println()
}
}
OUTPUT:
RESULT:
Thus, the java program executed successfully using JDK.
Shape and Box (Method Overriding & Constructor Chaining)
EX. No:6
Date:
AIM:
To show method overriding and constructor chaining for calculating area and volume.
ALGORITHM:
1. Create a Shape class with length and breadth.
2. Implement a constructor and calculate() for area.
3. Create Box class inheriting Shape, adding height.
4. Use constructor chaining and override calculate() for volume.
5. Instantiate objects in main() and call calculate().
6. Print both area and volume.
CODING:
class Shape {
double length, breadth;
Shape(double length, double breadth) {
this.length = length;
this.breadth = breadth;
}
double calculate() {
return length * breadth; // Area of rectangle
}
}
class Box extends Shape {
double height;
Box(double length, double breadth, double height) {
super(length, breadth);
this.height = height;
}
@Override
double calculate() {
return length * breadth * height; // Volume of box
}
}
public class ShapeTest {
public static void main(String[] args) {
Shape rect = new Shape(10, 5);
Box cube = new Box(10, 5, 3);
System.out.println("Area of Rectangle: " + rect.calculate());
System.out.println("Volume of Box: " + cube.calculate());
System.out.println(“Sri Harish R”)
}
}
OUTPUT:
RESULT:
Thus, the java program executed successfully using JDK.
Internet Service Registration
EX. No:7
Date:
AIM:
To perform user validation using conditional checks for registration.
ALGORITHM:
1. Create a User class with name and phoneNumbers.
2. Implement a method to check authentication based on passport or alternative IDs.
3. Create a validateUser() method to check valid combinations.
4. Create a main() method to input user details.
5. Validate authentication criteria.
6. Display registration status.
CODING:
class User {
String name;
String phone1, phone2;
String passport, license, pan, voterId;
User(String name, String phone1, String phone2, String passport, String license, String
pan, String voterId) {
this.name = name;
this.phone1 = phone1;
this.phone2 = phone2;
this.passport = passport;
this.license = license;
this.pan = pan;
this.voterId = voterId;
}
boolean validateUser() {
if (passport != null && !passport.isEmpty()) return true;
if ((license != null && pan != null) || (voterId != null && license != null) || (pan != null
&& voterId != null)) return true;
return false;
}
void displayStatus() {
System.out.println("User: " + name);
if (validateUser()) {
System.out.println("Registration Successful!");
} else {
System.out.println("Registration Failed! Provide valid documents.");
}
}
}
public class InternetServiceTest {
public static void main(String[] args) {
User u1 = new User("Alice", "9876543210", "9123456780", "", "L12345", "P67890",
""); // Valid
User u2 = new User("Bob", "9234567890", "9345678901", "", "", "P12345", ""); //
Invalid
u1.displayStatus();
u2.displayStatus();
System.out.println(“Sri Harish R”)
}
}
OUTPUT:
RESULT:
Thus, the java program executed successfully using JDK.
Payment Service (Abstract Class & Method Overriding)
EX. No:8
Date:
AIM:
To implement abstract classes for handling various types of payments.
ALGORITHM:
1. Create an abstract class RRPaymentServices with balance and payBill() method.
2. Create ShoppingPayment inheriting RRPaymentServices, implementing payBill().
3. Create CreditCardPayment inheriting RRPaymentServices, implementing payBill().
4. Implement bill processing logic for both payment types.
5. Create a main() method to process payments.
6. Display transaction details.
CODING:
abstract class RRPaymentServices {
double balance;
RRPaymentServices(double balance) {
this.balance = balance;
}
abstract void payBill(double amount);
}
class ShoppingPayment extends RRPaymentServices {
static int counter = 1000;
String paymentID;
ShoppingPayment(double balance) {
super(balance);
}
@Override
void payBill(double amount) {
if (amount != balance) {
System.out.println("Invalid payment amount!");
} else {
counter++;
paymentID = "S" + counter;
System.out.println("Shopping Payment Successful. Payment ID: " + paymentID);
}
}
}
class CreditCardPayment extends RRPaymentServices {
static int counter = 2000;
String transactionID;
double cashBack = 0, balanceDue = 0;
CreditCardPayment(double balance) {
super(balance);
}
@Override
void payBill(double amount) {
counter++;
transactionID = "C" + counter;
if (amount > balance) {
cashBack = amount - balance;
System.out.println("Payment Successful! Cashback: $" + cashBack);
} else if (amount < balance) {
balanceDue = balance - amount;
System.out.println("Partial Payment. Balance Due: $" + balanceDue);
} else {
System.out.println("Payment Successful!");
}
System.out.println("Transaction ID: " + transactionID);
}
}
public class PaymentTest {
public static void main(String[] args) {
ShoppingPayment sp = new ShoppingPayment(500);
sp.payBill(500); // Valid Payment
CreditCardPayment cp = new CreditCardPayment(1000);
cp.payBill(1200); // Cashback scenario
cp.payBill(800); // Balance due scenario
System.out.println(“Sri Harish R”)
}
}
OUTPUT:
RESULT:
Thus, the java program executed successfully using JDK.
DataProvider Interface (Intern & Trainee Percentage Calculation)
EX. No: 9
Date:
AIM:
To calculate and display the percentage for Interns and Trainees via interfaces.
ALGORITHM:
1. Create an interface DataProvider with a method calcPercentage().
2. Create Intern class implementing DataProvider, considering grace marks.
3. Create Trainee class implementing DataProvider, considering only course marks.
4. Implement calcPercentage() in both classes.
5. Create a main() method to instantiate objects and calculate percentages.
6. Display the results.
CODING:
interface DataProvider {
double calcPercentage();
}
class Intern implements DataProvider {
double marks;
static final double MAX_MARKS = 8000;
Intern(double marks) {
this.marks = marks + 100; // Adding grace marks
}
@Override
public double calcPercentage() {
return (marks / MAX_MARKS) * 100;
}
}
class Trainee implements DataProvider {
double marks;
static final double MAX_MARKS = 8000;
Trainee(double marks) {
this.marks = marks;
}
@Override
public double calcPercentage() {
return (marks / MAX_MARKS) * 100;
}
}
public class PercentageTest {
public static void main(String[] args) {
Intern intern = new Intern(7200);
Trainee trainee = new Trainee(7500);
System.out.println("Intern's Percentage: " + intern.calcPercentage() + "%");
System.out.println("Trainee's Percentage: " + trainee.calcPercentage() + "%");
System.out.println(“Sri Harish R”)
}
}
OUTPUT:
RESULT:
Thus, the java program executed successfully using JDK.
Box, BoxWeight, and Shipment
(Multilevel Inheritance)
EX. No: 10
Date:
AIM:
To demonstrate multilevel inheritance and calculate box volume, weight, and shipment costs.
ALGORITHM:
1. Create Box class with length, breadth, height, and method for volume.
2. Create BoxWeight class inheriting Box, adding weight.
3. Create Shipment class inheriting BoxWeight, adding shipmentCost.
4. Implement displayDetails() to show all details.
5. Create main() method to instantiate objects.
6. Print volume, weight, and shipment cost.
CODING:
class Box {
double length, breadth, height;
Box(double length, double breadth, double height) {
this.length = length;
this.breadth = breadth;
this.height = height;
}
double calculateVolume() {
return length * breadth * height;
}
}
class BoxWeight extends Box {
double weight;
BoxWeight(double length, double breadth, double height, double weight) {
super(length, breadth, height);
this.weight = weight;
}
}
class Shipment extends BoxWeight {
double shipmentCost;
Shipment(double length, double breadth, double height, double weight, double cost) {
super(length, breadth, height, weight);
this.shipmentCost = cost;
}
void displayDetails() {
System.out.println("Box Dimensions: " + length + " x " + breadth + " x " + height);
System.out.println("Volume: " + calculateVolume());
System.out.println("Weight: " + weight + " kg");
System.out.println("Shipment Cost: $" + shipmentCost);
System.out.println("-------------------------------");
}
}
public class ShipmentTest {
public static void main(String[] args) {
Shipment s1 = new Shipment(10, 5, 2, 8, 25);
Shipment s2 = new Shipment(15, 7, 3, 12, 40);
System.out.println("Shipment Details:");
s1.displayDetails();
s2.displayDetails();
System.out.println(“Sri Harish R”)
}
}
OUTPUT:
RESULT:
Thus, the java program executed successfully using JDK.