JAVA LAB PROGRAMS
JAVA LAB PROGRAMS
Aim:
Algorithm:
Step 2:Declare an integer variable sum and set it to 0. This variable will store the sum of all
integers from 1 to n.
Step 3:Declare an integer variable n and set it to 1000. This represents the upper limit of the
range of integers to be summed.
Step 4:Start a for loop with the loop variable i initialized to the value of n.
Step 5: Set the loop condition to execute the loop as long as i is greater than or equal to 1.Within
the loop, add the current value of i to the sum variable.
Step 6:Decrement i by 1 in each iteration to move to the next integer in the range.Repeat steps 6
to 7 until the loop condition becomes false.
Step 7: After the loop completes, the sum variable will contain the sum of all integers from 1 to
n.Print the result using System.out.println(), showing the message "Sum = " followed by the
value of the sum variable.
Program:
import java.io.*;
import java.util.*;
class Main {
int sum = 0;
int n = 1000;
1
for (int i = n; i >= 1; --i)
sum += i;
Output:
Sum = 500500
Result:
Thus the java program using looping statements has been executed successfully
2
EX.No: 2 PROGRAMS ON ARRAY
Aim:
Algorithm:
Step 2:Declare an integer array variable age and initialize it with three elements: 12, 4, and
5.Print the message "Using for Loop:" to the console.
Step 3:Start a for loop with the loop variable i initialized to 0.Set the loop condition to execute
the loop as long as i is less than the length of the age array.
Step 4:Within the loop, print the element of the age array at the index i.
Step 5:Increment i by 1 in each iteration to move to the next element in the array.Repeat steps 6
to 7 until the loop condition becomes false.
Program:
import java.io.*;
import java.util.*;
class Main {
System.out.println(age[i]);
3
}
Output:
12
Result:
Thus the java program using array has been executed successfully
4
EX.No: 3 PROGRAMS ON STRING HANDLING
Aim:
Algorithm:
a. Declare four string instance variables: str1, str2, str3, and str4.
Print the result of comparing str1 and str2 using the compareTo method of the String
class.
Print the result of comparing str1 and str3 using the compareTo method.
Print the result of comparing str3 and str4 using the compareTo method.
Step 5:Create an object of the "TestString" class named obj.Call the "stringComparison" method
on the obj object.
5
Program:
import java.io.*;
import java.util.*;
Class TestString{
System.out.println(str1.compareTo(str2));
System.out.println(str1.compareTo(str3));
System.out.println(str3.compareTo(str4));
}}
public class StringComparisonExample3 {
public static void main(String args[]){
TestString obj = new TestString();
obj.stringComparison();
}}
Output:
13
-9
Result: Thus the java program using string handling has been demonstrated and executed
successfully.
6
EX.No: 4 PROGRAMS ON CLASSES AND OBJECTS
Aim:
Write a Java Program to calculate Area of Rectangle using classes and object
Algorithm:
Step 2:Define a class named "Rectangle" with two double instance variables: length and breadth.
Step 4:Inside the "RectangleDemo" class, define the main method with the parameter args.
c. Set the length of the myrect object to 10. Set the breadth of the myrect object to 20.
d. Calculate the area by multiplying the length and breadth of the myrect object and store it in
the area variable.
e. Print the result using System.out.println(), showing the message "Area is " followed by the
value of the area variable.
Program:
import java.io.*;
import java.util.*;
class Rectangle {
double length;
double breadth;
7
classRectangleDemo {
double area;
myrect.length = 10;
myrect.breadth = 20;
Output:-
Area is 200.0
Result:
Thus the java program using classes and objects has been demonstrated and executed
successfully
8
EX.No: 5 PROGRAMS ON CONSTRUCTORS
Aim:
Algorithm:
a. Declare two instance variables: eid (integer type) and ename (string type).
Inside the constructor, set the value of eid to 111 and ename to "HARI."
c. Define a method named "disp" with no parameters and a void return type.
Inside the "disp" method, print the "emp id=" followed by the value of eid.
Print the "emp name=" followed by the value of ename.
a. Create an object of the "constructoremp" class named e using the default constructor.
Program:
import java.io.*;
import java.util.*;
Int eid;
9
String ename;
Constructoremp(){
eid=111;
ename="HARI";
voiddisp(){
e.disp();
}}
output:-
emp id=111
emp name=HARI
Result:
Thus the java program using constructors has been executed successfully
10
EX.No: 6 PROGRAMS ON INHERITANCE
Aim:
Algorithm:
a. Declare three instance variables: color (string type), speed (integer type), and size (integer
type).
b. Define a method named "attributes" with no parameters and a void return type.
Inside the "attributes" method, print "Color: " followed by the value of color.
Print "Speed: " followed by the value of speed.
Print "Size: " followed by the value of size.
Step 4:Define another class named "Car" which extends the "Vehicle" class.
a. Declare two additional instance variables: CC (integer type) and gears (integer type).
b. Define a method named "attributescar" with no parameters and a void return type.
Inside the "attributescar" method, call the "attributes" method from the superclass
("Vehicle") to print the common attributes.
Print "CC of Car: " followed by the value of CC.
Print "No of gears of Car: " followed by the value of gears.
Step 7:Inside the "Test" class, define the main method with the parameter args.
b. Set the values of the instance variables of b1: color, speed, size, CC, and gears.
11
c. Call the "attributescar" method on the b1 object to print all the attributes of the car.
Program:
import java.io.*;
import java.util.*;
class Vehicle {
String color;
int speed;
int size;
void attributes() {
}}
int CC;
int gears;
voidattributescar() {
12
System.out.println("No of gears of Car : " + gears);
}}
b1.color = "Blue";
b1.speed = 200 ;
b1.size = 22;
b1.CC = 1000;
b1.gears = 5;
b1.attributescar();
}}
Output :-
Size of Car : 22
CC of Car : 1000
No of gears of Car : 5
Result:
Thus the java program using inheritance has been executed successfully.
13
EX.No: 7 PROGRAMS ON INTERFACE
Aim:
Algorithm:
Step 2:Define an interface named "MyInterface" with two abstract methods: method1 and
method2.
Step 3:Define a class named "Demo" that implements the "MyInterface" interface.
Step 6:Inside the "Test" class, define the main method with the parameter args.
a. Create an object of the "Demo" class and assign it to a reference variable of type
"MyInterface" named obj.
Program:
import java.io.*;
import java.util.*;
interfaceMyInterface{
14
public void method1();
System.out.println("implementation of method1");
System.out.println("implementation of method2");
obj.method1();
}}
Output:
implementation of method1
Result:
Thus the java program for implementing interface has been executed successfully
15
EX.No: 8 PROGRAMS ON MULTITHREADING
Aim:
Algorithm:
Step 2:Define a class named "MultithreadingDemo" that extends the Thread class.
ii. Print a message indicating that the thread with the obtained ID is running.
b. If any exceptions occur while running the thread, catch and handle them by printing
"Exception is caught."
Step 5:Inside the "Multithread" class, define the main method with the parameter args.
a. Declare an integer variable n and set it to 8. This represents the number of threads to be
created.
ii. Call the start() method on the object to start the thread.
Program:
import java.io.*;
import java.util.*;
16
public void run() {
try {
System.out.println(
"Thread " + Thread.currentThread().getId()
+ " is running");
}
catch (Exception e) {
System.out.println("Exception is caught"); }
}}
public class Multithread {
public static void main(String[] args) {
int n = 8; // Number of threads
for (int i = 0; i < n; i++) {
MultithreadingDemo object
= new MultithreadingDemo();
object.start();
} }}
Output:
Thread 15 is running
Thread 14 is running
Thread 16 is running
Thread 12 is running
Thread 11 is running
Thread 13 is running
Thread 18 is running
Thread 17 is running
Result:
Thus the java program for multi threading has been executed successfully
17
EX.No: 8 PROGRAMS ON EXCEPTIONAL HANDLING
Aim:
Algorithm:
Step 3:Inside the "Main" class, define the main method with the parameter args.
Program:
import java.io.*;
import java.util.*;
class Main {
try {
int divideByZero = 5 / 0;
18
System.out.println("Rest of code in try block");
catch (ArithmeticException e) {
Output:
Result:
19
Thus the java program using exceptional handling has been executed successfully
Aim:
Algorithm:
Step 2:Import the required classes (java.io.* and java.util.*) at the beginning of the program.
Step 4:Inside the "CollectionDemo" class, define the main method with the parameter args.
a. Declare and initialize an integer array arr with four elements: 1, 2, 3, and 4.
ii. Add key-value pairs (1, "collection") and (2, "collections") to the Hashtable using
e. Print the first element of the array arr to the console using System.out.println(arr[0]).
f. Print the first element of the Vector v to the console using System.out.println(v.elementAt(0)).
g. Print the value associated with the key 1 in the Hashtable h to the console using
System.out.println(h.get(1)).
Program:
import java.io.*;
20
import java.util.*;
class CollectionDemo {
v.addElement(1);
v.addElement(2);
h.put(1, "collection");
h.put(2, "collections");
System.out.println(arr[0]);
System.out.println(v.elementAt(0));
System.out.println(h.get(1));
Output:
Collections
Result:
21
Thus the java program using collection framework has been executed successfully
22