java Lab Manual
java Lab Manual
1.Java Program to display “Hello World” and display the size of all the data types.
class SizeOfDataTypes
System.out.println("Hello World");
return numInstances;
numInstances++;
InstanceCounter() {
InstanceCounter.addInstance();
float pi=22/7f;
System.out.println("Value of Pi = "+pi);
3.Java Program to implement string operations string length, string concatenate, substring
class StringOperations {
String firstname="Sachin";
String name=firstname.concat(middlename).concat(lastname);
import java.util.Scanner;
class MaxOfThreeNumbers {
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
temp=a>b?a:b;
largest=c>temp?c:temp;
if(num%2==0)
else
import java.util.Scanner;
class Box {
double width;
double height;
double depth;
Box() {
width = 12.7d;
height = 15.0d;
depth = 5.6d;
width = w;
height = h;
depth = d;
double volume() {
class Jain {
double vol;
vol = Box1.volume();
vol = Box2.volume();
import java.util.Scanner;
class Car {
this.name=name;
this.miles=miles;
System.out.println(name+" - "+miles);
class Main {
Car cars[] = {
};
car.printDetails();
class Bicycle {
this.gear = gear;
this.speed = speed;
speed -= decrement;
speed += increment;
return "No of gears are " + gear + "\nSpeed of bicycle is " + speed;
super(gear, speed);
seatHeight = startHeight;
seatHeight = newValue;
// Main class
class Main1 {
public static void main(String[] args) {
System.out.println(mb.toString());
OUTPUT:
No of gears are 3
seat height is 25
interface Character {
void attack();
interface Weapon {
void use();
class Main3 {
warrior.attack();
warrior.use();
mage.attack();
mage.use();
OUTPUT:
//SumNumsInteractive java
import java.applet.Applet;
import java.awt.*;
text1=new TextField(10);
text2=new TextField(10);
text1.setText("0");
text2.setText("0");
add(text1);
add(text2);
int num1=0;
int num2=0;
int sum;
String s1,s2,s3;
g.drawString("Input a number in each box", 10, 50);
try {
s1=text1.getText();
num1=Integer.parseInt(s1);
s2=text2.getText();
num2=Integer.parseInt(s2);
catch(Exception el)
sum=num1+num2;
g.drawString (str,100,125);
repaint();
return true;
import java.util.Scanner;
try{
number1=sc.nextInt();
number2=sc.nextInt();
output=number1/number2;
System.out.println("Result:"+output);
catch(ArithmeticException e) {
System.out.println("Error:"+e.getMessage());
System.out.println("Error:"+e);
System.out.println("...End of Program...");
OUTPUT
Output 1
Result:2
...End of Program...
Output 2
Error: by zero
...End of Program...
12. Java program to add two integers and two float numbers. When no arguments are supplied give a
default value to calculate the sum. Use method overloading.
import java.util.Scanner;
return a + b;
return a + b;
// Integer addition
System.out.println("Enter two integers (separated by space) (or press Enter for default):");
if (intInput.isEmpty()) {
} else {
int a = Integer.parseInt(intValues[0]);
int b = Integer.parseInt(intValues[1]);
// Float addition
System.out.println("Enter two float numbers (separated by space) (or press Enter for default): ");
if (floatInput.isEmpty()) {
} else {
float x = Float.parseFloat(floatValues[0]);
float y = Float.parseFloat(floatValues[1]);
scanner.close();
OUTPUT
Enter two integers (separated by space) (or press Enter for default):
15 25
Sum of integers: 40
Enter two float numbers (separated by space) (or press Enter for default):
63.2 56.1
class Employee {
// Method to be overridden
System.out.println("Employee is working");
@Override
@Override
@Override
public void work() {
System.out.println("---------------");
emp.work();
emp.work();
emp.work();
emp.work();
OUTPUT
----------------------------
Employee is working
14. Java program to catch negative array size Exception. This exception is caused when the array is
initialized to negative values.
import java.util.*;
try {
} catch (NegativeArraySizeException e) {
} catch (InputMismatchException e) {
scanner.next();
finally {
scanner.close();
OUTPUT:
at java.base/java.util. Scanner.throwFor(Scanner.java:939)
at java base java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
15. Java program to handle null pointer exception and use the “finally” method to display a message to
the user.
import java.util.*;
try {
if (input.isEmpty()) {
input = null;
} catch (NullPointerException e) {
} finally {
System.out.println("END OF PROGRAM.");
scanner.close();
}OUTPUT
-END OF PROGRAM
Output 1: No Input
Enter a string:
-END OF PROGRAM-
16. Java program to import user-defined packages.
Step 1: Create a package "shape" inside ‘src’ folder in the working project.
Step 2: Inside "shape" create three classes 'Circle java', 'Square java", "Triangle java"
Step 3: Outside the "shape" package create main class file 'CustomPack.java'
Circle.java
package shape;
public Circle(double r) |
radius=r;
return (3.14*radius*radius);
Square.java
package shape;
public Square(int s) {
side=s;
return (side*side);
Triangle.java
package shape;
sidel=s1;
side2=s2;
side3=s3;
double a=Math.sqrt((s-side1)+(s-side2)+(s-side3));
return a;
CustomPack.java
package LabPrograms;
import shape.*;
import java.util.*;
int s=sc.nextInt();
double r=sc.nextDouble();
int sl=sc.nextInt();
int s2=sc.nextInt();
int s3=sc.nextInt();
OUTPUT
Area of Square is 49
6
5
import java.util.Scanner;
int reversedNumber = 0;
while (number != 0) {
number /= 10;
if (originalNumber == reversedNumber) {
} else {
scanner.close();
}OUTPUT
Output 1
Output 2
9669 is a palindrome
18. Java program to find the factorial of a list of numbers reading input as command line argument.
import java.math.BigInteger;
try {
} catch (NumberFormatException e) {
result = result.multiply(BigInteger.valueOf(i));
return result;
OUTPUT
Factorial of 5=120
Factorial of 4=24
Factorial of 7=5040
19. Java program to display all prime numbers between two limits.
import java.util.Scanner;
System.out.println("Prime numbers between " + lowerLimit + " and " + upperLimit + " are:");
if (isPrime(num)) {
if (number <= 1) {
return false;
if (number % i == 0) {
return false;
return true;
OUTPUT
this.threadName = threadName;
@Override
try {
} catch (InterruptedException e) {
t1.start();
t2.start();
OUTPUT
Thread-2 started.
Thread-2: 0
Thread-1 started.
Thread-1:0
Thread-2: 1
Thread-1: 1
Thread-2: 2
Thread-1: 2
Thread-2: 3
Thread-1: 3
Thread-2:4
Thread-1:4
Thread-2 finished.
Thread-1 finished.