Professional Documents
Culture Documents
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}
Code:
import java.io.*;
class Main
{
public static void main(String args[])
{
String 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"};
System.out.println("Enter the name you want to find");
int count=0;
try
{
java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader
(System.in));
String name=r.readLine();
for(int i=0;i<names.length;i++)
{
if(names[i].equalsIgnoreCase(name))
count++;
}
}
catch(Exception e){
System.out.print("Error occurred"+e);
}
if(count!=0)
System.out.println("The Number of times name is found = " + count );
else
System.out.println("name unavailable");
}
}
Question 1
Part 2
The code below takes an array of 10 integer values as an argument,prints its values,increments it by 10 and
again prints the incremented value
import java.util.*;
//importing the required packages
class problem3
//defining a class
{
System.out.println(integerArray[i]);
System.out.println(integerArray[i]);
Question 2
Part 1
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
Solution :
import java.io.*;
class findgcd
{
int gcd(int a,int b)
{
if(a==b)
return a;
else if(a>b)
return(gcd(a-b,b));
else
return(gcd(a,b-a));
}
}
class gcdtest1
{
Question 2
Part 2
Improve the understandability of the below given code:
The code explains BUBBLE SORT implentation.
class Problem1
//defining a class
{
int nElems; //declaring an integer variable 'nElems' to hold no of elements in the array
a = new int[max];
public void insert(int value) //defining the insert method to insert values in the array
{
int out, in; // declaring two integer variables 'out' & 'in'
if( a[in] > a[in+1] ) //conditional statement to compare the adjacent values
swap(in, in+1); //swaping the two values by calling the 'swap()' function
public void swap(int one, int two) //defining 'swap' function to perform swapping of elements
{
//interchanging the values
a[one] = a[two];
a[two] = temp;
}
// class definition ends