[go: up one dir, main page]

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

Jav Lab Manual

The document is an index and detailed description of various Java programming exercises, including finding factorials, checking prime numbers, sorting elements, creating patterns, and implementing string operations. It also covers concepts like abstract classes, method overloading, constructor overloading, and thread priorities. Each program includes code snippets and expected outputs, providing a comprehensive guide for learning Java programming.

Uploaded by

athulatk6
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 views23 pages

Jav Lab Manual

The document is an index and detailed description of various Java programming exercises, including finding factorials, checking prime numbers, sorting elements, creating patterns, and implementing string operations. It also covers concepts like abstract classes, method overloading, constructor overloading, and thread priorities. Each program includes code snippets and expected outputs, providing a comprehensive guide for learning Java programming.

Uploaded by

athulatk6
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/ 23

INDEX

SL.NO PROGRAMS PAGE. No

1 Write a program to find factorial of list of number reading 2-3


input as command line argument.
2 Write a program to check if a given number is prime number 4-5
or not
3. Write a program to sort list of elements in ascending and 6-7
descending order and show the exception handling.
4. Write a program to implement Rhombus pattern reading the 8-9
limit form user.
5. Write a program to implement all string operations. 10-11

6. Write a program in java to generate an abstract class A also 12


class B inherits the class A generate the object for class B and
display the text “call me from B”.

7. Write a program to find area of geometrical figures using 13-15


method.
8. Write a program to implement constructor overloading by 16-17
passing different number of parameter of different types.

9. Write a program to calculate bonus for different departments 18-20


using method overloading.
10 Write a program to implement thread priorities. 21-23
Lab Programs

1. Write a program to find factorial of list of number reading input as


command line argument.

import java.util.Scanner;
class Fact{
public static void main(String[] args){
int n,fac=1;
System.out.print("Enter the number");
Scanner r=new Scanner(System.in);
n=r.nextInt();

for(int i=1;i<=n;i++){
fac=fac*i;

}
System.out.println("factorial of the number is :"+fac);
}
}

2
201304
Output:

3
201304
2.Write a program to check if a given number is prime number or not.

package com.company;
public class CheckPrime {
static void Prime(int n) {
int i,flag = 0;
if(n==0 || n==1) {
System.out.println(n+ " is not a Prime number");
}else {
for(i=2;i<=n/2;i++) {
if(n%i ==0) {
System.out.println(n+ " is not a Prime Number");
flag = 1;
break;
}
}
if(flag == 0) {
System.out.println(n+ " is a Prime Number");
}
}
}
public static void main(String[] argus) {
Prime(1);
Prime(4);
Prime(17):
Prime(20):
}

4
201304
Output:

5
201304
3.Write a program to sort list of elements in Ascending order and descending
order and show the exception handling.

import java.util.Scanner;
public class SortDemo {
public static void main(String[] args)
int n;
Scanner sc = new Scanner(System.in);
try {

System.out.println("Enter the number of elements:");

n = sc.nextInt();
int[] arr = new int[10];
System.out.println("please enter" + n + "Number");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.println("After sorting");
System.out.println("Ascending order");

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


for (int j = i + 1; j < n; j++) {
int tmp = 0;
if (arr[i] > arr[j]) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}

6
201304
System.out.println("" + arr[i]);
}
System.out.println("\n\nDescending order \n");
for (int i = n - 1; i >= 0; i--) {
System.out.println("" + arr[i]);
}
}
catch (Exception exception) {
System.out.println("Error occured" + exception);
}
}
}

Output:

7
201304
4. Write a program to implement Rhombus pattern reading the limits from
user.

import java.io.*;
public class RhombusDemo
{
public static void main(String args[]) throws IOException
{
int i ,j, limit;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the limit:");
limit=Integer.parseInt(br.readLine());
for(i=1;i<=limit;i++)
{
for(j=limit-i; j>0; j--)
System.out.print(" ");
for (j=1;j<=2*i-1;j++)
System.out.print("*");
System.out.println();
}
for(i=limit-1;i>=1;i--)
{
for(j = 1; j<=limit-i; j++)
System.out.print(" ");
for(j = 1; j<=2*i-1; j++)
System.out.print("*");
System.out.println( );
}
}
}

8
201304
Output:

9
201304
5.Write a program to implement all string operations.

package com.company;

public class StringOperation {


public static void main(String[] argus) {

String s1 = "Hello";
String s2 = "World";
System.out.println("The Strings are "+s1+" and "+s2);

int len1 = s1.length();


int len2 = s2.length();
System.out.println("The length of "+s1+" is :"+len1);
System.out.println("The length of "+s2+" is :"+len2);

System.out.println("The Concatination of Two Strings is :"+s1.concat(s2));


System.out.println("The First Character of "+s1+" is:" +s1.charAt(0));
System.out.println("The Uppercase of s1 is :"+s1.toUpperCase());
System.out.println("The Lowercase of s1 is:" +s1.toLowerCase());
System.out.println("The letter E occurs at the position :"+s1.indexOf("e"));
System.out.println("Substring of "+s1+ " Starting From index 2 to 4 is
:"+s1.substring(2,4));
System.out.println("Replace letter 'e' with 'o' in s1:"+s1.replace('e','o'));

boolean check =s1.equals(s2);


if(check == false) {
System.out.println(s1+" and "+s2+" are not same");
}
else {
System.out.println(s1+" and " +s2+" are same");
}
}
}

10
201304
Output:

11
201304
6. Write a program in java to generate an abstract class A also class B
inherits the class A generate the object for class B and display the text
“call me from B”

package com.company;

import java.security.PublicKey;

abstract class A
{
abstract void call();
}
class B extends A
{
public void call()
{
System.out.println("Call me from B");
}
public static void main(String[] args)
{
B b=new B();
b.call();
}
}

Output:

12
201304
7. Write a program to find area of geometric figures using method
package com.company;
import java.io.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Area {


public static double circleArea(double r) {
return Math.PI * r * r;
}

public static double squareArea(double side) {


return side * side;
}

public static double rectArea(double width, double height) {


return width * height;
}

public static double triArea(double base, double height1) {


return 0.5 * base * height1;
}

public static String readLine() {


String input = "";
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try {
input = in.readLine();
} catch (Exception e) {
13
201304
System.out.println("Error" + e);
}
return input; }
public static void main(String args[]){
System.out.println("enter the radius");
Double radius= Double.parseDouble(readLine());
System.out.println("Area of Circle="+ circleArea(radius));
System.out.println("enter the side");
Double side= Double.parseDouble(readLine());
System.out.println("Area of Square="+ squareArea(side));
System.out.println("enter the width");
Double width= Double.parseDouble(readLine());
System.out.println("enter the height");
Double height= Double.parseDouble(readLine());
System.out.println("Area of Rectangle="+ rectArea(width,height));
System.out.println("enter the base");
Double base= Double.parseDouble(readLine());
System.out.println("enter the height");
Double height1= Double.parseDouble(readLine());
System.out.println("Area of Triangle="+ triArea(base,height1));

14
201304
Output:

15
201304
8. Write a program to implement constructor overloading by passing
different numbers by parameter of different type.

package com.company;

import javax.swing.*;

public class Box


{
int length, breadth, height;
Box()
{
length = breadth = height = 2;
System.out.println("Intialized with defalt constructor");
}
Box(int l, int b)
{
length=1; breadth=b; height=2;
System.out.println("Initialized with parameterized constructor having 2 params");
}
Box(int l,int b,int h)
{
length =l; breadth =b; height =h;
System.out.println("Intialized constructor having 3 params");
}
public int getVolume()
{
return length*breadth*height;
}
public static void main(String args[])

16
201304
{
Box box1 = new Box();
System.out.println("the Volume of Box1 is:"+ box1.getVolume());
Box box2 = new Box(10,20);
System.out.println("the Vol ume of Box2 is:"+ box2.getVolume());
Box box3 = new Box(10,20,30);
System.out.println("the Vol ume of Box3 is:"+ box3.getVolume());
}
}

Output:

17
201304
9.Write a program to calculate bonus for different department using method
overloading.

package com.company;
abstract class Department {
double salary,bonus,totalSalary;
public abstract void calBonus(double salary);
public void displaytotalSalry(String dept)
{

System.out.println(dept+"/t"+salary+"/t/t"+bonus+"/t"+totalSalary);
}

}
class Account extends Department
{
public void calBonus(double sal)
{
salary=sal;
bonus=sal*0.2;
totalSalary=salary+bonus;
}
}
class sales extends Department
{
public void calBonus(double sal)
{
salary=sal;bonus=sal*0.3;
totalSalary=salary+bonus;
}

18
201304
}
public class BonusCalculate
{
public static void main(String[] arr)
{
Department acc=new Account();
Department sales=new sales();
acc.calBonus(10000);
sales.calBonus(20000);

System.out.println("Department \t Basic salary \t Bonus \t total salary");


System.out.println();
acc.displaytotalSalry("Account dept ");
sales.displaytotalSalry("Sales dept ");
System.out.println();
}
}

19
201304
Output:

20
201304
10. Write a program to implement Thread Priority
package com.company;

class A extends Thread


{
public void run()
{
System.out.println("Thread A Started");for(int i=1;i<5;i++)
System.out.println("thread A:i="+i);
System.out.println("Exits from thread A");
}
}
class B extends Thread
{
public void run()
{
System.out.println("Thread B Started");for(int i=1;i<5;i++)
System.out.println("thread B:i="+i);
System.out.println("Exits from thread B");
}
}
class C extends Thread
{
public void run()
{
System.out.println("Thread C Started");for(int i=1;i<5;i++)
System.out.println("thread C:i="+i);
System.out.println("Exits from thread C");
}
}
21
201304
class threadPriority
{
public static void main(String[] arr)
{
A threadA = new A();
B threadB = new B();
C threadC = new C();

threadA.setPriority(Thread.NORM_PRIORITY);
threadB.setPriority(Thread.MAX_PRIORITY);
threadC.setPriority(Thread.MIN_PRIORITY);

System.out.println("Start thread A");threadA.start();


System.out.println("Start thread B");threadB.start();
System.out.println("Start thread C");threadC.start();
}
}

22
201304
Output:

23
201304

You might also like