Question 1
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”,
“Jackson”}
ANSWER:
import java.util.*;
import java.util.*;
import java.io.*;
class arrayofname
int i,count=0;
int n=name1.length;
System.out.println("\nThe Total number of Names in the
System is : "+n+"\n");
for(i=0;i<n;i++)
if(name1[i].equals(name))
count=count+1;
if(count==0)
else
Console con=System.console();
String name=con.readLine();
array(name);
}
Improve the understandability of the below given code:
import java.util.*;
class problem3
System.out.println(integerArray[i]);
System.out.println(integerArray[i]);
}
ANSWER:
import java.io.*;
public static void Increment(int[] arr) //defining the 'increment()' method with one
integer parameter
for (int i = 0; i < len; i ++) //definition of 'for' loop to print the
current values of array
System.out.print(arr[i]+"\t");
for (int i = 0; i < len; i ++) //definition of 'for' loop to increment the
current values of array
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
int i,n;
} // main
method ends
} //class
closes
Question 2
gcd(a,b) = a if a =b
ANSWER:
import java.lang.*;
import java.math.*;
import java.io.*;
int gcd=0;
if( a == b)
gcd = a;
}
else
while ( a != b)
if( a > b )
a = a - b;
else
b = b - a;
gcd=a;
return( gcd );
{
Console con=System.console();
int arg1=Integer.parseInt(con.readLine());
int arg2=Integer.parseInt(con.readLine());
System.out.println( "\nThe GCD for Integers " + arg1 + " and " + arg2 + " is : " + endGcd );
}
Improve the understandability of the below given code:
class Problem1
int[] a;
int nElems;
a = new int[max];
a[nElems] = value;
nElems++;
swap(in, in+1); }
{
long temp = a[one];
a[one] = a[two];
a[two] = temp;
ANSWER:
static int[] a; //declaring a new integer variable 'a' of array type to store the 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 parameterised 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..
public static void Sort() //defining the method to sort the array
{
public static void swap(int one, int two) //defining 'swap' function to perform swapping of
elements
a[one] = a[two];
a[two] = temp;