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

OOP Practical - PDF

CN _Practical Assignment Index 2024-25

Uploaded by

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

OOP Practical - PDF

CN _Practical Assignment Index 2024-25

Uploaded by

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

OOP-I[3140705] Enrollment No: 220090107171

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");
}
}

class SubPlay extends play7{


void displaySub() {
System.out.println("Inside subclass SubPlay");
}
}

// 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");
}
}

class HierarchicalB extends play7{


void displayB() {
System.out.println("Inside subclass HierarchicalB");
}
}

// Hybrid Inheritance
interface InterfaceC
{ void methodC();
}

class HybridInheritance extends HierarchicalA implements InterfaceC {


public void methodC() {
System.out.println("Inside methodC");
}
}

public class MainPlay7 {


public static void main(String[] args) {
// Single Inheritance
SubPlay obj1 = new SubPlay();
obj1.display();
obj1.displaySub();

// 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

HierarchicalB obj5 = new HierarchicalB();


obj5.display();
obj5.displayB();

// 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();
}

// Class play8 implementing the interface


class Play implements Behavior {
// Implementing the action method from the Behavior interface
public void action() {
System.out.println("play8 is displaying its behavior");
}
}

// Main class
public class Mainplay8 {
public static void main(String[] args) {
// Creating an object of play8
play8 PlayObj = new
play8();

// Calling the action method through the play8 object


PlayObj.action();
}
}
Output:

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");
}
}

// Concrete subclass extending the abstract class


class ConcretePlay9 extends Play9 {
// Implementing the abstract method
void display() {
System.out.println("Play9 is displaying itself");
}
}

// Main class
public class MainPlay9 {
public static void main(String[] args) {
// Creating an object of ConcretePlay9
ConcretePlay9 Play9Obj = new ConcretePlay9();

// Calling the methods


Play9Obj.display();
Play9Obj.show();
}
}
Output:

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");
}
}

public class MainPlay10 {


public static void main(String[] args) {
// Static binding - resolved at compile time
Play10.staticBinding();

// Dynamic binding - resolved at


runtime Play10 obj = new Play10();
obj.dynamicBinding();

Play10 obj2 = new SubPlay10();


obj2.dynamicBinding();
}
}
class SubPlay10extends Play10 {
void dynamicBinding() {
System.out.println("Dynamic binding: Inside dynamicBinding() method of SubPlay10");
}
}

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;

public class Play13 {


// Method that throws an IOException
static void exampleMethod() throws IOException {
throw new IOException("IOException occurred");
}

public static void main(String[] args) {


try {
exampleMethod(); // Call the method that throws an IOException
} catch (IOException e) {
System.out.println("Caught IOException: " + e.getMessage());
}
}
}

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);
}
}

public class Play14 {


// Method that throws the custom exception
static void exampleMethod(int value) throws CustomException {
if (value < 0) {
throw new CustomException("Negative value not allowed");
} else {
System.out.println("Value is: " + value);
}
}

public static void main(String[] args) {


try {
// Call the method with a negative
value exampleMethod(-5);
} catch (CustomException e) {
// Handle the custom exception
System.out.println("Caught CustomException: " + e.getMessage());
}
}
}

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:

class Play15 implements Runnable {


public void run() {
for (int i = 0; i<= 5; i++)
{ System.out.println(i);
}
}
}
class B implements Runnable {
public void run() {
for (int i = 5; i>= 0; i--) {
System.out.println(i);
}
}
}
class ThreadProgRunn {
public static void main(String args[])
{ Play15 obj1 = new Play15();
B obj2 = new B();
Thread t1 = new
Thread(obj1); Thread t2 =
new Thread(obj2); t1.start();
t2.start();
}
}
Output:

CKPCET/BE/CE
OOP-I[3140705] Enrollment No: 220090107171

Practical - 16

Aim: Write a program to demonstrate file properties using java.io.File.


Input:

import java.io.File;

public class Play16 {


public static void main(String[] args) {
// Replace "your_file_path" with the path to the file you want to inspect
File file = new File("your_file_path");

// Check if the file exists


if (file.exists()) {
// Print file properties
System.out.println("File Name: " + file.getName());
System.out.println("Absolute Path: " + file.getAbsolutePath());
System.out.println("Size: " + file.length() + " bytes");

// Check if the file is readable, writable, and


executable System.out.println("Readable: " + file.canRead());
System.out.println("Writable: " + file.canWrite());
System.out.println("Executable: " + file.canExecute());

// Check if the file is a directory or a regular file


System.out.println("Is Directory: " +
file.isDirectory()); System.out.println("Is File: " +
file.isFile());

// Print last modified timestamp


System.out.println("Last Modified: " + file.lastModified());
} else {
System.out.println("File does not exist.");
}
}
}
Output:

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;

public class Play17 {


public static void main(String[] args) {
// List Operations
ArrayList<String>myList = new ArrayList<>();

// Adding elements to the list


myList.add("A");
myList.add("B");
myList.add("C");
myList.add("D");

// Displaying the list


System.out.println("List elements:");
System.out.println(myList);

// Stack Operations
Stack<String>myStack = new Stack<>();

// Pushing elements to the stack


myStack.push("X");
myStack.push("Y");
myStack.push("Z");

// Displaying the stack


System.out.println("\nStack elements:");
System.out.println(myStack);

// Popping elements from the stack


String poppedElement = myStack.pop();
System.out.println("\nPopped Element: " + poppedElement);

// Displaying the updated stack


System.out.println("Stack after pop operation:");
System.out.println(myStack);
}
}

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

You might also like