Jav Lab Manual
Jav Lab Manual
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 {
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");
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;
String s1 = "Hello";
String s2 = "World";
System.out.println("The Strings are "+s1+" and "+s2);
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;
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.*;
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);
19
201304
Output:
20
201304
10. Write a program to implement Thread Priority
package com.company;
threadA.setPriority(Thread.NORM_PRIORITY);
threadB.setPriority(Thread.MAX_PRIORITY);
threadC.setPriority(Thread.MIN_PRIORITY);
22
201304
Output:
23
201304