0% found this document useful (0 votes)
29 views20 pages

Computer Project 2

The document outlines various Java programs involving arrays, strings, sorting, and searching algorithms: 1) It includes programs to perform linear and binary searches on arrays to find a user-input number. 2) Programs are provided to sort arrays using bubble, selection, and insertion sorts. 3) Additional programs calculate statistics of strings like numbers of uppercase, lowercase, digits and special characters. 4) A program is given to find the total number of characters in a string excluding spaces. The document serves as a reference for different array and string programs in Java along with explanations of the algorithms and code snippets.
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)
29 views20 pages

Computer Project 2

The document outlines various Java programs involving arrays, strings, sorting, and searching algorithms: 1) It includes programs to perform linear and binary searches on arrays to find a user-input number. 2) Programs are provided to sort arrays using bubble, selection, and insertion sorts. 3) Additional programs calculate statistics of strings like numbers of uppercase, lowercase, digits and special characters. 4) A program is given to find the total number of characters in a string excluding spaces. The document serves as a reference for different array and string programs in Java along with explanations of the algorithms and code snippets.
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/ 20

CONTENTS

• SIMPLE PRINTING
• ASSIGNMENT
• ACCEPTING VALUE
• MATH FUNCTIONS
• INPUT ACCEPT DATA
• IF ELSE PROGRAMS
• SLAB PROGRAMS
• SWITCH CASE
• LOOPS
• NUMBER PROGRAMS
• USER DEFINED METHODS
• ARRAYS
• STRINGS
ARRAYS

Q.ACCEPT ANY NUMBER FROM THE USER ,


SEARCH THAT NUMBER IN THE FOLLOWING
ARRAY A. DISPLAY APPROPIATE ERROR
MESSAGE , IF THE NUMBER DOES NOT EXIST
IN THE ARRAY A.
THE ELEMENTS ARE 15,30,1,-
1,0,25,350,150,200,100( USING LINEAR SEARCH )

import java.util.Scanner;
class array 1
{
public static void main()
{
Scanner ob=new Scanner(System.in);i
int I, j,
int l=a.length;
System.out.println(“ enter the number to be searched in
the list ”);
int a []={15,30,1,-1,0,25,350,150,200,100};
b
oolean f=false;
for (i=0;i<l;i++)
{
If (a[]==n)
{
System.out.println(“ exist in the list “);
f=true;
break;
}}
If (f== false)
System.out.println(n+”doesn’t exist in the list “);
}
OUTPUT:
ENTER A NUMBER TO BE SEARCHED IN THE
LIST
30
EXIST IN THE LIST

MNEMONIC CODE:
VARIABLE TYPE DESCRIPTION
I INT TO STORE
COLUMN
J INT TO STORE
ROW
A STRING TO STORE IN
AN ARRAY
N INT TO STORE A
NUMBER
F BOOLEAN TO STORE
FALSE
BINARY SEARCH
Q.ACCEPT ANY NUMBER FROM THE USER ,
SEARCH THAT NUMBER IN THE FOLLOWING
ARRAY A. DISPLAY APPROPIATE ERROR
MESSAGE , IF THE NUMBER DOES NOT EXIST
IN THE ARRAY A.
THE ELEMENTS ARE
15,35,40,66,70,95,100,200,310,501( USING BINARY
SEARCH )

import java.util.Scanner;
class array 2
{
public static void main()
{
Scanner ob=new Scanner(System.in);i
int f=0;
int mid,u=9,l=0;
int l=a.length;
System.out.println(“ enter the number to be searched in
the list ”);
int a []={15,35,40,66,70,95,100,200,310,501};
while(l<=u)
{
Mid=(l+u)/2;
If (a[mid]==s)
{
System.out.print(“ found in the list “);
f=1;
break;
}
If (s>a[mid])
L= mid+1;
Else if (s<a[mid])
U=mid -1;
}
If (f==0)
System.out.print(s+”not found in the list “);
}}
OUTPUT:
ENTER A NUMBER TO BE SEARCHED IN THE
LIST
66
FOUND IN THE LIST

MNEMONIC CODE:
VARIABLE TYPE DESCRIPTION
mid int To store middle
value
l int To store the last
number
u int To store the first
number
f int To store if found
in the list
s int To store the
number
BUBBLE SORT
Q. ARRANGE THE FOLLOWING ELEMENTS OF
ARRAY A IN THE ASCENDING ORDER
THE ELEMENTS ARE
“HARMAN”,”GUNGUN”,”ANCHITA”,”PARI”,”AN
AISHAA”,”NAYONICA”

Import java.util.Scanner;
Classq2
{
Public static void main()
{
Scanner ob=new Scanner(System.in);
Int I,j;
String x,y;
String
a[]={HARMAN”,”GUNGUN”,”ANCHITA”,”PARI”,”
ANAISHAA”,”NAYONICA};
Int l=a.length;
For (i=0 ,i<l, i++)
{
For(j=1,j<l-i-1,j++)
{
If (a[i].compareTo(a[j]);
{
X=a[j];
Y=a[j+1];
A[j]=y;
A[j+1]=x;
}
}
}
For (i=0,i<l,i++)
{
System.out.println(a[i]);
}
}
OUTPUT
ANAISHAA
ANCHITA
GUNGUN
HARMAN
NAYONICA
PARI

MNEMONIC CODE
VARIABLE TYPE DESCRIPTION
x string To interchange
with the other
array y
a string To store the
Original array
y string To interchange
with the other
array x
i int To use in for
loop
j int To use in for
loop

SELECTION SORT
Q.ARRANGE THE FOLLOWING ELEMENTS OF
AN ARRAY IN THE ASCENDING ORDER
THE ELEMENTS ARE 15,30,1-1,25,35,5,20,10

import java.util.Scanner;
class q3
{
public static void main()
{
int I,j,x,y;
int a[]={15,30,1-1,25,35,5,20,10};
int l=a.length;
for(i=0;i<l;i++)
{
for(j=i+1;j<l;j++)
{
If(a[i]>a[j])
{
x=a[i];
y=a[j];
a[j]=x;
a[i]=y;
}
System.out.println(“ array after sorting “);
for (i=0;i<l;i++)
{
System.out.println(a[i]);
}
}}
OUTPUT
-1,1,5,10,15,20,25,30,35

MNEMONIC CODE
VARIABLE TYPE DESCRIPTION
a int To store array
i int To store in loop
j int To store in loop
l int To store the
length of the
array
STRINGS
Q,TOTAL
UPPERCASE,LOWERCASE,DIGITS,SPECIAL
CHARACTERS IN THE STRING

import java.util.Scanner;
class string 2
{
public static void main()
{
int vc,lc,d,sp;
for(i=0;i<l;i++)
{
c=s.charAt(i);
If(c>=65 && c<=90)
{
uc++;
}
else if (c>=97 &&c<=122)
{
lc++
}
else if (c>=48 && c<=57)
{
d++;
}
else if (c>=33 && c<=47)
{
sp++;
}
}}
OUTPUT
harmaN kaUr
uc=2
sp=0
d=0
lc=8

MNEMONIC CODE
VARIABLE TYPE DESCRIPTION
uc int To store upper
case
sp int To store special
characters
lc int To store lower
case
d int To store digits
s int To store string
c char To store
characters
Q. total no of characters in the string ( excluding blank
space ).

import java.util.Scanner;
class string 3b
{
public static void main()
int I,l,b;
char c;
string s ;
Scanner ob=new Scanner(System.in);
System.out.println(“enter a string “);
s=ob.nextLine();
l=s.length();
for(i=0;i<l;i++)
{
c=s.charAt(i);
if (c==” “) //(c!=” “ )
b++;
System.out.println(“ total characters in a string “+(l-b));
}}
OUTPUT
Enter a string
Harman*kaur
Total number of characters – 9

Mnemonic code
VARIABLE TYPE DESCRIPTION
i int To store in the
loop
l int To store in the
loop
b int To store blank
space
c char To store a
character
s String To store a string

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy