Here are all Java Lab Practicals are written below. Click Here For Python Lab Page.
Lab-1
Write a program in Java to find the set of prime numbers from 1 to 100.
class PrimeNumbers
{
public static void main (String[] args)
{
int i =0;
int num =0;
//Empty String
String primeNumbers = "";
for (i = 1; i <= 100; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
//Appended the Prime number to the String
primeNumbers = primeNumbers + i + " ";
}
}
System.out.println("Prime numbers from 1 to 100 are :");
System.out.println(primeNumbers);
}
}
Lab-2
Write a program to compare two objects. Create two objects representing two complex numbers and find the larger one
package name.puzio.math;
public class Complex
{
private double real;
private double imaginary;
public Complex()
{
this( 0.0, 0.0 );
}
public final class ComplexNumber
{
private final double imaginary;
private final double real;
public final boolean equals(Object object)
{
if (!(object instanceof ComplexNumber))
return false;
ComplexNumber a = (ComplexNumber) object;
return (real == a.real) && (imaginary == a.imaginary);
}
public ComplexNumber(double real, double imaginary)
{
this.imaginary = imaginary;
this.real = real;
}
public static final ComplexNumber createPolar(double amount, double angel)
{
return new ComplexNumber(amount * Math.cos(angel), amount * Math.sin(angel));
}
public final double getImaginary()
{
return imaginary;
}
public final double getReal()
{
return real;
}
public final double getAmount()
{
return Math.sqrt((real * real) + (imaginary * imaginary));
}
public final double getAngle()
{
return Math.atan2(imaginary, real);
}
public final ComplexNumber add(ComplexNumber b)
{
return add(this, b);
}
public final ComplexNumber sub(ComplexNumber b)
{
return sub(this, b);
}
public final ComplexNumber div(ComplexNumber b)
{
return div(this, b);
}
public final ComplexNumber mul(ComplexNumber b)
{
return mul(this, b);
}
public final ComplexNumber conjugation()
{
return conjugation(this);
}
public Complex( double r, double i )
{
real = r;
imaginary = i;
}
public double getReal()
{
return this.real;
}
public double getImaginary()
{
return this.imaginary;
}
}
}
Lab-3
Write a Java Program to convert a Number to Word
class NumberToWordExample1
{
static void numberToWords(char num[])
{
int len = num.length;
if (len == 0)
{
System.out.println("The string is empty.");
return;
}
if (len > 4)
{
System.out.println("\n The given number has more than 4 digits.");
return;
}
String[] onedigit = new String[] {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
String[] twodigits = new String[] {"", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
String[] multipleoftens = new String[] {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
String[] poweroftens = new String[] {"Hundred", "Thousand"};
System.out.print(String.valueOf(num) + ": ");
if (len == 1)
{
System.out.println(onedigit[num[0]-'0']);
return;
}
int x = 0;
while (x < num.length)
{
if (len >= 3)
{
if (num[x] - '0' != 0)
{
System.out.print(onedigit[num[x] - '0'] + " ");
System.out.print(poweroftens[len - 3]+ " ");
}
--len;
}
else
{
if (num[x] - '0' == 1)
{
int sum = num[x] - '0' + num[x + 1] - '0';
System.out.println(twodigits[sum]);
return;
}
else if (num[x] - '0' == 2 && num[x + 1] - '0' == 0)
{
System.out.println("Twenty");
return;
}
else
{
int i = (num[x] - '0');
if (i > 0)
System.out.print(multipleoftens[i]+ " ");
else
System.out.print("");
++x;
if (num[x] - '0' != 0)
System.out.println(onedigit[num[x] - '0']);
}
}
++x;
}
}
public static void main(String args[])
{
numberToWords("1111".toCharArray());
numberToWords("673".toCharArray());
numberToWords("85".toCharArray());
numberToWords("5".toCharArray());
numberToWords("0".toCharArray());
numberToWords("20".toCharArray());
numberToWords("1000".toCharArray());
numberToWords("12345".toCharArray());
numberToWords("".toCharArray());
}
}
Lab-4
Write a Java Program to copy all elements of one array into another array
public class CopyArray {
public static void main(String[] args)
{
int [] arr1 = new int [] {1, 2, 3, 4, 5};
int arr2[] = new int[arr1.length];
for (int i = 0; i < arr1.length; i++)
{
arr2[i] = arr1[i];
}
System.out.println("Elements of original array: ");
for (int i = 0; i < arr1.length; i++)
{
System.out.print(arr1[i] + " ");
}
System.out.println();
System.out.println("Elements of new array: ");
for (int i = 0; i < arr2.length; i++)
{
System.out.print(arr2[i] + " ");
}
}
}
Lab-5
Write a Java Program to sort the elements of an array in ascending order
public class SortAsc {
public static void main(String[] args)
{
int [] arr = new int [] {5, 2, 8, 7, 1};
int temp = 0;
System.out.println("Elements of original array: ");
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
for (int i = 0; i < arr.length; i++)
{
for (int j = i+1; j < arr.length; j++)
{
if(arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println();
System.out.println("Elements of array sorted in ascending order: ");
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
}
}
Lab-6
Write a Java Program to find the frequency of odd & even numbers in the given matrix
public class OddEven
{
public static void main(String[] args)
{
int rows, cols, countOdd = 0, countEven = 0;
int a[][] = {
{4, 1, 3},
{3, 5, 7},
{8, 2, 6}
};
rows = a.length;
cols = a[0].length;
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
if(a[i][j] % 2 == 0)
countEven++;
else
countOdd++;
}
}
System.out.println("Frequency of odd numbers: " + countOdd);
System.out.println("Frequency of even numbers: " + countEven);
}
}
Lab-7
Write a Java Program to determine whether a given string is a palindrome
public class PalindromeString
{
public static void main(String[] args)
{
String string = "Kayak";
boolean flag = true;
string = string.toLowerCase();
for(int i = 0; i < string.length()/2; i++)
{
if(string.charAt(i) != string.charAt(string.length()-i-1))
{
flag = false;
break;
}
}
if(flag)
System.out.println("Given string is palindrome");
else
System.out.println("Given string is not a palindrome");
}
}
Lab-8
Write a Java program to draw a pattern such as
1 000*000*
2 4 0*00*00*0
3 6 9 00*0*0*00
4 8 12 16 000***000
public class pattern
{
public static void main(String[] args)
{
int lines=10;
int i=1;
int j;
for(i=1;i<=lines;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(i*j+" ");
}
System.out.println("");
}
}
}
public class pattern
{
public static void main(String[] args)
{
int lines=4;
int i,j;
for(i=1;i<=lines;i++)
{
for(j=1;j<=lines;j++)
{
if(i==j)
System.out.print("*");
else
System.out.print("0");
}
j--;
System.out.print("*");
while(j>=1)
{
if(i==j)
System.out.print("*");
else
System.out.print("0");
j--;
}
System.out.println("");
}
}
}
Lab-9
Write a Java program to Convert Decimal to Binary in Java
public class DecimalToBinaryExample2
{
public static void toBinary(int decimal)
{
int binary[] = new int[40];
int index = 0;
while(decimal > 0)
{
binary[index++] = decimal%2;
decimal = decimal/2;
}
for(int i = index-1;i >= 0;i--)
{
System.out.print(binary[i]);
}
System.out.println();
}
public static void main(String args[])
{
System.out.println("Decimal of 10 is: ");
toBinary(10);
System.out.println("Decimal of 21 is: ");
toBinary(21);
System.out.println("Decimal of 31 is: ");
toBinary(31);
}
}
}
Lab-10
Write a program to add two times given in hour minutes and seconds using class and object.
import java.io.*;
class time
{
int hour,min,sec;
public time(int h,int m,int s)
{
hour = h;
min = m;
sec = s;
}
public time()
{
hour = 0;
min = 0;
sec = 0;
}
public time sum(time t2)
{
time t3 = new time();
t3.sec = sec + t2.sec;
t3.min = min + t2.min;
t3.hour = hour + t2.hour;
if(t3.sec >= 60)
{
t3.min = t3.sec / 60;
t3.sec = t3.sec % 60;
}
if(t3.min >= 60)
{
t3.hour = t3.min / 60;
t3.min = t3.min % 60;
}
return(t3);
}
public void display()
{
System.out.println(hour+ ":"+min+":"+sec);
}
}
class timedemo
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
time t1 = new time(1,56,55);
time t2 = new time(2,00,10);
time t3 = new time();
t3 = t1.sum(t2);
t1.display();
t2.display();
t3.display();
}
}
Lab-11
Write a Java program to find the combination c(n,r) by inheriting from a class that computes the factorial of a number.
import java.util.*;
class mr5
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number N");
int n=sc.nextInt();
System.out.println("Enter a number R");
int r=sc.nextInt();
int ncr=fact(n)/(fact(r)*fact(n-r));
System.out.println("Value of "+n+"C"+r+" = "+ncr);
}
static int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
return f;
}
}
Lab-12
Write a Java program to find the area of different geometrical shapes using polymorphism.
class JavaExample
{
void calculateArea(float x)
{
System.out.println("Area of the square: "+x*x+" sq units");
}
void calculateArea(float x, float y)
{
System.out.println("Area of the rectangle: "+x*y+" sq units");
}
void calculateArea(double r)
{
double area = 3.14*r*r;
System.out.println("Area of the circle: "+area+" sq units");
}
public static void main(String args[])
{
JavaExample obj = new JavaExample();
obj.calculateArea(6.1f);
obj.calculateArea(10,22);
obj.calculateArea(6.1);
}
}
Lab-13
Write a Java program to create a user-defined package that finds the largest among an array of n numbers. Use this package to sort an array of n numbers using insertion/selection sort.
package rit;
public class InsertionSortExample
{
public static void insertionSort(int array[])
{
int n = array.length;
for (int j = 1; j < n; j++)
{
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) )
{
array [i+1] = array [i];
i--;
}
array[i+1] = key;
}
}
import rit.*;
public static void main(String a[])
{
int[] arr1 = {9,14,3,2,43,11,58,22};
System.out.println("Before Insertion Sort");
for(int i:arr1)
{
System.out.print(i+" ");
}
System.out.println();
insertionSort(arr1);
System.out.println("After Insertion Sort");
for(int i:arr1)
{
System.out.print(i+" ");
}
}
}
Lab-14
Create three threads and print 1 to 10 in each thread.
public class PrintSequenceRunnable implements Runnable
{
public int PRINT_NUMBERS_UPTO=10;
static int number=1;
int remainder;
static Object lock=new Object();
PrintSequenceRunnable(int remainder)
{
this.remainder=remainder;
}
public void run()
{
while (number < PRINT_NUMBERS_UPTO-1)
{
synchronized (lock)
{
while (number % 3 != remainder)
{
try
{
lock.wait();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " " + number);
number++;
lock.notifyAll();
}
}
}
}
public class PrintThreadsSequentiallyMain
{
public static void main(String[] args)
{
PrintSequenceRunnable runnable1=new PrintSequenceRunnable(1);
PrintSequenceRunnable runnable2=new PrintSequenceRunnable(2);
PrintSequenceRunnable runnable3=new PrintSequenceRunnable(0);
Thread t1=new Thread(runnable1,"T1");
Thread t2=new Thread(runnable2,"T2");
Thread t3=new Thread(runnable3,"T3");
t1.start();
t2.start();
t3.start();
}
}
Lab-15
Write a Java program to illustrate the concept of some exceptions such as divide by zero or array index out of bound etc.
class Example1
{
public static void main(String args[])
{
try
{
int num1=30, num2=0;
int output=num1/num2;
System.out.println ("Result: "+output);
}
catch(ArithmeticException e)
{
System.out.println ("You Shouldn't divide a number by zero");
}
}
}
class ExceptionDemo2
{
public static void main(String args[])
{
try
{
int a[]=new int[10];
a[11] = 9;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println ("ArrayIndexOutOfBounds");
}
}
}
Have You Finished JAVA Record. ЁЯШКЁЯШКЁЯСН
Very Good, Picture abhi baki he mere dost
No comments:
Post a Comment