Professional Documents
Culture Documents
import java.io.*;
import java.util.StringTokenizer;
String arrNames=br.readLine();
String name=br.readLine();
int countNoOccurance=0;
while(st1.hasMoreTokens())
if(name.equalsIgnoreCase(st1.nextToken()))
countNoOccurance++;
}
}
I/O:
Dave Ann George Sam Ted Gag Saj Agati Mary Sam Ayan Dev Kity Meery Smith Johnson
Bill Williams Jones Brown Davis Miller Wilson Moore Taylor Anderson Thomas Jackson
sam
This code defines that when any element of the integer array is occurred then it is
printed first and after that it’s incremented by 10 and then the resultant array is printed, the
same code is being defined in the component classes of java.utill.* The component class is
the abstract superclass of the non menu-related Abstract Window Toolkit components. Class
component can also be extended directly to create a lightweight component. A lightweight
component is a component that is not associated with a native opaque window. The code is
predefined in the package and whenever we want to execute any application which wants the
code. Then without writing the whole code we can directly import the package and execute
that application
}
Question 2
gcd(a,b) = a if a =b
import java.io.*;
import java.util.Scanner;
class Main
int a=sr.nextInt();
int b=sr.nextInt();
int gcd;
if(a==b)
gcd=a;
else if(a>b)
gcd=findgcd(a-b,b);
else
gcd=findgcd(b,b-a);
System.out.println("The greatest common divisor of two positive numbers " + a + " and " + b
+ " is " + gcd);
if (d == 0)
return c;
I/O:
Bubble sort is also known as exchange sort. Bubble sort is a simplest sorting
algorithm. In bubble sort algorithm array is traversed from 0 to the length-1
index of the array and compared one element to the next element and swap
values in between if the next element is less than the previous element. In
other words, bubble sorting algorithm compare two values and put the
largest value at largest index. The algorithm follow the same steps
repeatedly until the values of array is sorted. In worst-case the complexity of
bubble sort is O(n2) and in best-case the complexity of bubble sort is O(n).
{
int out, in; // declaring two integer variables 'out' & 'in'
for(out=nElems-1; out>1; out--) //outer loop
{
for(in=0; in<out; in++) //inner loop
{
if( a[in] > a[in+1] ) //conditional statement to compare the adjacent values
swap(in, in+1); //swapping the two values in specified order
}
}
}
public static void swap(int one, int two) //defining 'swap' function toperform swapping of
elements
{
int temp = a[one]; //interchanging the values
a[one] = a[two];
a[two] = temp;
}
public static void main(String args[]) //definition of main method
{
ArrayBub(3); //calling the method 'arraybub() with 3as parameter'
insert(3); //calling the method 'insert()with 3 as parameter'