Java LAB
Java LAB
Page
[Link] Date Name of the Experiment Status Remarks
No
Exp. Name: Write a Java Program to find the reverse of a given number. And also
[Link]: 1 Date:
check whether it is palindrome or not.
Page No:
Aim:
Write a Java Program to find the reverse of a given number. And also check whether it is palindrome or not.
Source Code:
ID: CSE016
[Link]
import [Link];
public class Palindrome
{
public static void main(String args[])
{
int n, m, a = 0,x;
Scanner s = new Scanner([Link]);
[Link]("Enter any number:");
n = [Link]();
m = n;
while(n > 0)
{
x = n % 10;
a = a * 10 + x;
n = n / 10;
}
[Link]("Reverse of a number is "+a);
if(a == m)
{
[Link]("Given number "+m+" is Palindrome");
}
else
{
[Link]("Given number "+m+" is Not Palindrome");
Test Case - 1
User Output
Enter any number: 145
Reverse of a number is 541
Given number 145 is Not Palindrome
Test Case - 2
User Output
Enter any number: 111
Reverse of a number is 111
Given number 111 is Palindrome
[Link] 1/1
6/16/2021 [Link]
[Link]: 2 Exp. Name: Java Program to Display the Fibonacci Series Date:
Aim:
Page No:
Write a class FibonacciSeries with a main method. The method receives one command line argument.
Write a program to display fibonacci series i.e. 0 1 1 2 3 5 8 13 21.....
For example:
ID: CSE016
Cmd Args : 80
0 1 1 2 3 5 8 13 21 34 55
q10896/[Link]
package q10896;
class FibonacciSeries {
public static void main(String[] args) {
int n=[Link](args[0]);
int a=0,b=1;
[Link](a+ " "+b);
int c=a+b;
do
{
[Link](" "+c);
a=b;
b=c;
c=a+b;
}while(c<n);
Test Case - 1
User Output
0 1 1 2 3 5
Test Case - 2
User Output
0 1 1 2 3 5 8 13 21 34 55
Test Case - 3
User Output
0 1 1 2 3 5 8 13 21 34 55
[Link] 1/1
6/16/2021 [Link]
Exp. Name: Write a Java program that prints out all prime numbers up to given
[Link]: 3 Date:
integer.
Page No:
Aim:
Write a Java program that prompts the user for an integer and then prints out all prime numbers up to that
integer. (Use Scanner/ BufferedReader class)
Source Code:
ID: CSE016
[Link]
import [Link].*;
class PrintPrime{
public static void main(String a[]){
Scanner s=new Scanner([Link]);
[Link]("Enter a number: ");
int n=[Link]();
[Link]("Prime numbers are: ");
for (int i = 1; i <= n; i++) {
int counter=0;
for(int num =i; num>=1; num--) {
if(i%num==0) {
counter = counter + 1;
}
}
if (counter ==2)
{
//Appended the Prime number to the String
[Link](i+" ");
}
}
}
}
Test Case - 1
User Output
Enter a number: 50
Prime numbers are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Test Case - 2
User Output
Enter a number: 25
Prime numbers are: 2 3 5 7 11 13 17 19 23
Test Case - 3
User Output
Enter a number: 75
Prime numbers are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73
[Link] 1/1
6/16/2021 [Link]
Aim:
Page No:
Write a class MultiplicationOfMatrix with a public method multiplication which returns the
multiplication result of its arguments. if the first argument column size is not equal to the row size of the second
argument, then the method should return null.
ID: CSE016
Consider the following example for your understanding
Matrix 1:
Enter number of rows: 3
Enter number of columns: 2
Enter 2 numbers separated by space
Enter row 1: 1 2
Enter row 2: 4 5
Enter row 3: 7 8
Matrix 2:
Enter number of rows: 2
Enter number of columns: 3
Enter 3 numbers separated by space
Enter row 1: 1 2 3
Enter row 2: 4 5 6
Multiplication of the two given matrices is:
9 12 15
24 33 42
39 54 69
Matrix 1:
Enter number of rows: 2
Enter number of columns: 2
Enter 2 numbers separated by space
Enter row 1: 1 2
Enter row 2: 3 4
q11106/[Link]
package q11106;
public class MultiplicationOfMatrix{
public int[][] multiplication(int[][] matrix1, int[][] matrix2) {
/*Return the result if the matrix1 coloumn size is equal to matrix2 row size and
print the result.
* @Return null.
*/
[Link] 1/4
6/16/2021 [Link]
// Write your logic here for matrix multiplication
int r1=[Link];
int r2=[Link];
int c1=matrix1[0].length;
int c2=matrix2[0].length;
Page No:
if(c1==r2)
{
int c[][]=new int[r1][c2];
ID: CSE016
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c2; j++)
{
c[i][j] = 0;
for (int k = 0; k < c1; k++)
c[i][j] += matrix1[i][k]* matrix2[k][j];
}
}
return c;
}
else return null;
}
}
q11106/[Link]
package q11106;
import [Link];
public class MultiplicationOfMatrixMain {
public static void main(String[] args) {
Scanner s = new Scanner([Link]);
MultiplicationOfMatrix multiplier = new MultiplicationOfMatrix();
[Link]("Matrix 2:");
int[][] m2 = readMatrix(s);
if (multi == null) {
[Link]("Multiplication of matrices is not possible");
} else {
[Link]("Multiplication of the two given matrices is:");
for (int i = 0; i < [Link]; i++) {
int c = multi[i].length;
for (int j = 0; j < c; j++) {
String spacer = j == c - 1 ? "\n" : " ";
[Link](multi[i][j] + spacer);
}
}
}
}
[Link] 2/4
6/16/2021 [Link]
[Link]("Enter number of rows: ");
int r = [Link]();
[Link]("Enter number of columns: ");
int c = [Link]();
int[][] m = new int[r][c];
Page No:
[Link]("Enter " + c + " numbers separated by space");
for (int i = 0; i < r; i++) {
[Link]("Enter row " + (i + 1) + ": ");
ID: CSE016
for (int j = 0; j < c; j++) {
m[i][j] = [Link]();
}
}
return m;
}
}
Test Case - 1
User Output
Matrix 1: 2
Enter number of rows: 2
Enter number of columns: 3
Enter 3 numbers separated by space 1 2 3
Enter row 1: 1 2 3
Enter row 2: 4 5 6
Matrix 2: 3
Enter number of rows: 3
Enter number of columns: 2
Enter 2 numbers separated by space 1 2
Enter row 1: 1 2
Test Case - 2
User Output
Matrix 1: 2
Enter number of rows: 2
Enter number of columns: 2
Enter 2 numbers separated by space 1 2
Enter row 1: 1 2
Enter row 2: 3 4
Matrix 2: 2
Enter number of rows: 2
Enter number of columns: 2
Enter 2 numbers separated by space 5 6
Enter row 1: 5 6
Enter row 2: 7 8
[Link] 3/4
6/16/2021 [Link]
Test Case - 2
Multiplication of the two given matrices is:
19 22
43 50
Page No:
ID: CSE016
Rajeev Gandhi Memorial College of Engineering and Technology (Autonomous)
[Link] 4/4
6/16/2021 [Link]
[Link]: 5 Exp. Name: Find minimum and maximum numbers from given array. Date:
Aim:
Page No:
Write a Java program to find minimum and maximum numbers in a given array.
Source Code:
[Link]
ID: CSE016
import [Link];
class MinMaxArray{
public static void main(String args[]){
Scanner s=new Scanner([Link]);
[Link]("Enter number of elements: ");
int n=[Link]();
int arr[]=new int[n];
[Link]("Enter array elements: ");
for(int i=0; i<n; i++){
arr[i]=[Link]();
}
int min=arr[0];
int max=arr[0];
for(int i=0; i<n; i++){
if(arr[i]<min)
min=arr[i];
if(arr[i]>max)
max=arr[i];
}
[Link]("Mimimum element in array is: "+min);
[Link]("Maximum element in array is: "+max);
}
}
Test Case - 1
User Output
Enter number of elements: 5
Enter array elements: 100 1 135 0 12
Mimimum element in array is: 0
Maximum element in array is: 135
Test Case - 2
User Output
Enter number of elements: 10
Enter array elements: 2 2222 222 22222 22 1 1111 111111 111 11111
Mimimum element in array is: 1
Maximum element in array is: 111111
[Link] 1/2
6/16/2021 [Link]
Test Case - 3
User Output
Enter number of elements: 8
Page No:
Enter array elements: 88 77 66 55 44 33 22 11
Mimimum element in array is: 11
Maximum element in array is: 88
ID: CSE016
Rajeev Gandhi Memorial College of Engineering and Technology (Autonomous)
[Link] 2/2
6/16/2021 [Link]
[Link]: 6 Exp. Name: Write a Java program to find the volume of a box by creating objects. Date:
Aim:
Page No:
Write a Java program to find the volume of a box by creating objects.
Source Code:
[Link]
ID: CSE016
import [Link].*;
class BoxDemo
{
double l,b,h;
BoxDemo(double l,double b,double h)
{
this.l=l;
this.b=b;
this.h=h;
}
public double vol()
{
return (l*b*h);
}
public static void main(String args[])
{
double l1,b1,h1;
Scanner sc=new Scanner([Link]);
[Link]("Enter the length of box:");
l1=[Link]();
[Link]("Enter the breadth of box:");
b1=[Link]();
[Link]("Enter height of box:");
h1= [Link]();
Test Case - 1
User Output
Enter the length of box: 3
Page No:
Enter the breadth of box: 6
Enter height of box: 9
Volume is 162.0
ID: CSE016
Rajeev Gandhi Memorial College of Engineering and Technology (Autonomous)
[Link] 2/2
6/16/2021 [Link]
Exp. Name: Create static methods to perform arithmetic operations and access them
[Link]: 7 Date:
in other classes.
Page No:
Aim:
Write a java program to create static methods to perform arithmetic operations. Access them in other classes
and display results.
Source Code:
ID: CSE016
[Link]
import [Link].*;
class ArithmeticOperations{
static int add(int a,int b){
return a+b;
}
static int sub(int a,int b){
return a-b;
}
static int mul(int a,int b){
return a*b;
}
static int div(int a,int b){
if (b==0)
return 0;
else
return a/b;
}
static int mod(int a,int b){
if (b==0)
return 0;
else
return a%b;
}
}
}
[Link] 1/2
6/16/2021 [Link]
Page No:
Test Case - 1
User Output
Enter a number: 5
ID: CSE016
Enter another number: 10
Addition of 5 and 10 is: 15
Subtraction of 5 and 10 is: -5
Multiplication of 5 and 10 is: 50
Division of 5 and 10 is: 0
Modulo of 5 and 10 is: 5
Test Case - 2
User Output
Enter a number: 50
Enter another number: 90
Addition of 50 and 90 is: 140
Subtraction of 50 and 90 is: -40
Multiplication of 50 and 90 is: 4500
Division of 50 and 90 is: 0
Modulo of 50 and 90 is: 50
[Link] 2/2
6/16/2021 [Link]
[Link]: 8 Exp. Name: Write a Java program to exchange two variables using call by value Date:
Aim:
Page No:
Write a Java program to exchange two variables using call by value
Source Code:
[Link]
ID: CSE016
import [Link].*;
class SwapDemo
{
public void swap(int i,int j)
{
int temp =i;
j=i;
temp=i;
}
Test Case - 1
User Output
Enter value 1: 10
Enter value 2: 20
Before swapping value of a is 10 value of b is 20
After swapping value of a is 10 value of b is 20
[Link] 1/1
6/16/2021 [Link]
[Link]: 9 Exp. Name: Write a Java program to exchange two variables using call by reference Date:
Aim:
Page No:
Write a Java program to exchange two variables using call by reference
Source Code:
[Link]
ID: CSE016
import [Link];
class SwapDemo1
{
int x,y;
SwapDemo1(int i, int j){
this.x=i;
this.y=j;
}
void swap(SwapDemo1 o){
int temp=o.x;
this.x=o.y;
this.y=temp;
}
public static void main(String a[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter value 1:");
int i1=[Link]();
[Link]("Enter value 2:");
int i2=[Link]();
SwapDemo1 s=new SwapDemo1(i1,i2);
[Link]("Before swapping value of a is "+s.x+" value of b is "+s.y);
[Link](s);
[Link]("After swapping value of a is "+s.x+" value of b is "+s.y);
}
Test Case - 1
User Output
Enter value 1: 10
Enter value 2: 20
Before swapping value of a is 10 value of b is 20
After swapping value of a is 20 value of b is 10
[Link] 1/1
6/16/2021 [Link]
[Link]: 10 Exp. Name: Write a Java program to implement Method overloading Date:
Aim:
Page No:
Write a Java program with a class name Addition with the methods add(int, int) , add(int, float) ,
add(float, float) and add(float, double, double) to add values of different argument types.
Write the main(String[]) method within the class and assume that it will always receive a total of 6 command
ID: CSE016
line arguments at least, such that the first 2 are int, next 2 are float and the last 2 are of type double.
If the main() is provided with arguments : 1, 2, 1.5f, 2.5f, 1.0, 2.0 then the program should print the output as:
Sum of 1 and 2 : 3
Sum of 1.5 and 2.5 : 4.0
Sum of 2 and 2.5 : 4.5
Sum of 1.5, 1.0 and 2.0 : 4.5
q11266/[Link]
package q11266;
class Addition{
void add(int x, int y){
[Link]("Sum of "+x+" and "+y+" : "+(x+y));
}
void add(int x, float y){
[Link]("Sum of "+x+" and "+y+" : "+(x+y));
}
void add(float x, float y){
[Link] 1/2
6/16/2021 [Link]
Test Case - 1
User Output
Sum of 2 and 1 : 3
Page No:
Sum of 5.0 and 3.6 : 8.6
Sum of 1 and 3.6 : 4.6
Sum of 5.0, 9.2 and 5.26 : 19.46
ID: CSE016
Rajeev Gandhi Memorial College of Engineering and Technology (Autonomous)
[Link] 2/2
6/16/2021 [Link]
[Link]: 11 Exp. Name: Write a Java program to implement Constructor overloading Date:
Aim:
Page No:
Write a class Box which contains the data members width, height and depth all of type double.
Write the implementation for the below 3 overloaded constructors in the class Box :
Box() - default constructor which initializes all the members with -1
ID: CSE016
Box(length) - parameterized constructor with one argument and initialize all the members with the value
in length
the members with the corresponding arguments
Box(width, height, depth) - parameterized constructor with three arguments and initialize
Write a method public double volume() in the class Box to find out the volume of the given box.
Write the main method within the Box class and assume that it will receive either zero arguments, or one
argument or three arguments.
For example, if the main() method is passed zero arguments then the program should print the output as:
Similarly, if the main() method is passed one argument : 2.34, then the program should print the output as:
then the program should print the output as: Likewise, if the main() method is passed three arguments : 2.34,
3.45, 1.59, then the program should print the output as:
package q11267;
public class Box{
private double width,height,depth;
public Box(){
width=height=depth=-1;
}
public Box(double length){
width=height=depth=length;
}
public Box(double width,double height, double depth){
[Link]= width;
[Link]=height;
[Link]=depth;
}
public double volume(){
return width*height*depth;
}
public static void main(String args[])throws Exception{
int n=[Link];
[Link] 1/2
6/16/2021 [Link]
if(n==0)
{
Page No:
if(n==1)
{
double l=[Link](args[0]);
ID: CSE016
[Link]("Volume of Box("+l+") is : "+new Box(l).volume());
}
if(n==3)
{
double l=[Link](args[0]);
double b=[Link](args[1]);
double h=[Link](args[2]);
[Link]("Volume of Box("+l+", "+b+", "+h+") is : "+new Box(l,b,h).volu
me());
}
}
}
Test Case - 1
User Output
Volume of Box() is : -1.0
Test Case - 2
User Output
Volume of Box(3.0) is : 27.0
[Link] 2/2
6/16/2021 [Link]
[Link]: 12 Exp. Name: Write a Java program to implement Multilevel Inheritance Date:
Aim:
Page No:
Write a Java program to illustrate the multilevel inheritance concept.
ID: CSE016
write a method setData() to initialize the data members
write a method displayData() which will display the given id and name
Create another class Result which is derived from the class Marks
contains the data members total and avg of float data type
write a method compute() to find total and average of the given marks
write a method showResult() which will display the total and avg marks
Write a class MultilevelInheritanceDemo with the main() method which will receive five arguments as id,
name, javaMarks, cMarks and cppMarks.
If the input is given as command line arguments to the main() as "99", "Lakshmi", "55.5", "78.5", "72" then
the program should print the output as:
Id : 99
Name : Lakshmi
Java marks : 55.5
C marks : 78.5
Cpp marks : 72.0
Total : 206.0
Avg : 68.666664
q11264/[Link]
package q11264;
class Student{
int id;
String name;
void setData(int i,String n){
id=i;name=n;
}
void displayData(){
[Link]("Id : "+id);
[Link]("Name : "+name);
}
}
class Marks extends Student{
//int id;
float javaMarks,cMarks,cppMarks;
[Link] 1/3
6/16/2021 [Link]
void setMarks(float javaMarks, float cMarks, float cppMarks){
//[Link]=id;
[Link]=javaMarks;
[Link]=cMarks;
[Link]=cppMarks;
Page No:
}
void displayMarks(){
//[Link]("Id : "+id);
ID: CSE016
displayData();
[Link]("Java marks : "+javaMarks);
[Link]("C marks : "+cMarks);
[Link]("Cpp marks : "+cppMarks);
}
}
class Result extends Marks{
float total,avg;
void compute()
{
total=javaMarks+cMarks+cppMarks;
avg=total/3;
}
void showResult()
{
displayMarks();
[Link]("Total : "+total);
[Link]("Avg : "+avg);
}
}
class MultilevelInheritanceDemo{
public static void main(String a[]){
Result r=new Result();
int i=[Link](a[0]);
String s=a[1];
float s1=[Link](a[2]);
Test Case - 1
User Output
Id : 99
Name : Geetha
Java marks : 56.0
C marks : 75.5
Cpp marks : 66.6
Total : 198.1
[Link] 2/3
6/16/2021 [Link]
Test Case - 1
Avg : 66.03333
Page No:
Test Case - 2
User Output
Id : 199
ID: CSE016
Name : Lakshmi
Java marks : 55.5
C marks : 78.5
Cpp marks : 78.0
Total : 212.0
Avg : 70.666664
[Link] 3/3
6/16/2021 [Link]
[Link]: 13 Exp. Name: Write a Java program to achieve concept of Method Overriding Date:
Aim:
Page No:
Assume there is a class called Bank with method calculateInterest(float principal, int time) .
Create sub-classes of Bank with names SBI , ICICI and AXIS and override the
calculateInterest(float principal, int time) method.
ID: CSE016
Create a constant of type float called INTEREST_RATE in classes SBI , ICICI and AXIS with values
10.8 , 11.6 and 12.3 respectively.
Use the formula (principal * INTEREST_RATE * time) / 100 to calculate the interest for given principal and
time and return the value as float in the overriden method.
For example, if the two arguments passed to the main method are 1000 and 5, (principal and time) below is
the expected output:
q11271/[Link]
package q11271;
class Bank {
float calculateInterest(float principal, int time) {
return 0;
}
[Link] 1/2
6/16/2021 [Link]
Bank sbiBank = new SBI();
Bank iciciBank = new ICICI();
Bank axisBank = new AXIS();
float principal = [Link](args[0]);
int time = [Link](args[1]);
Page No:
[Link]("SBI rate of interest = " +[Link](principa
l,time) );
[Link]("ICICI rate of interest = " +[Link](princ
ID: CSE016
ipal,time) );
[Link]("AXIS rate of interest = " +[Link](princip
al,time) );
}
}
Test Case - 1
User Output
SBI rate of interest = 1804.9608
ICICI rate of interest = 1938.6616
AXIS rate of interest = 2055.65
Test Case - 2
User Output
SBI rate of interest = 540.0
ICICI rate of interest = 580.0
AXIS rate of interest = 615.0
Test Case - 3
Test Case - 4
User Output
SBI rate of interest = 648.0
ICICI rate of interest = 696.0
AXIS rate of interest = 738.0
Test Case - 5
User Output
SBI rate of interest = 75600.0
ICICI rate of interest = 81200.0
AXIS rate of interest = 86100.0
[Link] 2/2
6/16/2021 [Link]
Exp. Name: Write a Java program to import user defined packages to display results
[Link]: 14 for any mathematical operations like addition, subtractions, multiplications and Date:
division (class methods) from one package
Page No:
Aim:
Write a Java program to import user defined packages to display results for any mathematical operations like
addition, subtractions, multiplications and division (class methods) from one package and also producing
ID: CSE016
results square, cube and square-root of a given number (instant methods) from another package.
Source Code:
p1/[Link]
package p1;
public class Arithmatic
{
public static int add(int x,int y)
{
return x+y;
}
public static int sub(int x,int y)
{
return x-y;
}
public static int multiplication(int x,int y)
{
return x*y;
}
public static int div(int x,int y)
{
return y!=0?x/y:1;
}
}
package p2;
public class Calculations
{
public double square(int x)
{
return x*x;
}
public double cube(int x)
{
return x*x*x;
}
public double squareroot(int x)
{
return [Link](x);
}
[Link] 1/5
6/16/2021 [Link]
[Link]
Page No:
import [Link];
import [Link];
import [Link].*;
class Operations
ID: CSE016
{
public static void main(String args[])
{
Scanner s = new Scanner([Link]);
/*[Link]("1. addition :");
[Link]("2. subtraction:");
[Link]("3. multiplication:");
[Link]("4. division:");
[Link]("5. square:");
[Link]("6. cube:");
[Link]("7. square-root:");*/
[Link](" 1. addition \n 2. subtraction \n 3. multiplication \n 4. divisio
n"+"\n 5. square \n 6. cube \n 7. square-root \n Enter your Choice:");
int ch = [Link]();
int a=0,b=0;
if(ch>0 && ch<=4)
{
[Link]("Enter Two Integer Numbers: ");
a = [Link]();
b = [Link]();
}
else if (ch>4 && ch<=7)
{
[Link]("Enter an Integer Number: ");
a = [Link]();
}
switch(ch)
{
case 1:
[Link]("Addition of " + a + " and "+ b + " is : "+ [Link](a,
b));
break;
case 2:
[Link]("Subtraction of " + a + " and "+ b + " is : "+ [Link]
(a,b));
break;
case 3:
[Link]("Multiplication of " + a + " and "+ b + " is : "+ Arithmatic.m
ultiplication(a,b));
break;
case 4:
[Link]("Division of " + a + " and "+ b + " is : "+ [Link](a,
[Link] 2/5
6/16/2021 [Link]
b));
break;
case 5:
Page No:
[Link]("Square of " + a + " is : "+ new Calculations().square(a));
break;
ID: CSE016
case 6:
[Link]("Cube of " + a + " is : "+ new Calculations().cube(a));
break;
case 7:
[Link]("Square-root of " + a + " is : "+ new Calculations().squareroo
t(a));
break;
}
}
}
/* [Link](" [Link] :");
[Link]("[Link]:");
[Link]("[Link]:");
[Link]("[Link]:");
[Link]("[Link]:");
[Link]("[Link]:");
[Link]("[Link]-root:");*/
User Output
1. addition 1
2. subtraction 1
3. multiplication 1
4. division 1
5. square 1
6. cube 1
7. square-root 1
Enter your Choice: 1
Enter Two Integer Numbers: 55 63
Addition of 55 and 63 is : 118
Test Case - 2
User Output
1. addition 2
2. subtraction 2
3. multiplication 2
4. division 2
[Link] 3/5
6/16/2021 [Link]
Test Case - 2
5. square 2
6. cube 2
7. square-root 2
Page No:
Enter your Choice: 2
Enter Two Integer Numbers: 55 96
Subtraction of 55 and 96 is : -41
ID: CSE016
Test Case - 3
User Output
1. addition 3
2. subtraction 3
3. multiplication 3
4. division 3
5. square 3
6. cube 3
7. square-root 3
Enter your Choice: 3
Enter Two Integer Numbers: 5 6
Multiplication of 5 and 6 is : 30
Test Case - 4
User Output
1. addition 4
2. subtraction 4
3. multiplication 4
4. division 4
5. square 4
6. cube 4
Test Case - 5
User Output
1. addition 5
2. subtraction 5
3. multiplication 5
4. division 5
5. square 5
6. cube 5
7. square-root 5
Enter your Choice: 5
Enter an Integer Number: 9
Square of 9 is : 81.0
Test Case - 6
User Output
[Link] 4/5
6/16/2021 [Link]
Test Case - 6
1. addition 6
2. subtraction 6
3. multiplication 6
Page No:
4. division 6
5. square 6
6. cube 6
ID: CSE016
7. square-root 6
Enter your Choice: 6
Enter an Integer Number: 6
Cube of 6 is : 216.0
Test Case - 7
User Output
1. addition 7
2. subtraction 7
3. multiplication 7
4. division 7
5. square 7
6. cube 7
7. square-root 7
Enter your Choice: 7
Enter an Integer Number: 9
Square-root of 9 is : 3.0
Test Case - 8
User Output
1. addition 3
2. subtraction 3
Test Case - 9
User Output
1. addition 9
2. subtraction 9
3. multiplication 9
4. division 9
5. square 9
6. cube 9
7. square-root 9
Enter your Choice: 9
Invalid choice
[Link] 5/5
6/16/2021 [Link]
Exp. Name: Program to implement abstraction, create a class with one abstract
[Link]: 15 Date:
method and implementing them in another class.
Page No:
Aim:
Write a java program to implement abstraction. Create a class called Figure which contains two variables dim1,
dim2 and one abstract method area () of double type. Define two classes from Figure class namely Rectangle
and Triangle respectively and implement such abstract method and display the results.
ID: CSE016
Source Code:
[Link]
import [Link].*;
abstract class Figure{
double dim1,dim2;
Figure(double d1, double d2){
dim1=d1;
dim2=d2;
}
abstract double area();
}
class Rectangle extends Figure{
Rectangle(double d1,double d2){
super(d1,d2);
}
double area(){
return dim1*dim2;
}
}
[Link] 1/2
6/16/2021 [Link]
}
}
Page No:
Execution Results - All test cases have succeeded!
Test Case - 1
ID: CSE016
User Output
Enter length of rectangle: 100
Enter breadth of rectangle: 20
The area of rectangle with sides 100.0 and 20.0 is: 2000.0 50
Enter base of triangle: 50
Enter height of triangle: 82
The area of rectangle with sides 50.0 and 82.0 is: 2050.0
Test Case - 2
User Output
Enter length of rectangle: 54
Enter breadth of rectangle: 20
The area of rectangle with sides 54.0 and 20.0 is: 1080.0 35
Enter base of triangle: 35
Enter height of triangle: 50
The area of rectangle with sides 35.0 and 50.0 is: 875.0
Test Case - 3
User Output
Enter length of rectangle: 5
Enter breadth of rectangle: 10
[Link] 2/2
6/16/2021 [Link]
Aim:
Page No:
Write a Java program to implement multiple-inheritance.
Source Code:
[Link]
ID: CSE016
import [Link];
interface I1{
void setData(int l,int b);
}
interface I2{
int perimeter();
int area();
}
class MultipleInheritance implements I1,I2{
int l,b;
public void setData(int i,int j){
l=i;
b=j;
}
public int perimeter(){
if(l==b)
return 4*l;
else
return 2*(l+b);
}
public int area(){
return l*b;
}
public static void main(String args[]){
Scanner s=new Scanner([Link]);
Test Case - 1
User Output
Enter length of rectangle 1: 25
Page No:
Enter breadth of rectangle 1: 90
Perimeter of rectangle 1 is: 230 70
Area of rectangle 1 is: 2250 70
Enter length of rectangle 2: 70
ID: CSE016
Enter breadth of rectangle 2: 70
Perimeter of rectangle 2 is: 280
Area of rectangle 2 is: 4900
Test Case - 2
User Output
Enter length of rectangle 1: 360
Enter breadth of rectangle 1: 20
Perimeter of rectangle 1 is: 760 15
Area of rectangle 1 is: 7200 15
Enter length of rectangle 2: 15
Enter breadth of rectangle 2: 25
Perimeter of rectangle 2 is: 80
Area of rectangle 2 is: 375
[Link] 2/2
6/16/2021 [Link]
[Link]: 17 Exp. Name: Write a Java program to sort a list of names in ascending order. Date:
Aim:
Page No:
Write a Java program to sort a list of names in ascending order.
Source Code:
[Link]
ID: CSE016
import [Link].*;
class SortNames{
public static void main(String[] args)
{
int n;
String temp;
Scanner s = new Scanner([Link]);
[Link]("Enter the number of names you want to enter: ");
n = [Link]();
String names[] = new String[n];
Scanner s1 = new Scanner([Link]);
[Link]("Enter the names: ");
for(int i = 0; i < n; i++)
{
names[i] = [Link]();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (names[i].compareTo(names[j])>0)
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
Test Case - 1
User Output
Enter the number of names you want to enter: 5
Enter the names: Roy
Dora Dora
[Link] 1/2
6/16/2021 [Link]
Test Case - 1
Zoya Zoya
Suzan Suzan
Page No:
Harry Harry
Sorted names:Dora Harry Roy Suzan Zoya
Test Case - 2
ID: CSE016
User Output
Enter the number of names you want to enter: 10
Enter the names: Matt
Lionel Lionel
Gary Gary
Daniel Daniel
Chole Chole
Andreas Andreas
Sergio Sergio
Eden Eden
zenith zenith
Tim Tim
Sorted names:Andreas Chole Daniel Eden Gary Lionel Matt Sergio Tim zenith
Test Case - 3
User Output
Enter the number of names you want to enter: 7
Enter the names: Jhon
Jack Jack
Jhonson Jhonson
Jarden Jarden
[Link] 2/2
6/16/2021 [Link]
[Link]: 18 Exp. Name: Program to check the given String is Palindrome or not Date:
Aim:
Page No:
Create a class PalindromeOrNot with a main method. The method receives one command line argument.
Check the given argument is palindrome or not.
For example:
ID: CSE016
Cmd Args : madam
The given string madam is a palindrome
q11184/[Link]
package q11184;
class PalindromeOrNot {
public static void main(String ar[]) throws Exception{
String s=ar[0];
String temp=s;
StringBuffer str=new StringBuffer(s);
temp=[Link]().toString();
if([Link](s))
[Link]("The given string "+s+" is a palindrome");
else
[Link]("The given string "+s+" is not a palindrome");
}
Test Case - 1
User Output
The given string madam is a palindrome
Test Case - 2
User Output
The given string Godavari is not a palindrome
Test Case - 3
User Output
The given string malayalam is a palindrome
[Link] 1/2
6/16/2021 [Link]
Test Case - 4
User Output
The given string 12345 is not a palindrome
Page No:
ID: CSE016
Rajeev Gandhi Memorial College of Engineering and Technology (Autonomous)
[Link] 2/2
6/16/2021 [Link]
[Link]: 19 Exp. Name: Write a Java program that handles multiple exceptions. Date:
Aim:
Page No:
Write a Java program that handles multiple exceptions.
Source Code:
[Link]
ID: CSE016
import [Link].*;
import [Link].*;
import [Link].*;
class CatchMultipleException {
public static void main(String args[]) {
int num1, num2;
int arr[] = new int[10];
Scanner sc = new Scanner([Link]);
try {
/*Take values for numerator and denominator from the console */
[Link]("Enter a value for numerator: ");
num1=[Link]();
[Link]("Enter a value for denominator: ");
num2=[Link]();
arr[10] = num1/num2 ;
}
Test Case - 1
User Output
Enter a value for numerator: 15
Enter a value for denominator: 0
Exception caught: / by zero
Test Case - 2
User Output
Enter a value for numerator: 30
Enter a value for denominator: 2
Exception caught: Index 10 out of bounds for length 10
[Link] 1/1
6/16/2021 [Link]
Exp. Name: Create a user exception and then handles that exception in other
[Link]: 20 Date:
program.
Page No:
Aim:
Write a Java program to handle user defined exception. Create a user exception and then handle that
exception in other program.
Source Code:
ID: CSE016
[Link]
import [Link].*;
import [Link].*;
[Link]
/* Create a method validWeight which checks if the weight is more than 100 and
throws InvalidWeight exception otherwise. */
import [Link];
public class CheckWeight {
public static void validWeight(int wt) {
try{
if(wt>100)
throw new InvalidWeight(wt+" is invalid weight");
else
[Link](wt+" is the valid weight.");
Test Case - 1
User Output
Enter weight: 101
Exception caught: 101 is invalid weight
[Link] 1/2
6/16/2021 [Link]
Test Case - 2
User Output
Enter weight: 99
Page No:
99 is the valid weight.
Test Case - 3
ID: CSE016
User Output
Enter weight: 1000
Exception caught: 1000 is invalid weight
[Link] 2/2
6/16/2021 [Link]
Aim:
Page No:
Write a Java program that reads a file name from the user then displays information about whether the file
exists, whether the file is readable, whether the file is writable, the type of file and the length of the file in bytes.
Source Code:
ID: CSE016
[Link]
import [Link].*;
class FileInfo
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter file name([Link] or [Link]): ");
String fname=[Link]();
File f=new File(fname);
[Link]("File existance: " +[Link]());
[Link]("File writable: " +[Link]());
[Link]("File readable: " +[Link]());
[Link]("File length(in bytes): " +[Link]() + " Bytes");
}
}
[Link]
We know that we cannot save our environment overnight. But, having an intention to make
possible is all that count. Reduce wastage of papers. Try not to ruin plants. They
[Link]
[Link] 1/2
6/16/2021 [Link]
Test Case - 1
User Output
Enter file name([Link] or [Link]): [Link]
Page No:
File existance: false
File writable: false
File readable: false
File length(in bytes): 0 Bytes
ID: CSE016
Test Case - 2
User Output
Enter file name([Link] or [Link]): [Link]
File existance: true
File writable: true
File readable: true
File length(in bytes): 521 Bytes
[Link] 2/2
6/16/2021 [Link]
[Link]: 22 Exp. Name: Count characters, words and lines in a file. Date:
Aim:
Page No:
Write a Java program that displays the number of characters, lines and words in a text file.
Source Code:
[Link]
ID: CSE016
import [Link].*;
import [Link].*;
class CountFile
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner([Link]);
[Link]("Enter file name([Link] or [Link]): ");
String fname=[Link]();
int cno=0;
int lno=0;
int wno=0;
BufferedReader br=null;
try
{
br=new BufferedReader(new FileReader(fname));
}
catch(FileNotFoundException fe)
{
[Link]("File not found.");
}
String str;
while((str=[Link]())!=null)
{
}
wno=wno+[Link];
}
[Link]("Lines: " +lno);
[Link]("Words: " +wno);
[Link]("Characters: " +cno);
[Link]();
}
}
[Link]
[Link] 1/2
6/16/2021 [Link]
Your every action will count. You should not only hold others responsible, make yoursel
f responsible too.
Page No:
Why not start saving our environment being a little less self-concerned. Sometimes give
priority to the
nature before giving priority to yourself. Save the energy, save plants and be sympathe
tic to the nature
ID: CSE016
surrounding us.
Life priorities and necessities are never going to reduce. But among all of them, make
some time for ensuring
the well being of the environment you live in. To save our environment, no life changin
g movement is required.
If anything is required, that is will power, honest inclination and some small initiati
ves. Save our
environment by being a responsible citizen. Teach your child and others to save water.
Do not waste water.
It is a very precious element of our environment.
[Link]
So we should try to save our environment by making the small day to day initiatives.
The first thing you should do is try to save water, trees and electricity. This will
make a big difference. Also try to spread good words and educate children about it. Lov
e
the nature to save the earth for our own future.
Test Case - 1
Test Case - 2
User Output
Enter file name([Link] or [Link]): [Link]
File not found.
[Link] 2/2
6/16/2021 [Link]
[Link]: 23 Exp. Name: Write a Java program demonstrating the usage of Threads Date:
Aim:
Page No:
Write a Java program that uses three threads to perform the below actions:
1. First thread should print "Good morning" for every 1 second for 2 times
2. Second thread should print "Hello" for every 1 seconds for 2 times
3. Third thread should print "Welcome" for every 3 seconds for 1 times
ID: CSE016
Write appropriate constructor in the Printer class which implements Runnable interface to take three
arguments : message, delay and count of types String, int and int respectively.
Write code in the [Link]() method to print the message with appropriate delay and for number of
times mentioned in count.
Write a class called ThreadDemo with the main() method which instantiates and executes three instances
of the above mentioned Printer class as threads to produce the desired output.
[Note: If you want to sleep for 2 seconds you should call [Link](2000); as the
[Link](...) method takes milliseconds as argument.]
q11349/[Link]
package q11349;
public class ThreadDemo {
public static void main(String[] args) throws Exception {
Thread t1 = new Thread(new Printer("Good morning", 1, 2));
Thread t2 = new Thread(new Printer("Hello", 1, 2));
Thread t3 = new Thread(new Printer("Welcome", 3, 1));
[Link]();
[Link]();
try{
[Link](time);
[Link] 1/2
6/16/2021 [Link]
for(int i=0; i< delay; i++) {
[Link](message);
}
}
catch(InterruptedException ie){
Page No:
[Link](ie);
}
}
ID: CSE016
}
Test Case - 1
User Output
Good morning
Hello
Welcome
Good morning
Hello
All the three threads t1, t2 and t3 have completed execution.
[Link] 2/2
6/16/2021 [Link]
[Link]: 24 Exp. Name: Java program that correctly implements the producer consumer problem Date:
Aim:
Page No:
Write a Java program that correctly implements the producer-consumer problem using the concept of
interthread communication.
Source Code:
ID: CSE016
q2499/[Link]
package q2499;
class Q {
int n;
boolean valueSet = false;
synchronized int get() {
while(!valueSet)
try {
wait();
} catch(InterruptedException e) {
[Link]("InterruptedException caught");
}
[Link]("Got: " + n);
valueSet = false;
notify();
return n;
}
synchronized void put(int n) {
while(valueSet)
try {
wait();
} catch(InterruptedException e) {
[Link]("InterruptedException caught");
}
this.n = n;
}
public void run() {
for(int i=0;i<n;i++)
{
[Link](i);
}
[Link] 1/3
6/16/2021 [Link]
Page No:
Consumer(Q q,int n) {
this.q = q;
this.n=n;
ID: CSE016
//new Thread(this, "Consumer").start();
}
public void run() {
for(int i=0;i<n;i++) {
[Link]();
}
}
}
class ProdCons {
public static void main(String args[]) throws Exception{
Q q = new Q();
int n=[Link](args[0]);
Producer p=new Producer(q,n);
Consumer c=new Consumer(q,n);
Thread t=new Thread(p);
Thread t1=new Thread(c);
[Link]();
[Link]();
[Link]("Producer-Consumer problem using the concept of Interthread Communic
ation");
}
}
User Output
Producer-Consumer problem using the concept of Interthread Communication
Put: 0
Got: 0
Put: 1
Got: 1
Put: 2
Got: 2
Put: 3
Got: 3
Put: 4
Got: 4
Test Case - 2
User Output
Producer-Consumer problem using the concept of Interthread Communication
Put: 0
[Link] 2/3
6/16/2021 [Link]
Test Case - 2
Got: 0
Put: 1
Got: 1
Page No:
Put: 2
Got: 2
Put: 3
ID: CSE016
Got: 3
Put: 4
Got: 4
Put: 5
Got: 5
Put: 6
Got: 6
Put: 7
Got: 7
Test Case - 3
User Output
Producer-Consumer problem using the concept of Interthread Communication
Put: 0
Got: 0
Put: 1
Got: 1
Put: 2
Got: 2
Put: 3
Got: 3
Put: 4
Got: 4
Put: 5
[Link] 3/3
6/16/2021 [Link]
Aim:
Page No:
[Link] a Java program to store student class objects in Array List and sort the ArrayList. Create a class name
ArrayListDemo with a main method. The method takes student names as inputs from the command line
arguments. Wto store student class objects in Array List and sort the ArrayList?
ID: CSE016
Source Code:
q24085/[Link]
package q24085;
import [Link].*;
// write your code here
class ArrayListIterationDemo
{
public static void main(String args[])
{
ArrayList<String> obj = new ArrayList<String>();
for(int i=0;i<[Link];i++)
{
[Link](args[i]);
}
[Link]( obj);
[Link](obj);
[Link]("After sorting : " +obj);
}
User Output
[Ram, Ravi, Raj]
After sorting : [Raj, Ram, Ravi]
Test Case - 2
User Output
[Lalitha, Lavanya, Ramya]
After sorting : [Lalitha, Lavanya, Ramya]
Test Case - 3
User Output
[Suguna, Srujana, Sruthi]
After sorting : [Srujana, Sruthi, Suguna]
[Link] 1/1
6/16/2021 [Link]
Aim:
Page No:
Write a Java program to create HashSet from ArrayList and remove duplicates and display results?
Source Code:
[Link]
ID: CSE016
import [Link].*;
class ArrayListIterationDemo
{
public static void main(String args[])throws Exception
{
ArrayList<String> obj=new ArrayList<String>();
for(int i=0;i<[Link];i++)
{
[Link](args[i]);
}
[Link](obj);
HashSet<String> has=new HashSet(obj);
}
}
Test Case - 1
Test Case - 2
User Output
[suguna, suguna, srujana]
List after removing duplicate elements:
srujana
suguna
[Link] 1/1
6/16/2021 [Link]
Page No:
Aim:
Write a java program to traversing elements in descending order in TreeSet.
Source Code:
ID: CSE016
[Link]
import [Link].*;
class HashSetMethodsDemo
{
public static void main(String args[])throws Exception
{
TreeSet<Object> ints = new TreeSet<Object>();
for(int i=0;i<[Link];i++)
{
[Link](args[i]);
}
TreeSet<Object> intsReverse =
(TreeSet<Object>)[Link]();
[Link]("Traversing element through Iterator in descending orde
r");
for(Object ob : intsReverse)
{
[Link](ob);
}
}
}
Test Case - 1
User Output
Traversing element through Iterator in descending order
Yamuna
Rama
Krishna
Ganga
[Link] 1/1
RAJEEV GANDHI MEMORIAL COLLEGE OF ENGINEERING & TECHNOLOGY
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
1. Of the 25 marks for internal, 10 marks will be awarded for day-to-day work and 10 marks
to be awarded for the Record work and 5 marks to be awarded by conducting an internal
laboratory test.
2. Concerned Teachers have to do necessary corrections with explanations.
3. Concerned Lab teachers should enter marks in index page.
4. Internal exam will be conducted by two Staff members.
1. For Practical subjects there is a continuous evaluation during the semester for 25 Sessional
marks and 50 end examination marks.
2. The end examination shall be conducted by the teacher concerned (Internal Examiner) and
another External Examiner, recommended by Head of the Department with the approval of
principal.