0% found this document useful (0 votes)
2 views58 pages

Java Practical Note Book 2025

The document contains a series of Java programming assignments, each demonstrating different programming concepts such as addition, swapping numbers, checking odd/even, calculating factorials, and more. Each assignment includes code snippets, sample outputs, and dates, showcasing fundamental programming techniques and problem-solving skills. The assignments cover a range of topics from basic arithmetic operations to more complex tasks like matrix addition and command line argument handling.

Uploaded by

sudipmanna088
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)
2 views58 pages

Java Practical Note Book 2025

The document contains a series of Java programming assignments, each demonstrating different programming concepts such as addition, swapping numbers, checking odd/even, calculating factorials, and more. Each assignment includes code snippets, sample outputs, and dates, showcasing fundamental programming techniques and problem-solving skills. The assignments cover a range of topics from basic arithmetic operations to more complex tasks like matrix addition and command line argument handling.

Uploaded by

sudipmanna088
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

Assignment 1.

Addition of Two Numbers


Date:12.03.26

import [Link];
public class Addition {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
int sum = a + b;
[Link]("Sum: " + sum);
[Link]();
}
}

Output:

Enter first number: 5


Enter second number: 3
Sum: 8
Assignment 2. Swap Two Numbers
Date:12.03.26

import [Link];
public class Swap {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number (a): ");
int a = [Link]();
[Link]("Enter second number (b): ");
int b = [Link]();

int temp = a;
a = b;
b = temp;

[Link]("After swapping - a: " + a + ", b: " + b);


[Link]();
}
}

Output:

Enter first number (a): 10


Enter second number (b): 20
After swapping - a: 20, b: 10
Assignment 3. Check Odd or Even Date:
12.03.26

import [Link];

public class OddEven {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
if(num % 2 == 0) {
[Link](num + " is Even.");
} else {
[Link](num + " is Odd.");
}
[Link]();
}
}

Output:

Enter a number: 7
7 is Odd.
Assignment 4. Find Factorial of a Number
Date: 12.03.26

import [Link];

public class Factorial {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
long factorial = 1;
for(int i = 1; i <= num; ++i) {
factorial *= i;
}
[Link]("Factorial of " + num + " = " + factorial);
[Link]();
}
}

Output:

Enter a number: 5
Factorial of 5 = 120
Assignment 5. Check Prime Number Date:
12.03.26

import [Link];

public class PrimeCheck {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
boolean isPrime = true;
if (num <= 1) isPrime = false;
else {
for (int i = 2; i <= [Link](num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if(isPrime)
[Link](num + " is a Prime number.");
else
[Link](num + " is not a Prime number.");
[Link]();
}
}

Output:

Enter a number: 13
13 is a Prime number.
Assignment 6. Check Palindrome Number
Date: 12.03.26

import [Link];

public class PalindromeNumber {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int original = num;
int reversed = 0;

while(num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}

if(original == reversed)
[Link](original + " is a Palindrome.");
else
[Link](original + " is not a Palindrome.");
[Link]();
}
}

Output:

Enter a number: 121


121 is a Palindrome.
Assignment 7. Check Armstrong Number (3-digit)
Date: 12.03.26

import [Link];

public class Armstrong {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int original = num;
int result = 0;

while (original != 0) {
int remainder = original % 10;
result += [Link](remainder, 3);
original /= 10;
}

if(result == num)
[Link](num + " is an Armstrong number.");
else
[Link](num + " is not an Armstrong number.");
[Link]();
}
}

Output:

Enter a number: 153


153 is an Armstrong number.
Assignment 8. Generate Fibonacci Series
Date:19.03.26

import [Link];
public class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of terms: ");
int n = [Link]();
int first = 0, second = 1;
[Link]("Fibonacci Series: " + first + " " + second);

for (int i = 2; i < n; i++) {


int next = first + second;
[Link](" " + next);
first = second;
second = next;
}
[Link]();
}
}

Output:

Enter the number of terms: 7


Fibonacci Series: 0 1 1 2 3 5 8
Assignment 9. Reverse a Number
Date:19.03.26

import [Link];
public class ReverseNumber {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int reversed = 0;

while(num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
[Link]("Reversed Number: " + reversed);
[Link]();
}
}

Output:

Enter a number: 1234


Reversed Number: 4321
Assignment 10. Bubble Sort (Integer Array)
Date: 19.03.26

import [Link];

import [Link];
public class BubbleSort {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int[] arr = new int[5];
[Link]("Enter 5 numbers:");
for(int i=0; i<5; i++) {
arr[i] = [Link]();
}

for(int i=0; i<[Link]-1; i++) {


for(int j=0; j<[Link]-i-1; j++) {
if(arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
[Link]("Sorted Array: " + [Link](arr));
[Link]();
}
}

Output:

Enter 5 numbers:
5 2 8 1 9
Sorted Array: [1, 2, 5, 8, 9]
Assignment 11. Linear Search Date:
19.03.26

import [Link];

public class LinearSearch {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int[] arr = {10, 20, 30, 40, 50};
[Link]("Enter element to search: ");
int key = [Link]();
boolean found = false;

for(int i=0; i<[Link]; i++) {


if(arr[i] == key) {
[Link]("Element found at index: " + i);
found = true;
break;
}
}
if(!found) {
[Link]("Element not found.");
}
[Link]();
}
}

Output:

Enter element to search: 30


Element found at index: 2
Assignment 12. Solve Quadratic Equation
Date: 19.03.26import [Link];

public class QuadraticEquation {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter coefficients a, b, c:");
double a = [Link]();
double b = [Link]();
double c = [Link]();

double discriminant = b * b - 4 * a * c;

if (discriminant > 0) {
double root1 = (-b + [Link](discriminant)) / (2 * a);
double root2 = (-b - [Link](discriminant)) / (2 * a);
[Link]("Roots are real and different.");
[Link]("Root 1 = " + root1);
[Link]("Root 2 = " + root2);
} else if (discriminant == 0) {
double root = -b / (2 * a);
[Link]("Roots are real and same.");
[Link]("Root = " + root);
} else {
[Link]("Roots are complex and different.");
}
[Link]();
}
}

Output:

Enter coefficients a, b, c:
1 -3 2
Roots are real and different.
Root 1 = 2.0
Root 2 = 1.0
Assignment 13. Reverse a String Date:
19.03.26import [Link];

public class ReverseString {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String reversed = "";

for(int i = [Link]()-1; i >=0; i--) {


reversed += [Link](i);
}
[Link]("Reversed String: " + reversed);
[Link]();
}
}

Output:

Enter a string: Hello


Reversed String: olleH
Assignment 14. String Concatenation
Date: 19.03.26import [Link];

public class StringConcat {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first string: ");
String str1 = [Link]();
[Link]("Enter second string: ");
String str2 = [Link]();

String result = str1 + " " + str2;


[Link]("Concatenated String: " + result);
[Link]();
}
}

Output:

Enter first string: Good


Enter second string: Morning
Concatenated String: Good Morning
Assignment 15. Find Largest Among Three Numbers
Date: 19.03.26import [Link];

public class LargestNumber {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter three numbers:");
int a = [Link]();
int b = [Link]();
int c = [Link]();

int largest = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);


[Link]("Largest number is: " + largest);
[Link]();
}
}

Output:

Enter three numbers:


22 45 13
Largest number is: 45
Assignment 16. Check Vowel or Consonant
Date:23.03.26

import [Link];
public class VowelConsonant {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a character: ");
char ch = [Link]().charAt(0);
ch = [Link](ch);

if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {


[Link](ch + " is a Vowel.");
} else {
[Link](ch + " is a Consonant.");
}
[Link]();
}
}

Output:

Enter a character: E
e is a Vowel.
Assignment 17. Calculate Average of Numbers
Date: 23.03.26import [Link];

public class Average {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the count of numbers: ");
int count = [Link]();
double sum = 0;

[Link]("Enter " + count + " numbers:");


for(int i=0; i<count; i++) {
sum += [Link]();
}
double average = sum / count;
[Link]("Average = " + average);
[Link]();
}
}

Output:

Enter the count of numbers: 3


Enter 3 numbers:
10 20 30
Average = 20.0
Assignment 18. Print Multiplication Table
Date:23.03.26

import [Link];
public class MultiplicationTable {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();

for(int i=1; i<=10; i++) {


[Link](num + " x " + i + " = " + (num*i));
}
[Link]();
}
}

Output:

Enter a number: 5
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50
Assignment 19. Check Leap Year Date:
23.03.26import [Link];

public class LeapYear {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a year: ");
int year = [Link]();
boolean isLeap = false;

if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
isLeap = true;
else
isLeap = false;
} else
isLeap = true;
} else {
isLeap = false;
}

if(isLeap)
[Link](year + " is a Leap Year.");
else
[Link](year + " is not a Leap Year.");
[Link]();
}
}

Output:

Enter a year: 2024


2024 is a Leap Year.
Assignment 20. Calculate Power of a Number
Date: 23.03.26import [Link];

public class CalculatePower {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the base number: ");
int base = [Link]();
[Link]("Enter the exponent: ");
int exponent = [Link]();
long result = 1;

for (; exponent != 0; --exponent) {


result *= base;
}
[Link]("Result: " + result);
[Link]();
}
}

Output:

Enter the base number: 2


Enter the exponent: 5
Result: 32
Assignment 21. Command Line Arguments (Sum of Numbers)
Date:27.03.26

public class CommandLineSum {


public static void main(String[] args) {
if ([Link] < 2) {
[Link]("Please provide at least two numbers as
arguments.");
return;
}
int sum = 0;
[Link]("Arguments received: ");
for (String arg : args) {
[Link](arg + " ");
sum += [Link](arg);
}
[Link]("\nSum of arguments: " + sum);
}
}

Compile & Run:

javac [Link]
java CommandLineSum 5 10 15 20

Output:

Arguments received: 5 10 15 20
Sum of arguments: 50
Assignment 22. Matrix Addition Date: :27.03.26

import [Link];

public class MatrixAddition {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows and columns: ");
int rows = [Link]();
int cols = [Link]();

int[][] a = new int[rows][cols];


int[][] b = new int[rows][cols];
int[][] sum = new int[rows][cols];

[Link]("Enter matrix A:");


for (int i=0; i<rows; i++)
for (int j=0; j<cols; j++)
a[i][j] = [Link]();

[Link]("Enter matrix B:");


for (int i=0; i<rows; i++)
for (int j=0; j<cols; j++)
b[i][j] = [Link]();

for (int i=0; i<rows; i++)


for (int j=0; j<cols; j++)
sum[i][j] = a[i][j] + b[i][j];

[Link]("Resultant Matrix:");
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++)
[Link](sum[i][j] + " ");
[Link]();
}
[Link]();
}
}

Output:

Enter number of rows and columns: 2 2


Enter matrix A:
1 2
3 4
Enter matrix B:
5 6
7 8
Resultant Matrix:
6 8
10 12
Assignment 23. Matrix Multiplication
Date::27.03.26

import [Link];
public class MatrixMultiplication {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter rows and columns for matrix A: ");
int r1 = [Link]();
int c1 = [Link]();
[Link]("Enter columns for matrix B: ");
int c2 = [Link]();

int[][] a = new int[r1][c1];


int[][] b = new int[c1][c2];
int[][] prod = new int[r1][c2];

[Link]("Enter matrix A:");


for (int i=0; i<r1; i++)
for (int j=0; j<c1; j++)
a[i][j] = [Link]();

[Link]("Enter matrix B:");


for (int i=0; i<c1; i++)
for (int j=0; j<c2; j++)
b[i][j] = [Link]();

for (int i=0; i<r1; i++) {


for (int j=0; j<c2; j++) {
for (int k=0; k<c1; k++)
prod[i][j] += a[i][k] * b[k][j];
}
}

[Link]("Product Matrix:");
for (int i=0; i<r1; i++) {
for (int j=0; j<c2; j++)
[Link](prod[i][j] + " ");
[Link]();
}
[Link]();
}
}

Output:

Enter rows and columns for matrix A: 2 2


Enter columns for matrix B: 2
Enter matrix A:
1 2
3 4
Enter matrix B:
5 6
7 8
Product Matrix:
19 22
43 50
Assignment 24. Student Class Definition
Date: :27.03.26import [Link];

class Student {
String name;
int rollNo;
double marks;

void display() {
[Link]("Name: " + name + ", Roll No: " + rollNo + ",
Marks: " + marks);
}
}

public class StudentDemo {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Student s1 = new Student();

[Link]("Enter name: ");


[Link] = [Link]();
[Link]("Enter roll no: ");
[Link] = [Link]();
[Link]("Enter marks: ");
[Link] = [Link]();

[Link]();
[Link]();
}
}

Output:

Enter name: John Doe


Enter roll no: 101
Enter marks: 85.5
Name: John Doe, Roll No: 101, Marks: 85.5
Assignment 25. Constructor Demonstration
Date: :27.03.26class Box {

double width, height, depth;

// Default Constructor
Box() {
width = height = depth = 1;
}

// Parameterized Constructor
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}

// Copy Constructor
Box(Box b) {
width = [Link];
height = [Link];
depth = [Link];
}

double volume() {
return width * height * depth;
}
}

public class ConstructorDemo {


public static void main(String[] args) {
Box box1 = new Box();
Box box2 = new Box(2, 3, 4);
Box box3 = new Box(box2);

[Link]("Volume of box1: " + [Link]());


[Link]("Volume of box2: " + [Link]());
[Link]("Volume of box3: " + [Link]());
}
}

Output:
Volume of box1: 1.0
Volume of box2: 24.0
Volume of box3: 24.0
Assignment 26. Inheritance Demonstration
Date: :27.03.26class Animal {

void eat() {
[Link]("Animal is eating.");
}
}

class Dog extends Animal {


void bark() {
[Link]("Dog is barking.");
}
}

public class InheritanceDemo {


public static void main(String[] args) {
Dog dog = new Dog();
[Link](); // Inherited method
[Link](); // Own method
}
}

Output:

Animal is eating.
Dog is barking.
Assignment 27. Interface Implementation
Date: :27.03.26interface Shape {

double calculateArea();
}

class Circle implements Shape {


double radius;
Circle(double r) { radius = r; }
public double calculateArea() {
return [Link] * radius * radius;
}
}

class Rectangle implements Shape {


double length, width;
Rectangle(double l, double w) { length = l; width = w; }
public double calculateArea() {
return length * width;
}
}

public class InterfaceDemo {


public static void main(String[] args) {
Shape circle = new Circle(5);
Shape rectangle = new Rectangle(4, 6);

[Link]("Circle Area: " + [Link]());


[Link]("Rectangle Area: " + [Link]());
}
}

Output:

Circle Area: 78.53981633974483


Rectangle Area: 24.0
Assignment 28. Polymorphism (Method Overriding)
Date: :27.03.26class Bank {

double getRateOfInterest() {
return 0;
}
}

class SBI extends Bank {


double getRateOfInterest() {
return 8.4;
}
}

class ICICI extends Bank {


double getRateOfInterest() {
return 7.3;
}
}

public class PolymorphismDemo {


public static void main(String[] args) {
Bank b1 = new SBI();
Bank b2 = new ICICI();

[Link]("SBI Rate of Interest: " +


[Link]());
[Link]("ICICI Rate of Interest: " +
[Link]());
}
}

Output:

SBI Rate of Interest: 8.4


ICICI Rate of Interest: 7.3
Assignment 29. Autoboxing & Unboxing
Date: :27.03.26public class AutoboxingDemo {

public static void main(String[] args) {


// Autoboxing: primitive to wrapper
Integer intObj = 10;
Double doubleObj = 15.5;

// Unboxing: wrapper to primitive


int i = intObj;
double d = doubleObj;

[Link]("Integer Object: " + intObj);


[Link]("Double Object: " + doubleObj);
[Link]("Primitive int: " + i);
[Link]("Primitive double: " + d);
}
}

Output:

Integer Object: 10
Double Object: 15.5
Primitive int: 10
Primitive double: 15.5
Assignment 30. Garbage Collection Request
Date: :27.03.26public class GarbageCollectionDemo {

public static void main(String[] args) {


Runtime rt = [Link]();
[Link]("Free memory before GC: " + [Link]());

// Creating objects that will be garbage


for (int i=0; i<10000; i++) {
new GarbageCollectionDemo();
}

[Link]("Free memory after object creation: " +


[Link]());
[Link](); // Requesting Garbage Collection
[Link]("Free memory after GC: " + [Link]());
}
}

Output:

Free memory before GC: xxxxxxx


Free memory after object creation: xxxxxxx
Free memory after GC: xxxxxxx
Assignment 31. File Handling (Write & Read)
Date:03.04.26

import [Link].*;
import [Link];

public class FileHandlingDemo {


public static void main(String[] args) {
// Writing to a file
try (FileWriter fw = new FileWriter("[Link]")) {
[Link]("Hello, File Handling in Java!\nThis is a second
line.");
[Link]("Successfully wrote to the file.");
} catch (IOException e) {
[Link]("An error occurred while writing.");
}

// Reading from a file


try (Scanner fileScanner = new Scanner(new File("[Link]"))) {
[Link]("File content:");
while ([Link]()) {
[Link]([Link]());
}
} catch (FileNotFoundException e) {
[Link]("File not found.");
}
}
}

Output:

Successfully wrote to the file.


File content:
Hello, File Handling in Java!
This is a second line.
Assignment 32. Wrapper Classes Demonstration
Date:03.04.26

public class WrapperDemo {


public static void main(String[] args) {
// Converting primitive to wrapper (boxing)
Integer intObj = [Link](100);
Double doubleObj = [Link](55.75);
Character charObj = [Link]('A');

// Converting wrapper to primitive (unboxing)


int i = [Link]();
double d = [Link]();
char c = [Link]();

// Using parse methods


String numStr = "123";
int parsedInt = [Link](numStr);

[Link]("Integer Object: " + intObj);


[Link]("Parsed Integer: " + parsedInt);
[Link]("Character: " + c);
}
}

Output:

Integer Object: 100


Parsed Integer: 123
Character: A
Assignment 33. Exception Handling (Try-Catch)
Date: 03.04.26import [Link];

import [Link];

public class ExceptionHandlingDemo {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

try {
[Link]("Enter numerator: ");
int num = [Link]();
[Link]("Enter denominator: ");
int den = [Link]();

int result = num / den;


[Link]("Result: " + result);

} catch (ArithmeticException e) {
[Link]("Error: Division by zero is not allowed.");
} catch (InputMismatchException e) {
[Link]("Error: Please enter valid integers.");
} finally {
[Link]("This block always executes.");
[Link]();
}
}
}

Output:

Enter numerator: 10
Enter denominator: 0
Error: Division by zero is not allowed.
This block always executes.
Assignment 34. Thread Creation (Extending Thread)
Date: 03.04.26class MyThread extends Thread {

public void run() {


for (int i=1; i<=5; i++) {
[Link]([Link]().getName() + ": " +
i);
try { [Link](500); } catch (InterruptedException e) {}
}
}
}

public class ThreadDemo {


public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();

[Link]("Thread-1");
[Link]("Thread-2");

[Link]();
[Link]();
}
}

Output:

Thread-1: 1
Thread-2: 1
Thread-1: 2
Thread-2: 2
Thread-1: 3
Thread-2: 3
Thread-1: 4
Thread-2: 4
Thread-1: 5
Thread-2: 5
Assignment 35. Java Applet (Basic Structure)
Date: 03.04.26import [Link];

import [Link];

/*
<applet code="MyApplet" width=300 height=200>
</applet>
*/

public class MyApplet extends Applet {


public void paint(Graphics g) {
[Link]("Hello from Java Applet!", 50, 50);
}
}

Output: (When run in appletviewer)

Hello from Java Applet!


Assignment 36. Swing GUI (JFrame & JButton)
Date: 03.04.26import [Link].*;

import [Link].*;

public class SwingDemo extends JFrame implements ActionListener {


JButton button;
JLabel label;

SwingDemo() {
button = new JButton("Click Me");
label = new JLabel("Hello Swing!");
[Link](100, 50, 100, 30);
[Link](100, 100, 100, 30);

[Link](this);

add(button);
add(label);
setSize(300, 200);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e) {


[Link]("Button Clicked!");
}

public static void main(String[] args) {


new SwingDemo();
}
}

Output: (GUI Window with button and label that updates on click)
Assignment 37. JDBC Database Connection
Date: 03.04.26import [Link].*;

public class JDBCDemo {


public static void main(String[] args) {
try {
// Load JDBC driver
[Link]("[Link]");

// Establish connection
Connection con = [Link](
"jdbc:mysql://localhost:3306/testdb", "username",
"password");

// Create statement
Statement stmt = [Link]();

// Execute query
ResultSet rs = [Link]("SELECT * FROM students");

// Process results
while ([Link]()) {
[Link]("ID: " + [Link](1) + ", Name: " +
[Link](2));
}

// Clean up
[Link]();

} catch (Exception e) {
[Link]("JDBC Error: " + [Link]());
}
}
}

Output:

ID: 1, Name: John


ID: 2, Name: Alice
Assignment 38. Socket Programming (Client-Server) Date:
[Link]: 03.04.26import [Link].*;

import [Link].*;

public class Server {


public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(6666);
[Link]("Server waiting for client...");

Socket socket = [Link]();


DataInputStream dis = new DataInputStream([Link]());
String message = [Link]();
[Link]("Client says: " + message);

[Link]();
}
}

[Link]:

import [Link].*;
import [Link].*;

public class Client {


public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 6666);
DataOutputStream dos = new
DataOutputStream([Link]());
[Link]("Hello Server!");
[Link]();
[Link]();
[Link]();
}
}

Output (Server):

Server waiting for client...


Client says: Hello Server!
Assignment 39. Custom Exception Date:
03.04.26class InvalidAgeException extends Exception {

public InvalidAgeException(String message) {


super(message);
}
}

public class CustomExceptionDemo {


static void validateAge(int age) throws InvalidAgeException {
if (age < 18) {
throw new InvalidAgeException("Age must be 18 or above");
}
[Link]("Valid age: " + age);
}

public static void main(String[] args) {


try {
validateAge(15);
} catch (InvalidAgeException e) {
[Link]("Exception caught: " + [Link]());
}
}
}

Output:

Exception caught: Age must be 18 or above


Assignment 40. Method Overloading (Compile-time
Polymorphism) Date: 03.04.26public class MethodOverloadingDemo {

// Method with 2 int parameters


static int add(int a, int b) {
return a + b;
}

// Method with 3 int parameters


static int add(int a, int b, int c) {
return a + b + c;
}

// Method with double parameters


static double add(double a, double b) {
return a + b;
}

public static void main(String[] args) {


[Link]("Sum of 5 and 10: " + add(5, 10));
[Link]("Sum of 5, 10 and 15: " + add(5, 10, 15));
[Link]("Sum of 5.5 and 10.5: " + add(5.5, 10.5));
}
}

Output:

Sum of 5 and 10: 15


Sum of 5, 10 and 15: 30
Sum of 5.5 and 10.5: 16.0
Assignment 41. Left-Aligned Star Pyramid (Right Triangle)
Date:

import [Link];

public class LeftPyramid {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows: ");
int rows = [Link]();

for (int i = 1; i <= rows; i++) {


for (int j = 1; j <= i; j++) {
[Link]("* ");
}
[Link]();
}
[Link]();
}
}

Output:

Enter number of rows: 5


*
* *
* * *
* * * *
* * * * *
Assignment 42. Center-Aligned Star Pyramid (Full Pyramid)
Date:

import [Link];

public class CenterPyramid {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows: ");
int rows = [Link]();

for (int i = 1; i <= rows; i++) {


// Print spaces
for (int j = i; j < rows; j++) {
[Link](" ");
}
// Print stars
for (int k = 1; k <= (2 * i - 1); k++) {
[Link]("*");
}
[Link]();
}
[Link]();
}
}

Output:

Enter number of rows: 5


*
***
*****
*******
*********
Assignment 43. Inverted Star Pyramid Date:

import [Link];

public class InvertedPyramid {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows: ");
int rows = [Link]();

for (int i = rows; i >= 1; i--) {


// Print spaces
for (int j = rows; j > i; j--) {
[Link](" ");
}
// Print stars
for (int k = 1; k <= (2 * i - 1); k++) {
[Link]("*");
}
[Link]();
}
[Link]();
}
}

Output:

Enter number of rows: 5


*********
*******
*****
***
*
Assignment 44. Diamond Pattern (Combination of Pyramid &
Inverted Pyramid) Date:

import [Link];

public class DiamondPattern {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows (half of diamond): ");
int rows = [Link]();

// Upper pyramid
for (int i = 1; i <= rows; i++) {
for (int j = i; j < rows; j++) {
[Link](" ");
}
for (int k = 1; k <= (2 * i - 1); k++) {
[Link]("*");
}
[Link]();
}

// Lower inverted pyramid


for (int i = rows - 1; i >= 1; i--) {
for (int j = rows; j > i; j--) {
[Link](" ");
}
for (int k = 1; k <= (2 * i - 1); k++) {
[Link]("*");
}
[Link]();
}
[Link]();
}
}

Output:

Enter number of rows (half of diamond): 4


*
***
*****
*******
*****
***
*
Assignment 45. Hollow Star Pyramid Date:

import [Link];

public class HollowPyramid {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows: ");
int rows = [Link]();

for (int i = 1; i <= rows; i++) {


// Print spaces
for (int j = i; j < rows; j++) {
[Link](" ");
}
// Print stars with hollow center
for (int k = 1; k <= (2 * i - 1); k++) {
if (k == 1 || k == (2 * i - 1) || i == rows) {
[Link]("*");
} else {
[Link](" ");
}
}
[Link]();
}
[Link]();
}
}

Output:

Enter number of rows: 5


*
* *
* *
* *
*********
Assignment 46. Number Pyramid with Stars
Date:

import [Link];

public class NumberStarPyramid {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows: ");
int rows = [Link]();

for (int i = 1; i <= rows; i++) {


// Print spaces
for (int j = i; j < rows; j++) {
[Link](" ");
}
// Print numbers and stars
for (int k = 1; k <= (2 * i - 1); k++) {
if (k % 2 == 0) {
[Link]("*");
} else {
[Link](i);
}
}
[Link]();
}
[Link]();
}
}

Output:

Enter number of rows: 4


1
2*2
3*3*3
4*4*4*4
Assignment 47. Right-Aligned Star Pyramid
Date:

import [Link];

public class RightPyramid {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows: ");
int rows = [Link]();

for (int i = 1; i <= rows; i++) {


// Print spaces
for (int j = 1; j <= rows - i; j++) {
[Link](" ");
}
// Print stars
for (int k = 1; k <= i; k++) {
[Link]("*");
}
[Link]();
}
[Link]();
}
}

Output:

Enter number of rows: 5


*
**
***
****
*****
Assignment 48. Pascal's Triangle with Stars
Date:

import [Link];

public class PascalStarTriangle {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows: ");
int rows = [Link]();

for (int i = 0; i < rows; i++) {


// Print spaces
for (int j = 0; j < rows - i - 1; j++) {
[Link](" ");
}
// Print stars in Pascal's pattern
int number = 1;
for (int k = 0; k <= i; k++) {
[Link]("* ");
}
[Link]();
}
[Link]();
}
}

Output:

Enter number of rows: 5


*
* *
* * *
* * * *
* * * * *
Assignment 49. Hourglass Star Pattern Date:

import [Link];

public class HourglassPattern {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows: ");
int rows = [Link]();

// Upper inverted pyramid


for (int i = rows; i >= 1; i--) {
for (int j = rows; j > i; j--) {
[Link](" ");
}
for (int k = 1; k <= (2 * i - 1); k++) {
[Link]("*");
}
[Link]();
}

// Lower pyramid
for (int i = 2; i <= rows; i++) {
for (int j = rows; j > i; j--) {
[Link](" ");
}
for (int k = 1; k <= (2 * i - 1); k++) {
[Link]("*");
}
[Link]();
}
[Link]();
}
}

Output:

Enter number of rows: 5


*********
*******
*****
***
*
***
*****
*******
*********
Assignment 50. Christmas Tree Pattern Date:

import [Link];

public class ChristmasTree {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of sections: ");
int sections = [Link]();

// Tree sections
for (int s = 1; s <= sections; s++) {
for (int i = 1; i <= s + 2; i++) {
// Print spaces
for (int j = i; j < sections + 3; j++) {
[Link](" ");
}
// Print stars
for (int k = 1; k <= (2 * i - 1); k++) {
[Link]("*");
}
[Link]();
}
}

// Tree trunk
for (int i = 1; i <= 2; i++) {
for (int j = 1; j < sections + 2; j++) {
[Link](" ");
}
[Link]("***");
}
[Link]();
}
}

Output:

Enter number of sections: 3


*
***
*****
*
***
*****
*******
*
***
*****
*******
*********
***
***
Assignment 51. Arrow Head Pattern Date:

import [Link];

public class ArrowHead {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows: ");
int rows = [Link]();

// Upper part of arrow


for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
[Link]("* ");
}
[Link]();
}

// Lower part of arrow


for (int i = rows - 1; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
[Link]("* ");
}
[Link]();
}
[Link]();
}
}

Output:

Enter number of rows: 4


*
* *
* * *
* * * *
* * *
* *
*
Assignment 52. Binary Number Pyramid Date:

import [Link];

public class BinaryPyramid {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows: ");
int rows = [Link]();

for (int i = 1; i <= rows; i++) {


// Print spaces
for (int j = i; j < rows; j++) {
[Link](" ");
}
// Print binary pattern
for (int k = 1; k <= (2 * i - 1); k++) {
[Link](k % 2);
}
[Link]();
}
[Link]();
}
}

Output:

Enter number of rows: 5


1
101
10101
1010101
101010101

You might also like