0% found this document useful (0 votes)
12 views7 pages

Practicals 28-10-2024

The document contains multiple Java programs demonstrating various character and array manipulations, including methods to check character types, auto boxing and unboxing, sorting algorithms (selection and bubble sort), binary search, and checking for identical character arrays. Each program includes variable descriptions and example outputs. The document serves as a practical guide for implementing basic programming concepts in Java.

Uploaded by

nadaagroup
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Practicals 28-10-2024

The document contains multiple Java programs demonstrating various character and array manipulations, including methods to check character types, auto boxing and unboxing, sorting algorithms (selection and bubble sort), binary search, and checking for identical character arrays. Each program includes variable descriptions and example outputs. The document serves as a practical guide for implementing basic programming concepts in Java.

Uploaded by

nadaagroup
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

7 write a method to check if entered character is digit,letter or space and another

print method to print uppercase if the character is Lowercase and vice versa.

public class characterfunctions


{
public void check(char ch)
{
if(Character.isLetter(ch))
System.out.println(ch+"is a letter");
else if(Character.isDigit(ch))
System.out.println(ch+"is a Digit");
else if(Character.isSpaceChar(ch))
System.out.println(ch+"is a Unicode space");
else if(Character.isWhitespace(ch))
System.out.println(ch+"is a blank");
}
public void print(char c)
{
if(Character.isLowerCase(c))
System.out.println(c+" is a Lowercase letter"+" and its uppercase is "+Character.toUpperCase(c));
else if(Character.isUpperCase(c))
System.out.println(c+" is a Uppercase letter"+" and its Lowercase is "+Character.toLowerCase(c));

}
}

Variable Description table

Variable name Datatype Purpose


ch char Receives the character
passed to the check method.
Used to determine if it is a
letter, digit, space, or blank.
c char Receives the character
passed to the print method.
Used to determine if it is
uppercase or lowercase and
prints the opposite case.

Output
If ‘c’ is passed in the argument for method check
Result: c is a Letter

If ‘a’ is passed in the argument for method check


a is a Lowercase letter and its uppercase is A
8 example program for auto boxing and unboxing*/
public class autoboxing
{
void result()
{
char ch='G';
int num=75;
double v=598.25d;
Integer obj=new Integer(num);//autoboxing
Double val=new Double(v);//autoboxing
Character st=new Character(ch);//autoboxing
System.out.println("Value of wrapper class Integer object obj="+obj);
System.out.println("Value of wrapper class character object obj="+st);
System.out.println("Value of wrapper class double object obj="+val);
ch=st;//unboxing
num=obj;//unboxing
v=val;//unboxing
System.out.println("Value of primitive data type char="+ch);
System.out.println("Value of primitive data type int="+num);
System.out.println("Value of primitive data type double="+v);
}
}

9
Write a program to initialize a single dimensional array x[] of 10 integer elements. Arrange the array in ascending
order using selection sort method. Print the array before and after sorting. Each array should be printed in single
line with a suitable space between each element.

import java.io.*;
public class selectionsort
{ public void sorting()
{
int x[]={22,8,4,11,5,78,2,7,15,44};
int i,j,index,small,temp;
System.out.println("Unsorted array or array before sorting");
for(i=0;i<10;i++)
{System.out.print(x[i]+"\t");
}
for(i=0;i<10-1;i++)
{
small=x[i];
index=i;
for(j=i+1;j<10;j++)
{
if(x[j]<small)
{
small=x[j];
index=j;
}
}
temp=x[i];
x[i]=x[index];
x[index]=temp;
}
System.out.println("\n sorted array or array after sorting");
for(i=0;i<10;i++)
{
System.out.print(x[i]+"\t");
}
}}

output
Unsorted array or array before sorting
22 8 4 11 5 78 2 7 15 44
sorted array or array after sorting
2 4 5 7 8 11 15 22 44 78

Variable description table:


Variable name Datatype Purpose
X[] int To initialize array of integers
small int To store smallest element
index int To store index of smallest
element
temp Int To help in swapping element
i,j int,int Loop variable

10 Write a program to initialize a single dimensional array x[] of 10 integer elements. Arrange the array in ascending
order using bubble sort method. Print the array before and after sorting. Each array should be printed in a single
line with suitable space between each element.
import java.io.*;
public class bubblesort
{
public void sorting()
{
int x[]={22,8,4,11,5,78,2,7,15,44};
int i,j,temp;
System.out.println("Unsorted array or array before sorting");
for(i=0;i<10;i++)
{System.out.print(x[i]+"\t");
}
for(i=0;i<10-1;i++)
{
for(j=0;j<10-i-1;j++)
{
if(x[j]>x[j+1])
{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
}}
System.out.println("\n sorted array or array after sorting");
for(i=0;i<10;i++)
{
System.out.print(x[i]+"\t");
}
}
}
output
Unsorted array or array before sorting
22 8 4 11 5 78 2 7 15 44
sorted array or array after sorting
2 4 5 7 8 11 15 22 44 78

Variable description table


Variable name Datatype Purpose
X[] int To initialize array of integers
temp Int To help in swapping element
i,j int,int Loop variable
11 Design a function void BinarySearch(int val) to initialize a single dimensional array num[] containing 15 integers.
Arrange the array the num[] in ascending order. Search from sorted array num[] using binary search technique.
Print the sorted array num[] in s single line. Also print the index/position of value, if found in the sorted array
otherwise print a message.” Not found in the list”, search unsuccessful” along with value of val.

import java.io.*;
public class Bin_Search
{
public void BinarySearch(int val)
{
int num[]={22,12,10,8,4,11,5,78,2,3,7,15,44,52,25};
int j,i,pos=0,temp;
int found=0;
for(i=0;i<15-1;i++)
{
for(j=0;j<15-i-1;j++)
{
if(num[j]>num[j+1])
{
temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
int s=0,e=14,mid=0;
while(s<=e)
{
mid=(s+e)/2;
if(val==num[mid])
{
found=1;
pos=mid;
break;}
else if(val>num[mid])
s=mid+1;
else if(val<num[mid])
e=mid-1;
}
System.out.println("\n sorted array or array after sorting");
for(i=0;i<10;i++)
{
System.out.print(num[i]+"\t");
}
System.out.println();
if(found==1)
System.out.println(val+" is found at"+pos+"index/position");
else
System.out.println(val+"not found in the list");
}
}
output:

sorted array or array after sorting


2 3 4 5 7 8 10 11 12 15
12 is found at8index/position

Variable description table


Variable name Datatype Purpose
num[] int To initialize array of integers
temp Int To help in swapping element
i,j int,int Loop variable
pos int To store index if value is
found
s,e int,int To mange start and last
indexes
mid int To find middle index
12 Write a program to initialize the following character array and print a suitable message after checking the array
whether the two array are identical or not. Make suitable use of Boolean data type.
X[]={‘m’,’n’,’o’,’p’}
Y[]={‘m’,’n’,’o’,’p’}

Solution:
import java.io.*;
public class problem10
{
public void check()
{
char x[]={'m','n','o','p'};
char y[]={'m','n','o','p'};
int size1=x.length;
int size2=y.length;
boolean result=true;
System.out.println("elements of array x=");
for(int i=0;i<size1;i++)
{
System.out.print(x[i]+"\t");
}
System.out.println("\nelements of array y=");
for(int i=0;i<size2;i++)
{
System.out.print(y[i]+"\t");
}
if(size1!=size2)
System.out.println("The array are not identical, because size of both array are not same");
else
for(int i=0;i<size1;i++)
{
if(x[i]!=y[i])
{
result=false;
break;
}
}
if(result==true)
System.out.println("\n Both the array are identical");
else
System.out.println("The given array are not identical");
}
}
output:
elements of array x=
m n o p
elements of array y=
m n o p
Both the array are identical
Variable description table
Variable name Datatype Purpose
x[] char Character array having
characters
y[] char Character array having
characters
size1 int To find number of elements
ofx[]
size2 int To find number of elements
of y[]
result Boolean To store value true or false
i int Loop variable

You might also like