Faculty of Engineering and Technology: Object Oriented Programming /java Lab (CS-453)
Faculty of Engineering and Technology: Object Oriented Programming /java Lab (CS-453)
TECHNOLOGY
Object Oriented
PROGRAMMING /JAVA Lab
(CS-453)
SESSION:-2021-22
1
INDEX
Sr PROGRAM Pg no. Signature
no.
1 WAJP to compute Square Root of a given 3-4
number.
2 WAJP to compute the Area of a Room using 5
multiple classes.
3 WAJP that depicts the use of Nested if-else 6-7
statement.
4 WAJP that depicts the use of while loop. 8
2
LAB-1
Write a Java Program to compute Square Root of a given number.
import java.util.Scanner;
public class FindSquareRootExample1
{
public static void main(String[] args)
{
System.out.print("Enter a number: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println("The square root of "+ n+ " is: "+squareRoot(n));
}
public static double squareRoot(int num)
{
double t;
double sqrtroot=num/2;
do
{
t=sqrtroot;
sqrtroot=(t+(num/t))/2;
}
while((t-sqrtroot)!= 0);
return sqrtroot;
}
}
3
OUTPUT:
4
LAB-2
Write a program to compute the Area of a Room using multiple classes.
class Room
{
float length;
float breadth;
OUTPUT:
5
LAB-3
Write a program that depicts the use of Nested if-else statements.
import java.util.Scanner;
class Nested
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the age: ");
int age=sc.nextInt();
System.out.println("Enter the weight: ");
int weight=sc.nextInt();
if(age>=18)
{
if(weight>50)
{
System.out.println("The person is eligible to donate blood");
}
else
{
System.out.println("The person is not eligible to donate blood");
}
}
else
{
System.out.println("Age must be greater than 18");
}
}
}
6
OUTPUT:
7
LAB-4
Write a program that depicts the use of while loop.
public class Whileloopdemo {
public static void main(String args[])
{
int i = 1;
while (i < 6) {
System.out.println("Hello World");
i++;
}
}
OUTPUT:
8
LAB-5
Write a program for sorting a list of numbers.
public class NumberSorting {
public static void main(String args[])
{
int number [] = { 55, 40, 80, 65, 71 };
int n = number.length;
System.out.print("Given list : ");
for (int i = 0; i < n; i++)
{
System.out.print(" " + number[i]);
}
System.out.println("\n");
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
if (number[i] < number[j])
{
int temp = number[i];
number[i] = number[j];
number[j] = temp;
}
}
}
System.out.println("Sorted list : ");
for (int i = 0; i < n; i++)
{
System.out.println(" " + number[i]);
9
}
System.out.println(" ");
}
}
OUTPUT:
10
LAB-6
Write a program that depicting the floating point operation in java.
public class Floatpoint {
public static void main (String args[]){
float a= 21.5F, b=4.7F;
System.out.println(" a = " +a);
System.out.println(" b=" +b);
System.out.println(" a+b= " +(a+b));
System.out.println(" a-b=" +(a-b));
System.out.println(" a*b=" +(a*b));
System.out.println(" a/b=" +(a/b));
System.out.println(" a%b=" +(a%b));
}
}
OUTPUT:
11
LAB-7
Write a program printing Multiplication Table using do…while loop.
import java.util.Scanner;
class Multiplication_Table4{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int num;
System.out.println("Enter the number: ");
num=sc.nextInt();
int i=1;
do{
System.out.println(num+"x"+i+"="+num*i);
i++;
}while( i<=10);
}
}
OUTPUT:
12
LAB-8
Write a program stating the application of 2-D arrays.
class Multable {
final static int ROWS = 20;
final static int COLOUMNS = 20;
public static void main (String args[]){
int product[][]= new int[ROWS][COLOUMNS];
int ROW, COLUMN;
System.out.println("MULTIPLICATION TABLE");
System.out.println(" ");
int i,j;
for(i=10; i<ROWS; i++)
{
for(j=10; j<COLOUMNS; j++)
{
product[i][j]=i*j;
System.out.print(" " +product[i][j]);
}
System.out.println(" ");
}
}
}
13
OUTPUT:
14
LAB-9
Write a program for implementation of Relational Operations.
public class RelationalOperators {
public static void main (String args[]){
float a= 40.73F, b=48.7F, c = 40.73F;
System.out.println(" a = " +a);
System.out.println(" b=" +b);
System.out.println(" c=" +c);
System.out.println(" a<b= is " +(a<b));
System.out.println(" a>b= is" +(a>b));
System.out.println(" a==b = is" +(a==b));
System.out.println(" a<=b = is" +(a<=b));
System.out.println(" a>=b = is" +(a>=b));
System.out.println(" b!=c is" +(b!=c));
System.out.println(" b== a+c is" +(b==a+c));
}
}
OUTPUT:
15
LAB-10
Write a program for illustration of the use of Casting Operations.
public class Casting {
public static void main(String args[]) {
int i;
float sum;
sum = 0.0F;
for (i = 1; i <= 10; i++) {
sum = sum + 1 / (float) i;
System.out.print("i = " + i);
System.out.println(" Sum = " + sum);
}
}
}
OUTPUT:
16