Java Practical File ss1
Java Practical File ss1
scanner.close();
{ number = Math.abs(number);
int count = 0;
{ count++;
return count;
OUTPUT:
Page |2
if (n == 0) {
return 1;
else {
scanner.close();
if (num < 0) {
} else {
OUTPUT:
Page |3
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{3,3,3},{2,2,2},{1,1,1}};
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++)
c[i][j]+=a[i][k]*b[k][j];
System.out.print(c[i][j]+" ");
System.out.println();
}}
OUTPUT:
Page |4
int n = scanner.nextInt();
System.out.println("Fibonacci series:");
printFibonacci(n);
firstTerm = secondTerm;
secondTerm = nextTerm;
OUTPUT:
Page |5
System.out.println();
System.out.print(" ");
System.out.print("*");
System.out.println();
OUTPUT:
Page |6
staticVariable = 10;
obj1.staticVariable = 20;
System.out.println("Modified value of staticVariable through obj1: " + obj1.staticVariable);
System.out.println("Value of staticVariable through obj2 after modification: " +
obj2.staticVariable);
}
}
OUTPUT:
Page |7
OUTPUT:
Page |8
// Main class
public class InheritanceExample {
public static void main(String[] args) {
// Create an object of child class
Child childObj = new Child();
// Call methods of both parent and child classes
childObj.displayParent(); // Method from parent class
childObj.displayChild(); // Method from child class
}
}
OUTPUT:
P a g e | 10
// Base class
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
// Derived class 1
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}
// Derived class 2
class Cat extends Animal {
void meow() {
System.out.println("Cat is meowing");
}
}
// Main class
public class HierarchicalInheritance {
public static void main(String[] args) {
Dog dog = new Dog();
Cat cat = new Cat();
OUTPUT:
P a g e | 11
return a + b;
return a + b + c;
return s1 + s2;
}}
OUTPUT:
P a g e | 12
class Animal
// Derived class
@Override
void makeSound() {
System.out.println("Dog barks");
}}
// Main class
OUTPUT:
P a g e | 13
void calculateArea() {
@Override
void calculateArea() {
@Override
void calculateArea() {
// Main class
OUTPUT:
P a g e | 15
// Abstract class
abstract class Shape {
// Abstract method to calculate area
abstract double calculateArea();
// Concrete method
void display() {
System.out.println("This is a shape.");
}
}
// Constructor
Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
// Constructor
Circle(double radius) {
this.radius = radius;
}
}
}
// Main class
public class AbstractClassExample {
public static void main(String[] args) {
// Creating objects of different shapes
Shape rectangle = new Rectangle(5, 3);
Shape circle = new Circle(4);
circle.display();
System.out.println("Area of circle: " + circle.calculateArea());
}
}
OUTPUT:
P a g e | 17
interface Shape {
double length;
double width;
// Constructor
this.length = length;
this.width = width;
@Override
@Override
double radius;
// Constructor
Circle(double radius) {
this.radius = radius;
@Override
@Override
// Main class
System.out.println("Rectangle:");
System.out.println("\nCircle:");
OUTPUT:
P a g e | 20
try {
Thread.sleep((long)(Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
// Main class
class MultithreadingExample {
// Create a new thread and pass the MyThread instance to its constructor
thread.start();
try {
Thread.sleep((long)(Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
OUTPUT:
P a g e | 22
try {
int result = 10 / 0;
// ArrayIndexOutOfBoundsException
System.out.println(arr[5]);
// NullPointerException
System.out.println(str.length());
} catch (ArithmeticException e) {
} catch (ArrayIndexOutOfBoundsException e) {
} catch (NullPointerException e) {
} catch (Exception e) {
OUTPUT:
P a g e | 23
18: Create a custom exception and throw in case of age<18 for voting.
class UnderAgeException extends Exception
{ public UnderAgeException()
} else {
try {
checkVotingEligibility(age);
catch (UnderAgeException e)
}}
OUTPUT:
P a g e | 24
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Student Table</title>
</head>
<body>
<h2>Student Table</h2>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Grade</th>
</tr>
<tr>
<td>1</td>
<td>Sanjay</td>
<td>18</td>
<td>A</td>
</tr>
<tr>
<td>2</td>
P a g e | 25
<td>Kunal</td>
<td>17</td>
<td>B</td>
</tr>
<tr>
<td>3</td>
<td>Yash</td>
<td>19</td>
<td>A</td>
</tr>
</table>
</body>
</html>
OUTPUT:
P a g e | 26
import javax.swing.*;
@Override
super.paintComponent(g);
g.setColor(Color.RED);
// Draw a rectangle
g.setColor(Color.BLUE);
g.setColor(Color.GREEN);
// Draw an oval
g.setColor(Color.YELLOW);
g.setColor(Color.BLACK);
P a g e | 27
// Draw a line
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 350);
frame.add(new DrawShapes());
frame.setVisible(true);
OUTPUT:
P a g e | 28
import java.awt.event.*;
public LoginForm() {
setTitle("Login Form");
setSize(300, 150);
setResizable(false);
addWindowListener(new WindowAdapter() {
System.exit(0);
});
// Initialize components
add(lblUsername);
add(txtUsername);
add(lblPassword);
add(txtPassword);
add(btnLogin);
add(btnCancel);
btnLogin.addActionListener(this);
btnCancel.addActionListener(this);
// ActionListener implementation
if (e.getSource() == btnLogin) {
System.out.println("Login successful!");
} else {
}
P a g e | 30
txtUsername.setText("");
txtPassword.setText("");
loginForm.setVisible(true);
OUTPUT:
P a g e | 31
import java.awt.event.*;
};
public Calculator() {
setTitle("Calculator");
setSize(250, 250);
setLayout(new BorderLayout());
setResizable(false);
addWindowListener(new WindowAdapter() {
System.exit(0);
});
P a g e | 32
// Initialize components
tfDisplay.setEditable(false);
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
add(tfDisplay, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
// ActionListener implementation
if ("0123456789".contains(command)) {
tfDisplay.setText(tfDisplay.getText() + command);
} else if ("+-*/".contains(command)) {
P a g e | 33
} else if (command.equals("=")) {
calculateResult();
} else if (command.equals("C")) {
tfDisplay.setText("");
switch (operator) {
case "+":
result += operand;
break;
case "-":
result -= operand;
break;
case "*":
result *= operand;
P a g e | 34
break;
case "/":
result /= operand;
break;
tfDisplay.setText(Integer.toString(result));
calculator.setVisible(true);
OUTOUT: