JAVA lab programs CZCA & BA
JAVA lab programs CZCA & BA
1/n).
Aim: To write a Java program to generate Harmonic series (1/1+1, ½+2, 1/n).
Source Code :
import java.util.Scanner;
class HarmonicSeries {
n
Scanner scanner = new Scanner(System.in);
haint n = scanner.nextInt();
double sum = 0;
for (int i = 1; i <= n; i++) {
sum += 1.0 / i + i;
rd
System.out.printf("1/%d + %d = %.2f\n", i, i, 1.0 / i + i);
}
}
}
Output :
ja
1
Program 2: Write a java program to display even, odd and their sum up to
given numbers.
Aim : To write a java program to display even, odd and their sum up to given
numbers.
Source Code :
import java.util.Scanner;
class EvenOddSum {
n
System.out.print("Enter the number: ");
int number = scanner.nextInt();
}
}
Output :
2
Program 3: Write a Java program to find a substring in the given string.
Source code :
import java.io.*;
public class FindSubstring {
n
String c1 = br.readLine();
System.out.println("Enter substring:");
haString c2 = br.readLine();
if (c1.indexOf(c2) != -1) {
System.out.println("The string contains the substring: " + c2);
rd
} else {
System.out.println("Substring not found.");
}
}
}
na
Output :
ja
3
Source code:
import java.util.Scanner;
public class AlphabeticalOrder {
n
System.out.println("Enter all the names:");
for (int i = 0; i < n; i++) {
ha}
names[i] = scanner.nextLine();
}
}
System.out.println("Names in alphabetical order:");
for (String name : names) {
System.out.println(name);
ja
}
}
}
Output :
4
Program 5: Write a java program to demonstrate use of constructor.
Source code:
class Cuboid {
double width;
double height;
double depth;
Cuboid() {
System.out.println("Constructor without Parameter");
n
width = 10;
height = 10;
depth = 10;
}
ha
Cuboid(int a, int b, int c) {
System.out.println("Constructor with parameter");
rd
width = a;
height = b;
depth = c;
}
na
double volume() {
return width * height * depth;
}
}
ja
5
System.out.println("Volume is: " + vol2);
}
}
Output :
n
ha
Program 6: Write a java program to increment method overloading.
Source Code :
rd
class Super {
int x;
na
Super(int x) {
this.x = x;
}
void display() {
ja
int y;
Sub(int x, int y) {
super(x);
6
this.y = y;
}
@Override
void display() {
super.display(); // Call parent's display() for Super's x
System.out.println("Sub y = " + y);
}
}
n
public static void main(String[] args) {
Sub s1 = new Sub(100, 200);
}
}
ha
s1.display();
rd
Outline :
na
ja
Source code :
class Room {
int length;
int breadth;
Room(int x, int y) {
7
length = x;
breadth = y;
}
int area() {
return length * breadth;
}
}
n
height = z;
}
}
}
ha
int volume() {
return length * breadth * height;
Output :
8
Program 8: Write a Java program to implement interfaces.
Source code :
public interface Area {
final static float PI = 3.14159f;
n
@Override
public float compute(float x, float y) {
}
}
ha
return x * y;
return PI * x * x;
}
}
ja
9
}
}
Output :
n
Aim : To write a Java program to create threads using threads class.
ha
Source code:
class A extends Thread {
public void run() {
for (int i = 1; i <= 59; i++) {
System.out.println("From thread A: i = " + i);
rd
}
System.out.println("Exit from A");
}
}
na
}
System.out.println("Exit from B");
}
}
10
System.out.println("Exit from C");
}
}
threadA.start();
threadB.start();
n
threadC.start();
}
}
ha
Output :
rd
na
ja
11
Program 10: Write a Java program to implement exception handling.
Source code:
import java.io.*;
n
}
}
ha
public class MarksException {
public static void main(String[] args) {
try {
int m1, m2, m3, sum;
rd
float avg;
m1 = Integer.parseInt(in.readLine());
m2 = Integer.parseInt(in.readLine());
m3 = Integer.parseInt(in.readLine());
ja
sum = m1 + m2 + m3;
avg = sum / 3.0f;
12
System.out.println("Average: " + avg);
} catch (NumberFormatException e) {
System.out.println("Invalid input format. Please enter numerical
values.");
} catch (IOException e) {
System.out.println("An I/O error occurred while reading input.");
} catch (MarksOutOfBoundException e) {
System.out.println(e.getMessage());
}
}
}
n
Output :
ha
rd
Program 11: Write a Java program to demonstrate Applets.
Source code:
import java.io.*;
import java.awt.*;
import java.applet.*;
ja
13
*** Now create a HTML file using Class name (DrawingPolygon.html). The
HTML code should be as follows. ⬇
HTML :
<!DOCTYPE html>
<html>
<head>
<title>Draw Polygon Applet</title>
</head>
<body>
<applet code="DrawingPolygon.class" width="350" height="300">
n
</applet>
</body>
ha
</html>
***Then compile the Java program, it creates a Class file. Then write this
command in CMD appletviewer DrawingPolygon.html. ↩
rd
na
ja
Janardhan Buchupati
14