OOP Practical - PDF
OOP Practical - PDF
Practical – 7
AIM: Write a program to demonstrate the use of following types
of inheritance:
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
Input:
// Single Inheritance
class play7{
void display() {
System.out.println("Inside class play7");
}
}
// Multilevel Inheritance
class SubSubPlay extends SubPlay {
void displaySubSub() {
System.out.println("Inside sub-subclass SubSubPlay");
}
}
// Multiple
Inheritance interface
InterfaceA { void
methodA();
}
interface InterfaceB {
void methodB();
}
class MultipleInheritance implements InterfaceA, InterfaceB {
public void methodA() {
System.out.println("Inside methodA");
}
public void methodB() {
System.out.println("Inside methodB");
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
}
}
// Hierarchical Inheritance
class HierarchicalA extends { void
displayA() {
System.out.println("Inside subclass HierarchicalA");
}
}
// Hybrid Inheritance
interface InterfaceC
{ void methodC();
}
// Multilevel Inheritance
SubSubUnavowed obj2 = new SubSubUnavowed();
obj2.display();
obj2.displaySub();
obj2.displaySubSub();
// Multiple Inheritance
MultipleInheritance obj3 = new MultipleInheritance();
obj3.methodA();
obj3.methodB();
// Hierarchical Inheritance
HierarchicalA obj4 = new HierarchicalA();
obj4.display();
obj4.displayA();
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
// Hybrid Inheritance
HybridInheritance obj6 = new HybridInheritance();
obj6.display();
obj6.displayA();
obj6.methodC();
}
}
OUTPUT:-
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
Practical – 8
AIM:Demonstrate the use of “interface” using a program.
INPUT:
// Define an interface
interface Behavior {
void action();
}
// Main class
public class Mainplay8 {
public static void main(String[] args) {
// Creating an object of play8
play8 PlayObj = new
play8();
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
Practical – 9
AIM: Demonstrate abstract class using the program.
INPUT:
// Define an abstract class
abstract class Play9 {
// Abstract method
abstract void display();
// Concrete method
void show() {
System.out.println("Play9 is showing itself");
}
}
// Main class
public class MainPlay9 {
public static void main(String[] args) {
// Creating an object of ConcretePlay9
ConcretePlay9 Play9Obj = new ConcretePlay9();
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
Practical – 10
Aim: Write a program to demonstrate static and dynamic binding of
objects.
Input:
class Play10 {
// Static method
static void staticBinding() {
System.out.println("Static binding: Inside staticBinding() method of Play10");
}
// Dynamic method
void dynamicBinding() {
System.out.println("Dynamic binding: Inside dynamicBinding() method of Play10");
}
}
Output:
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
Practical – 11
Aim: Write a program to demonstrate the visibility modifiers in java.
Input:
// Public class
public class Play11 {
// Private variable
private int privateVariable = 10;
// Protected variable
protected int protectedVariable = 20;
// Default (package-private) variable
int defaultVariable = 30;
// Public method
public void publicMethod() {
System.out.println("Public method");
}
// Protected method
protected void protectedMethod() {
System.out.println("Protected method");
}
// Default (package-private) method
void defaultMethod() {
System.out.println("Default method");
}
// Private method
private void privateMethod() {
System.out.println("Private method");
}
// Main method
public static void MainPlay11 (String[] args) {
// Create an object of the Play11
class Play11 obj = new Play11();
// Access the public variable
System.out.println(Play11.defaultVariable);
// Access the protected variable - Note: This line won't compile as it's not accessible in a
static context
// System.out.println(Play11.protectedVariable);
// Access the default variable - Note: This line won't compile as it's not accessible in a
static context
// System.out.println(Play11.defaultVariable);
// Call the public method
Play11.publicMethod();
// Call the protected method - Note: This line won't compile as it's not accessible in a
static context
// Play11.protectedMethod();
// Call the default method - Note: This line won't compile as it's not accessible in a static
context
// Play11.defaultMethod();
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
// Try to access the private variable - Note: This line won't compile as it's not accessible
in a static context
// System.out.println(Play11.privateVariable);
// Try to call the private method - Note: This line won't compile as it's not accessible in a
static context
// Play11.privateMethod();
}
}
Output:
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
Practical - 12
Aim:Write a program to demonstrate try, catch, finally-exception handling.
Input:
Class MainPlay12 {
Public static void main(String args[]) {
intval1, val2;
try{
//this block will contain statements that may raise exceptions
System.out.println("Try Block:: Start");
val1 = 0;
val2 = 25/ val1;
System.out.println(val2);
System.out.println("Try Block:: End");
}
catch(ArithmeticException e) {
//handler for ArithmeticException
System.out.println("ArithmeticException :: Divide by Zero!!");
}
System.out.println("Outside try-catch:: Rest of the code.");
}
}
Output:
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
Practical – 13
Aim: Write a program to demonstrate the use of throws.
Input:
import java.io.IOException;
Output:
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
Practical – 14
Aim:Write a program to demonstrate user defined exceptions.
Input:
// Custom exception class
class CustomException extends Exception {
// Constructor with a message
public CustomException(String message) {
super(message);
}
}
Output:
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
Practical – 15
Aim:Demonstrate all the ways to use multithreading and synchronize
methods using suitable programs.
Input:
class Play15 extends Thread {
public void run() {
for(int i=0; i<=5; i++) {
System.out.println(i);
}
}
}
class B extends Thread {
public void run() {
for(int i=10; i>=5; i--) {
System.out.println(i);
}
}
}
class ThreadProg {
public static void main(String args[]) {
Play15 obj = new Play15();
B objB = new B();
obj.start();
objB.start();
}
}
Output:
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
Input 2:
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
Practical - 16
import java.io.File;
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
Practical – 17
Aim: Write a program to demonstrate list and stack operations.
Input:
import java.util.ArrayList;
import java.util.Stack;
// Stack Operations
Stack<String>myStack = new Stack<>();
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
Output:
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
Practical – 18
Aim: Write a program that moves a circle up, down, left or right using
arrow keys.
Input:
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to
change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/javafx/FXMain.java to edit this template
*/
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/**
*
* @author student
*/
public class circle extends Application {
@Override
public void start(Stage primaryStage) {
Circle circle = new Circle(200,200,100);
circle.setFill(Color.RED);
circle.setStroke(Color.BLACK);
Text text = new Text(20,20,"Use arrow keys to move the circle");
Group root = new Group(text,circle);
Scene scene = new Scene(root,400,200);
scene.setOnKeyPressed(e->
{
switch(e.getCode())
{
case DOWN: circle.setCenterY(circle.getCenterY()
+50); break;
case UP: circle.setCenterY(circle.getCenterY()-50);
break;
case LEFT: circle.setCenterX(circle.getCenterX()-50);
break;
case RIGHT:circle.setCenterX(circle.getCenterX()+50);
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
break;
}
});
primaryStage.setScene(scene);
primaryStage.setTitle("Move Circle Using Arrow Keys");
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
Output:
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
Practical - 19
Aim:Write a program that displays the color of a circle as red when the
mouse button is pressed and as blue when the mouse button is released.
Input:
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to
change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/javafx/FXMain.java to edit this template
*/
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
/**
*
* @author student
*/
public class circle_color extends Application {
@Override
public void start(Stage primaryStage) {
Circle circle = new Circle(100,100,200);
circle.setStroke(Color.BLACK);
HBox root = new HBox(circle);
Scene scene = new Scene(root,500,500);
primaryStage.setScene(scene);
root.setOnMousePressed(E->circle.setFill(Color.RED));
root.setOnMouseReleased(E->circle.setFill(Color.BLUE));
primaryStage.setTitle("Circle Color Demo");
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
Output:
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
Practical – 20
Aim:Write a GUI program that use button to move the message to the left
and right and use the radio button to change the color for the message
displayed.
Input:
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to
change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/javafx/FXMain.java to edit this template
*/
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/**
*
* @author student
*/
public class msg_move extends Application {
public Text text = new Text(100,100,"JavaFX is wonderful Programing language");
@Override
public void start(Stage primaryStage) {
HBoxhBox_LRbuttons=new HBox(20);
Button left = new Button("Left");
Button right = new
Button("Right");
hBox_LRbuttons.getChildren().addAll(left,right);
HBoxhBox_ColorRadioButtons=new HBox(20);
RadioButton red = new RadioButton("RED");
RadioButton blue=new RadioButton("BLUE");
RadioButton green=new RadioButton("GREEN");
hBox_ColorRadioButtons.getChildren().addAll(red,blue,green);
ToggleGroup group=new ToggleGroup();
red.setToggleGroup(group);
blue.setToggleGroup(group);
green.setToggleGroup(group);
Pane Pane_Message=new Pane();
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
Pane_Message.getChildren().add(text);
BorderPaneborderPane=new BorderPane();
borderPane.setCenter(Pane_Message);
borderPane.setTop(hBox_ColorRadioButtons);
borderPane.setBottom(hBox_LRbuttons);
left.setOnAction(e->text.setX(text.getX()-10));
right.setOnAction(e->text.setX(text.getX()+10));
red.setOnAction(e->
{
if(red.isSelected())
{
text.setFill(Color.RED);
}
});
blue.setOnAction(e->
{
if(blue.isSelected())
{
text.setFill(Color.BLUE);
}
});
green.setOnAction(e->
{
if(green.isSelected())
{
text.setFill(Color.GREEN);
}
});
Scene scene = new
Scene(borderPane,400,200);
primaryStage.setScene(scene);
primaryStage.setTitle("Arrow Button and Color Radio Button Demo");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171
Output:
CKPCET/BE/CE