0% found this document useful (0 votes)
15 views14 pages

JAVA lab programs CZCA & BA

The document contains multiple Java programs demonstrating various programming concepts such as generating harmonic series, displaying even and odd numbers, finding substrings, sorting strings alphabetically, using constructors, method overloading, single inheritance, implementing interfaces, creating threads, exception handling, and applet demonstration. Each program includes source code and aims to illustrate specific programming techniques. Additionally, there are instructions for compiling and running the applet program.

Uploaded by

janabuchupati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views14 pages

JAVA lab programs CZCA & BA

The document contains multiple Java programs demonstrating various programming concepts such as generating harmonic series, displaying even and odd numbers, finding substrings, sorting strings alphabetically, using constructors, method overloading, single inheritance, implementing interfaces, creating threads, exception handling, and applet demonstration. Each program includes source code and aims to illustrate specific programming techniques. Additionally, there are instructions for compiling and running the applet program.

Uploaded by

janabuchupati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Program 1: Write a Java program to generate Harmonic series (1/1+1, ½+2,

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 {

public static void main(String[] args) {

n
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the value of n: ");

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);
}

System.out.println("Sum of the series: " + sum);


na

}
}

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 {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

n
System.out.print("Enter the number: ");
int number = scanner.nextInt();

haint evenSum = 0, oddSum = 0;

for (int i = 1; i <= number; i++) {


if (i % 2 == 0) {
rd
evenSum += i;
System.out.println(i + " is even");
} else {
oddSum += i;
System.out.println(i + " is odd");
na

}
}

System.out.println("Sum of even numbers: " + evenSum);


System.out.println("Sum of odd numbers: " + oddSum);
}
ja

Output :

2
Program 3: Write a Java program to find a substring in the given string.

Aim : To write a Java program to find a substring in the given string.

Source code :
import java.io.*;
public class FindSubstring {

public static void main(String args[]) throws IOException {


BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));

System.out.println("Enter main string:");

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

Program 4: Write a java program to arrange given strings in alphabetical


order.

Aim : To write a java program to arrange given strings in alphabetical order.

3
Source code:
import java.util.Scanner;
public class AlphabeticalOrder {

public static void main(String args[]) {


Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number of names you want to enter:");


int n = scanner.nextInt();

String[ ] names = new String[n];

n
System.out.println("Enter all the names:");
for (int i = 0; i < n; i++) {

ha}
names[i] = scanner.nextLine();

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


for (int j = 0; j < n - i - 1; j++) {
if (names[j].compareTo(names[j + 1]) > 0) {
rd
String temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;
}
na

}
}
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.

Aim : To 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

public class ConstructorDemo {

public static void main(String args[]) {

Cuboid cuboid1 = new Cuboid();


double vol1 = cuboid1.volume();
System.out.println("Volume is: " + vol1);

Cuboid cuboid2 = new Cuboid(8, 11, 19);


double vol2 = cuboid2.volume();

5
System.out.println("Volume is: " + vol2);
}
}

Output :

n
ha
Program 6: Write a java program to increment method overloading.

Aim : To 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

System.out.println("Super x = " + x);


}
}

class Sub extends Super {

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);
}
}

public class OverrideTest {

n
public static void main(String[] args) {
Sub s1 = new Sub(100, 200);

}
}

ha
s1.display();
rd
Outline :
na
ja

Program 7: Write a Java program application of single inheritence.

Aim : To write a Java program application of single inheritence.

Source code :
class Room {
int length;
int breadth;

Room(int x, int y) {

7
length = x;
breadth = y;
}

int area() {
return length * breadth;
}
}

class Bedroom extends Room {


int height;

Bedroom(int x, int y, int z) {


super(x, y);

n
height = z;
}

}
}
ha
int volume() {
return length * breadth * height;

public class InheritanceExample {


rd
public static void main(String[] args) {
Bedroom room1 = new Bedroom(14, 12, 10);

int area = room1.area();


int volume = room1.volume();
na

System.out.println("Area: " + area);


System.out.println("Volume: " + volume);
}
}
ja

Output :

8
Program 8: Write a Java program to implement interfaces.

Aim : To write a java program to implement interfaces.

Source code :
public interface Area {
final static float PI = 3.14159f;

float compute(float x, float y);


}

class Rectangle implements Area {

n
@Override
public float compute(float x, float y) {

}
}

ha
return x * y;

class Circle implements Area {


rd
@Override
public float compute(float x, float y) {
if (y == 0) {
return 0f;
}
na

return PI * x * x;
}
}
ja

public class InterfaceExample {

public static void main(String[] args) {


Area rectangle = new Rectangle();
Circle circle = new Circle();

float rectangleArea = rectangle.compute(10, 20);


float circleArea = circle.compute(10, 0);

System.out.println("Area of rectangle: " + rectangleArea);


System.out.println("Area of circle: " + circleArea);

9
}
}

Output :

Program 9: Write a Java program to create threads using threads class.

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

class B extends Thread {


public void run() {
for (int j = 1; j <= 4; j++) {
System.out.println("From thread B: j = " + j);
ja

}
System.out.println("Exit from B");
}
}

class C extends Thread {


public void run() {
for (int k = 1; k <= 5; k++) {
System.out.println("From thread C: k = " + k);
}

10
System.out.println("Exit from C");
}
}

public class ThreadTest {


public static void main(String[] args) {
A threadA = new A();
B threadB = new B();
C threadC = new C();

threadA.start();
threadB.start();

n
threadC.start();
}
}

ha
Output :
rd
na
ja

11
Program 10: Write a Java program to implement exception handling.

Aim : To write a Java program to implement exception handling.

Source code:
import java.io.*;

class MarksOutOfBoundException extends Exception {


public MarksOutOfBoundException() {

super("Marks are out of bounds. Please enter values between 0 and


100.");

n
}
}

ha
public class MarksException {
public static void main(String[] args) {
try {
int m1, m2, m3, sum;
rd
float avg;

java.io.DataInputStream in = new java.io.DataInputStream(System.in);

System.out.println("Enter three subject marks (0-100):");


na

m1 = Integer.parseInt(in.readLine());
m2 = Integer.parseInt(in.readLine());
m3 = Integer.parseInt(in.readLine());
ja

for (int mark : new int[]{m1, m2, m3}) {


if (mark < 0 || mark > 100) {
throw new MarksOutOfBoundException();
}
}

sum = m1 + m2 + m3;
avg = sum / 3.0f;

System.out.println("Sum of marks: " + sum);

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.

Aim : To write a Java program to demonstrate Applets.


na

Source code:
import java.io.*;
import java.awt.*;
import java.applet.*;
ja

public class DrawingPolygon extends Applet


{
public void paint(Graphics g)
{
Int[ ] x = {70, 150, 190, 180, 100};
Int[ ] y = {80, 110, 160, 190, 100};
int n=6;
g.drawPolygon(x, y, 6);
}
}

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy