Java Practical File
Java Practical File
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
{ System.out.println("Hello, World!");
}}
OUTPUT:
CODE 2
import java.util.Scanner;
int n1 = scanner.nextInt();
int n2 = scanner.nextInt();
if (isPrime(i)) {
scanner.close();
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
OUTPUT:
5
CODE 4
OUTPUT:
CODE 5
import java.util.Scanner;
int n = sc.nextInt();
System.out.println("Enter elements:");
arr[i] = sc.nextInt();
6
int max = Integer.MIN_VALUE, secondMax = Integer.MIN_VALUE;
secondMax = max;
max = num;
secondMax = num;
OUTPUT:
7
CODE 6
import java.util.Scanner;
int i = sc.nextInt();
float f = sc.nextFloat();
double d = sc.nextDouble();
String s = sc.nextLine();
char c = sc.next().charAt(0);
boolean b = sc.nextBoolean();
8
OUTPUT:
CODE 7
9
OUTPUT:
CODE 8
System.out.println("Resultant matrix:");
System.out.println();
10
OUTPUT:
CODE 9
class CalculateArea {
OUTPUT:
11
CODE 10
import java.util.Scanner;
class PrivateVarClass {
this.number = number;
obj.setNumber(input);
obj.printNumber();
OUTPUT:
12
CODE 11
import java.util.Scanner;
class PrivateDirectAccess {
OUTPUT:
CODE 12
class Test {
private int i;
public Test(int i) {
this.i = i;
13
Test obj2 = new Test(20);
obj1.display();
obj2.display();
obj3.display();
OUTPUT:
CODE 13
class Counter {
Counter() {
count++;
new Counter();
new Counter();
new Counter();
14
OUTPUT:
CODE 14
class Obj {
private int a;
static int b;
void set(int a) {
this.a = a;
b = value;
void print() {
obj1.set(1);
obj2.set(2);
obj3.set(3);
Obj.setStatic(20);
15
obj1.print();
obj2.print();
obj3.print();
OUTPUT:
CODE 15
class A {
class B extends A {
void accessVariables() {
obj.accessVariables();
16
OUTPUT:
CODE 16
abstract class A {
return a * b;
OUTPUT:
17
CODE 17
package pkg;
public int a = 1;
protected int b = 2;
private int d = 4;
pkg/Pkg2Class.java
package pkg;
new Pkg2Class().display();
OUTPUT:
18
CODE 18
try {
} catch (ArithmeticException e) {
try {
} catch (ArrayIndexOutOfBoundsException e) {
OUTPUT:
CODE 19
19
}
t1.start();
t2.start();
OUTPUT:
20