0% found this document useful (0 votes)
4 views20 pages

Java Practical File

The document contains a comprehensive list of programming tasks and code examples primarily focused on Java programming concepts, including basic syntax, data types, string manipulation, object-oriented programming, exception handling, and multithreading. Each task is accompanied by code snippets that demonstrate the implementation of the respective concepts. The document serves as a practical guide for learners to understand and apply various programming techniques.

Uploaded by

devanshb377
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)
4 views20 pages

Java Practical File

The document contains a comprehensive list of programming tasks and code examples primarily focused on Java programming concepts, including basic syntax, data types, string manipulation, object-oriented programming, exception handling, and multithreading. Each task is accompanied by code snippets that demonstrate the implementation of the respective concepts. The document serves as a practical guide for learners to understand and apply various programming techniques.

Uploaded by

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

1

INDEX
S no. CODE SIGNATURE
1. Compiling and running a "hello world" program in
Windows
2. Write a program to calculate the prime number
from n1 to n2. n1 and n2 are entered
through keyboard.
3. Write a program to concatenate two strings. One
string is obtained from command
line and other from keyboard.
4. Write a program to convert string into integer and
integer into string.
5. 6. Write a program to find the 2nd maximum
number from an integer array.
6. Write a program to accept the input in variables of
all data type from keyboard and
display these on the screen.
7. Write a program to check whether a string is empty,
length of string, remove the
white spaces, compare two string, convert the
upper case string to lower case.
8. Write a program to multiply two matrix of 3 X 3 and
print the result in matrix format.
9. Write a program to calculate the area of various
shapes with the function name
'Area' in class name 'CalculateArea'.
10. Write a program to initialize the private integer
variable of a class with the value enter
through keyboard and print the value of this
variable.
11. Write a program to initialize the private variable of a
class without the use of public
functions. Print the value of the variable also.
12. Write a program in which there is a display function
in class Test. Display function
print the value of private variable i and j of Test
class. Value of i and j should be 0
when we call display by object objl of Test class,
Value of i should be 10 and j
remain 0 when we call it by object obj2 of Test class,
Value of i and j should be 20
and 30 when we call it by object obj3 of Test class.
13. Write a program to count the number of object
created of a class.Hint : No use of loop.

2
14. Write a program to add the value of variable i & j of
two object obj1 and obj2
respectively and store the addition of both variable
in object obj3. All three objects
are part of a same class and i & j are private
variables.
15. There is a class A which have one default, one
private, one protected and one public
variable and there is a public method AccessAll()
which store and print the value in
all variable. Derive class A into another class B. Also
write here a public function in
class B which access all 4 variable of class A. Make
the object of Class A and Class B
in main and see the result. Write the output and
discuss the reasons.
16. Write a program which have an abstract class Abst
with abstract function, calculate
with two integers as parameter. Define this
calculate function, in the derived class
Divide of Abst, which divide two numbers. Also
define calculate function, in another
derived class Multiply of Abst, which multiply two
numbers. Object name should be
Calculator. No other object is required.
17. Create a package pkg1, which contain a public class
Pkg1Class, which have integer
variables of type private, default, public, protected
each one for each. Write another
package pkg2, which have a class Pkg2Class.
Pkg2Class extends the Pkg1Class of
pkg1 package. Pkg2Class also have main function.
Try to access all the variables
declared in Pkg1Class in the main function of
Pkg2Class with the object of Pkg1Class
and Pkg2Class and observe the result. Write down
your findings.
18. Write a simple program of exception handling in
which you handle 1 checked and 1
unchecked exception.
19. Write a program for multithreading using Thread
class and another program using
Runnable interface.

3
CODE 1

public class HelloWorld {

public static void main(String[] args)

{ System.out.println("Hello, World!");

}}

OUTPUT:

CODE 2

import java.util.Scanner;

public class PrimeInRange {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the starting number (n1): ");

int n1 = scanner.nextInt();

System.out.print("Enter the ending number (n2): ");

int n2 = scanner.nextInt();

System.out.println("Prime numbers between " + n1 + " and " + n2 + " are:");

for (int i = n1; i <= n2; i++) {

if (isPrime(i)) {

System.out.print(i + " ");

scanner.close();

public static boolean isPrime(int num) {

if (num <= 1)

return false;

4
for (int i = 2; i <= Math.sqrt(num); i++) {

if (num % i == 0)

return false;

return true;

OUTPUT:

CODE 3

public class ConcatStrings {

public static void main(String[] args) {

java.util.Scanner sc = new java.util.Scanner(System.in);

System.out.print("Enter a string: ");

String inputStr = sc.nextLine();

String cmdStr = args.length > 0 ? args[0] : "";

System.out.println("Concatenated String: " + cmdStr + inputStr);

OUTPUT:

5
CODE 4

public class StringConversion {

public static void main(String[] args) {

String str = "123";

int num = Integer.parseInt(str);

System.out.println("String to int: " + num);

String str2 = Integer.toString(num);

System.out.println("Int to string: " + str2);

OUTPUT:

CODE 5

import java.util.Scanner;

public class SecondMax {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter array size: ");

int n = sc.nextInt();

int[] arr = new int[n];

System.out.println("Enter elements:");

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

arr[i] = sc.nextInt();

6
int max = Integer.MIN_VALUE, secondMax = Integer.MIN_VALUE;

for (int num : arr) {

if (num > max) {

secondMax = max;

max = num;

} else if (num > secondMax && num < max) {

secondMax = num;

System.out.println("Second maximum: " + secondMax);

OUTPUT:

7
CODE 6

import java.util.Scanner;

public class AllDataTypes {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter an integer: ");

int i = sc.nextInt();

System.out.print("Enter a float: ");

float f = sc.nextFloat();

System.out.print("Enter a double: ");

double d = sc.nextDouble();

System.out.print("Enter a string: ");

sc.nextLine(); // consume the newline after previous input

String s = sc.nextLine();

System.out.print("Enter a character: ");

char c = sc.next().charAt(0);

System.out.print("Enter a boolean: ");

boolean b = sc.nextBoolean();

System.out.println("\n--- Displaying All Entered Values ---");

System.out.println("Integer: " + i);

System.out.println("Float: " + f);

System.out.println("Double: " + d);

System.out.println("String: " + s);

System.out.println("Character: " + c);

System.out.println("Boolean: " + b);

8
OUTPUT:

CODE 7

public class StringFunctions {

public static void main(String[] args) {

String str1 = " Hello World ";

String str2 = "hello world";

System.out.println("Is empty: " + str1.isEmpty());

System.out.println("Length: " + str1.length());

System.out.println("Trimmed: " + str1.trim());

System.out.println("Compare: " + str1.trim().equals(str2));

System.out.println("Upper to Lower: " + str1.toLowerCase());

9
OUTPUT:

CODE 8

public class MatrixMultiplication {

public static void main(String[] args) {

int[][] a = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

int[][] b = { {9, 8, 7}, {6, 5, 4}, {3, 2, 1} };

int[][] result = new int[3][3];

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

for(int j = 0; j < 3; j++) {

for(int k = 0; k < 3; k++) {

result[i][j] += a[i][k] * b[k][j];

System.out.println("Resultant matrix:");

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

for(int j = 0; j < 3; j++) {

System.out.print(result[i][j] + " ");

System.out.println();

10
OUTPUT:

CODE 9

class CalculateArea {

void Area(int side) {

System.out.println("Area of square: " + (side * side));

void Area(int length, int breadth) {

System.out.println("Area of rectangle: " + (length * breadth));

void Area(double radius) {

System.out.println("Area of circle: " + (3.14159 * radius * radius));

public static void main(String[] args) {

CalculateArea obj = new CalculateArea();

obj.Area(5); // Calls square method

obj.Area(4, 6); // Calls rectangle method

obj.Area(3.5); // Calls circle method

OUTPUT:

11
CODE 10

import java.util.Scanner;

class PrivateVarClass {

private int number;

public void setNumber(int number) {

this.number = number;

public void printNumber() {

System.out.println("Value: " + number);

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int input = sc.nextInt();

PrivateVarClass obj = new PrivateVarClass();

obj.setNumber(input);

obj.printNumber();

OUTPUT:

12
CODE 11

import java.util.Scanner;

class PrivateDirectAccess {

private int number;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

PrivateDirectAccess obj = new PrivateDirectAccess();

System.out.print("Enter a number: ");

obj.number = sc.nextInt(); // Direct access allowed inside same class

System.out.println("Value: " + obj.number);

OUTPUT:

CODE 12

class Test {

private int i;

public Test(int i) {

this.i = i;

public void display() {

System.out.println("Value of i: " + i);

public static void main(String[] args) {

Test obj1 = new Test(10);

13
Test obj2 = new Test(20);

Test obj3 = new Test(30);

obj1.display();

obj2.display();

obj3.display();

OUTPUT:

CODE 13

class Counter {

static int count = 0;

Counter() {

count++;

public static void main(String[] args) {

new Counter();

new Counter();

new Counter();

System.out.println("Number of objects created: " + count);

14
OUTPUT:

CODE 14

class Obj {

private int a;

static int b;

void set(int a) {

this.a = a;

static void setStatic(int value) {

b = value;

void print() {

System.out.println("a = " + a + ", b = " + b);

public class StaticExample {

public static void main(String[] args) {

Obj obj1 = new Obj();

Obj obj2 = new Obj();

Obj obj3 = new Obj();

obj1.set(1);

obj2.set(2);

obj3.set(3);

Obj.setStatic(20);

15
obj1.print();

obj2.print();

obj3.print();

OUTPUT:

CODE 15

class A {

private int a = 10;

protected int b = 20;

public int c = 30;

class B extends A {

void accessVariables() {

// System.out.println("a: " + a); // private - not accessible

System.out.println("b: " + b);

System.out.println("c: " + c);

public class AccessTest {

public static void main(String[] args) {

B obj = new B();

obj.accessVariables();

16
OUTPUT:

CODE 16

abstract class A {

abstract int calculate(int a, int b);

class Multiply extends A {

int calculate(int a, int b) {

return a * b;

public class AbstractDemo {

public static void main(String[] args) {

A obj = new Multiply();

System.out.println("Result: " + obj.calculate(5, 6));

OUTPUT:

17
CODE 17

package pkg;

public class Pkg1Class {

public int a = 1;

protected int b = 2;

int c = 3; // default access (package-private)

private int d = 4;

pkg/Pkg2Class.java

package pkg;

public class Pkg2Class extends Pkg1Class {

public void display() {

System.out.println("a: " + a); // public - accessible

System.out.println("b: " + b); // protected - accessible in subclass in same package

// c and d not accessible here (commented out)

public static void main(String[] args) {

new Pkg2Class().display();

OUTPUT:

18
CODE 18

public class ExceptionDemo {

public static void main(String[] args) {

try {

int result = 10 / 0; // unchecked exception

} catch (ArithmeticException e) {

System.out.println("Caught ArithmeticException: " + e);

try {

int[] arr = new int[2];

arr[3] = 5; // unchecked exception

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println("Caught ArrayIndexOutOfBoundsException: " + e);

OUTPUT:

CODE 19

// Using Thread class by extending it

class ThreadA extends Thread {

public void run() {

System.out.println("Running thread using Thread class.");

class ThreadB implements Runnable {

public void run() {

System.out.println("Running thread using Runnable interface.");

19
}

public class MultiThreadingExample {

public static void main(String[] args) {

ThreadA t1 = new ThreadA();

t1.start();

Thread t2 = new Thread(new ThreadB());

t2.start();

OUTPUT:

20

You might also like