0% found this document useful (0 votes)
2 views9 pages

java lab

The document contains several Java programs demonstrating different programming concepts. It includes programs for calculating factorials from command line arguments, finding prime numbers within a range, sorting elements with exception handling, performing string operations, calculating areas of different geometric figures, and implementing constructor overloading. Each program is accompanied by sample outputs and explanations of the concepts used.

Uploaded by

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

java lab

The document contains several Java programs demonstrating different programming concepts. It includes programs for calculating factorials from command line arguments, finding prime numbers within a range, sorting elements with exception handling, performing string operations, calculating areas of different geometric figures, and implementing constructor overloading. Each program is accompanied by sample outputs and explanations of the concepts used.

Uploaded by

Sathiya Prabha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

write a java program to find factorial of list of numbers reading input as command line

arguments

class Factorial

public static void main(String[] arg)

int[] num=new int[10];

if([Link]==0)

[Link]("No Command Line argument passed.");

return;

for(int i=0;i<[Link];i++)

num[i]=[Link](arg[i]);

for(int i=0;i<[Link];i++)

int fact=1;

for(int j=1;j<=num[i];j++)

fact *=j;

[Link]("The factorial of "+arg[i]+" is : "+fact);

}
OUTPUT:
Write a program to display all prime numbers between two limits.

class PrimeNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter first limit: ");


int start = [Link]();

[Link]("Enter second limit: ");


int end = [Link]();

[Link]("Prime numbers are:");

for (int i = start; i <= end; i++) {


int count = 0;

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


if (i % j == 0) {
count++;
}
}

if (count == 2) {
[Link](i + " ");
}
}
}
}

OUTPUT:

Enter first limit: 10


Enter second limit: 30
Prime numbers are:
11 13 17 19 23 29

Write a program to sort list of elements in ascending and descending order and show the

exception handling.
import [Link].*;

class SortDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

try {
[Link]("Enter the number of elements: ");
int n = [Link]();

int arr[] = new int[n];

[Link]("Enter the elements:");


for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}

// Ascending Order
[Link](arr);

[Link]("Ascending Order:");
for (int i = 0; i < n; i++) {
[Link](arr[i] + " ");
}

[Link]();

// Descending Order
[Link]("Descending Order:");
for (int i = n - 1; i >= 0; i--) {
[Link](arr[i] + " ");
}
}
catch (InputMismatchException e) {
[Link]("Exception: Please enter only integer values.");
}
}
}

Sample Output
Enter the number of elements: 5
Enter the elements:
20
10
50
30
40

Ascending Order:
10 20 30 40 50

Descending Order:
50 40 30 20 10

Exception Handling Used

 try block: Contains the code that may generate an error.

 catch block: Handles InputMismatchException when the user enters a non-integer


value instead of a number.

import [Link];

class StringOperations {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter first string: ");


String str1 = [Link]();

[Link]("Enter second string: ");


String str2 = [Link]();

[Link]("Length of first string: " + [Link]());

[Link]("Concatenation: " + [Link](str2));

[Link]("Uppercase: " + [Link]());

[Link]("Lowercase: " + [Link]());

[Link]("Character at index 2: " + [Link](2));


[Link]("Substring (0 to 3): " + [Link](0, 3));

if ([Link](str2))
[Link]("Strings are equal");
else
[Link]("Strings are not equal");

[Link]("Replaced String: " + [Link]('a', '@'));


}
}

Sample Output

Enter first string: Java


Enter second string: Programming

Length of first string: 4


Concatenation: JavaProgramming
Uppercase: JAVA
Lowercase: java
Character at index 2: v
Substring (0 to 3): Jav
Strings are not equal
Replaced String: J@v@

String Operations Used

1. length() – Finds the length of a string.

2. concat() – Joins two strings.

3. toUpperCase() – Converts to uppercase.

4. toLowerCase() – Converts to lowercase.

5. charAt() – Returns a character at a given position.

6. substring() – Extracts part of a string.

7. equals() – Compares two strings.

8. replace() – Replaces characters in a string.

import [Link];

class AreaFigures {
static void areaCircle(double r) {
double area = 3.14 * r * r;
[Link]("Area of Circle = " + area);
}

static void areaRectangle(double l, double b) {


double area = l * b;
[Link]("Area of Rectangle = " + area);
}

static void areaTriangle(double b, double h) {


double area = 0.5 * b * h;
[Link]("Area of Triangle = " + area);
}

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);

[Link]("1. Circle");
[Link]("2. Rectangle");
[Link]("3. Triangle");
[Link]("Enter your choice: ");

int choice = [Link]();

switch (choice) {
case 1:
[Link]("Enter radius: ");
double r = [Link]();
areaCircle(r);
break;

case 2:
[Link]("Enter length: ");
double l = [Link]();
[Link]("Enter breadth: ");
double b = [Link]();
areaRectangle(l, b);
break;
case 3:
[Link]("Enter base: ");
double base = [Link]();
[Link]("Enter height: ");
double h = [Link]();
areaTriangle(base, h);
break;

default:
[Link]("Invalid Choice");
}
}
}

Formulas Used

 Circle:

A=πr2A = \pi r^2A=πr2

rrr

A=πr2≈28.27A = \pi r^2 \approx 28.27A=πr2≈28.27

C=2πr≈18.85C = 2\pi r \approx 18.85C=2πr≈18.85

r = 3.00

 Rectangle: Area = Length × Breadth

 Triangle:

A=12bhA = \frac{1}{2}bhA=21bh

bbb

hhh

A=12bh≈24.00A = \frac{1}{2} b h \approx 24.00A=21bh≈24.00

b = 8.0h = 6.0

Sample Output

1. Circle
2. Rectangle
3. Triangle
Enter your choice: 1
Enter radius: 5
Area of Circle = 78.5
2. Write a program to implement constructor overloading by passing different number of
parameter of different types.
3. class Student {
String name;
int age;

// Default Constructor
Student() {
name = "Unknown";
age = 0;
[Link]("Default Constructor");
[Link]("Name: " + name + ", Age: " + age);
}

// Constructor with one parameter


Student(String n) {
name = n;
[Link]("Constructor with One Parameter");
[Link]("Name: " + name);
}

// Constructor with two parameters


Student(String n, int a) {
name = n;
age = a;
[Link]("Constructor with Two Parameters");
[Link]("Name: " + name + ", Age: " + age);
}

public static void main(String args[]) {


Student s1 = new Student();
Student s2 = new Student("Rahul");
Student s3 = new Student("Priya", 20);
}

You might also like