You are on page 1of 7

Question 1

Search for a name


Write a program to accept an array of names and a name and check whether the name
is present in the array. Return the count of occurrence. Use the following array as
input{“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”}

import java.io.*;

import java.util.StringTokenizer;

public class Main

public static void main(String args[])throws Exception

InputStreamReader isr=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(isr);

System.out.println("ENTER ARRAY OF NAMES:");

String arrNames=br.readLine();

StringTokenizer st1=new StringTokenizer(arrNames);

System.out.println("ENTER THE NAME TO BE SEARCHED:");

String name=br.readLine();

int countNoOccurance=0;

while(st1.hasMoreTokens())

if(name.equalsIgnoreCase(st1.nextToken()))

countNoOccurance++;

}
}

System.out.println("THE NAME "+name+" HAS BEEN APPEARED


"+countNoOccurance+" NO. OF TIMES");

I/O:

ENTER ARRAY OF NAMES:

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

ENTER THE NAME TO BE SEARCHED:

sam

THE NAME sam HAS BEEN APPEARED 2 NO. OF TIMES

Improve the understandability of the below given code:

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

Step by step explanation:


import java.util.*; //importing the required packages import java.io.*;
class Problem3 //creating a class
{
public static void Increment(int[] arr) //defining the 'increment()'method with one
integer parameter
{
int len = arr.length; // initializing a integer variable 'len' with the length, i.e. the no of
elements in array
System.out.println("\nBefore Increment, The Array was :");
for (int i = 0; i < len; i ++) //definition of 'for' loop toprint the current values of array
{
System.out.print(arr[i]+"\t");
}
for (int i = 0; i < len; i ++) //definition of 'for' loop toincrement the current values of array
{
arr[i] = arr[i] + 10; //add 10 to each value of array
}
System.out.println("\n\nAfter Increment, The Array is :");
for (int i=0; i < len; i ++) //definition of 'for' loop to print the updated values of array
{
System.out.print(arr[i]+"\t");
}
} //method definition ends
public static void main(String args[]) //definition of 'main' methodstarts..
{
int i,n;
Console con=System.console(); //creating an object of console class
System.out.print("\nEnter The Length of Array : ");
n=Integer.parseInt(con.readLine()); //reading the value for no. of elements from console
entered by user
int[] a = new int[n];//initializing the array
System.out.println("\nEnter Array values :");for(i=0;i<n;i++) //defining a 'for' loop to
reading the values from console to be stored in array
{
a[i]=Integer.parseInt(con.readLine()); //reading user input from console
}
Increment(a); // calling the method 'increment()' with the 'array a' as its parameter
} //main method ends

}
Question 2

Greatest common divisor

Calculate the greatest common divisor of two positive numbers a and b.

gcd(a,b) is recursively defined as

gcd(a,b) = a if a =b

gcd(a,b) = gcd(a-b, b) if a >b

gcd(a,b) = gcd(a, b-a) if b > a

import java.io.*;

import java.util.Scanner;

class Main

public static void main(String args[])

Scanner sr= new Scanner(System.in);

System.out.println("Enter The number a");

int a=sr.nextInt();

System.out.println("Enter The number b");

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);

public static int findgcd(int c,int d)

if (d == 0)

return c;

return findgcd(d, c % d);

I/O:

Enter The number a

Enter The number b

The greatest common divisor of two positive numbers 4 and 8 is 4

Improve the understandability of the below given code:

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).

Say we have an array unsorted A[0],A[1],A[2]................ A[n-1] and A[n] as


input. Then the following steps are followed by bubble sort algorithm to sort
the values of an array.

1.Compare A[0] and A[1] .


2.If A[0]>A[1] then Swap A[0] and A[1].
3.Take next A[1] and A[2].
4.Comapre these values.
5.If A[1]>A[2] then swap A[1] and A[2]
at last compare A[n-1] and A[n]. If A[n-1]>A[n] then swap A[n-1] and
A[n]. As we see the highest value is reached at nth position. At next iteration
leave nth value. Then apply the same steps repeatedly on
A[0],A[1],A[2]................ A[n-1] elements repeatedly until the values of array
is sorted.

Step by step explanation:

class Problem1 //creating a new class


{
static int[] a; //declaring a new integer variable 'a' of array type to storethe numbers
static int nElems; //declaring an integer variable to hold the value of ''element no.'' in the
array
public static void ArrayBub(int max) //defining a new parameterized method to initialize the
array with 'max' as size of the array
{
a = new int[max];
}
public static void insert(int value) //defining the insert method to insert values in the array.
{
a[nElems] = value; //assigning the value to array at current position
Elems++; //incrementing the position counter
}
public static void Sort() //defining the method to sort the array

{
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'

insert(6); //calling the method 'insert()with 6 as parameter'


insert(1); //calling the method 'insert()with 1 as parameter'
Sort(); // calling the 'sort()' method
System.out.println(a[2]); //printing the current value of array at 3rdposition
swap(2,1); //calling the swap function
System.out.println(a[0]); //printing the current value of array at 1stposition
} //main method ends here
} // class definition ends

You might also like