You are on page 1of 9

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, Ayan, Dev, Kity, Meery, Smith, Johnson, Bill, Williams, Jones, Brown, Davis, Miller, Wilson, Moore, Taylor, Anderson, Thomas, Jackson}

import java.util.*; import java.util.*; import*; class arrayofname { public static void array(String name) { int i,count=0; String[] name1={"Dave", "Ann", "George", "Sam","Ted","Gag","Saj","Agati","Mary","Sam","Ayan","Dev","Kity","Meery","S mith","Johnson","Bill","Williams","Jones","Brown","Davis","Ann","Miller","Wilso n","Moore","Taylor","Anderson","Thomas","Jackson"}; int n=name1.length; System.out.println("\nThe Total number of Namesin the System is :"+n+"\n"); System.out.println("You Searched for :"+name+" \n"); for(i=0;i<n;i++)

{ if(name1[i].equals(name)) { System.out.println("The name is foundat position :"+(i+1)); count=count+1; } } if(count==0) { System.out.println("The Name is NotFound.\n"); } else { System.out.println("\nThe Name Apeared for"+count+" times."); } }

public static void main(String args[]) { Console con=System.console(); System.out.print("\nEnter a Name to Search(Case-sensitive): "); String name=con.readLine(); array(name); } } /* END OF PROGRAM*/

Improve the understandability of the below given code:

import java.util.*; class problem3 { int[] numArray = new int[10]; public static void incrementElements (int[] integerArray) { int arraylen = integerArray.length; for (int i = 0; i < arraylen; i ++) { System.out.println(integerArray[i]); } for (int i = 0; i < arraylen; i ++) { integerArray[i] = integerArray[i] + 10; } for (int i=0; i < arraylen; i ++) { System.out.println(integerArray[i]); } }

import java.util.*; packages import*; //importing the required

class Problem3 {

//creating a class

public static void Increment(int[] arr) with one { int len = arr.length; length, i.e. the no

//defining the 'increment()'method integer parameter

// initializing ainteger variable 'len' with the of elements in array

System.out.println("\nBefore Increment, The Array was :"); for (int i = 0; i < len; i ++) values of array { System.out.print(arr[i]+"\t"); } for (int i = 0; i < len; i ++) current values of array{ arr[i] = arr[i] + 10; } System.out.println("\n\nAfter Increment, The Array is :"); for (int i=0; i < len; i ++) theupdated values of array { System.out.print(arr[i]+"\t");} } //meth od definition ends public static void main(String args[]) 'main' methodstarts..{ int i,n; Console con=System.console(); //creating an obect of console //definition of //definition of 'for' loop to print //definition of 'for' loop toincrement the //add 10 to each value of array //definition of 'for' loop toprint the current

class System.out.print("\nEnter The Length of Array : "); n=Integer.parseInt(con.readLine()); elements from c console entered by user int[] a = new int[n]; //reading thevalue for no. of

//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()); fromconsole } Increment(a); 'increment()' with the 'array a' parameter } } /* END OF PROGRAM*/ // calling themethod as its //main method ends //classcloses //reading user input

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*; import java.util.Scanner; class program2

{ public static void main(String args[]) { Scanner sr= new Scanner(; 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 numbers " + a + " and " + b + " is " + gcd); } public static int findgcd(int c,int d) { if (d == 0) return c; return findgcd(d, c % d); } } /* END OF PROGRAM*/

Improve the understandability of the below given code:

class Problem1 { int[] a; int nElems; public ArrayBub(int max) { a = new int[max]; } public void insert(int value) {

a[nElems] = value; nElems++; } public void Sort() { int out, in; for(out=nElems-1; out>1; out--) for(in=0; in<out; in++) if( a[in] > a[in+1] ) swap(in, in+1); } public void swap(int one, int two) { long temp = a[one]; a[one] = a[two]; a[two] = temp; }

class Problem1 { static int[] a; variable 'a' of array type numbers static int nElems; variable to hold the value of ''element no.'' in the array public static void ArrayBub(int max) methodto initialize size of the array { a = new int[max]; } public static void insert(int value) insertvalues in the array.. { //creating a new class //declaring a new integer to storethe //declaring an integer //defining a new parameterised the array with 'max' as

//defining the insert method to

a[nElems] = value; at current position nElems++; counter } public static void Sort() array

//assigning the value toarray //incrementing theposition //defining the method to sortthe

{ int out, in; // declaring two integervariables '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 tocompare the adjecent values swap(in, in+1); //swaping the two valuesin specified order } } } public static void swap(int one, int two) //defining 'swap' function toperform swapping of elements {int temp = a[one]; //interchanging thevalues a[one] = a[two]; a[two] = temp; } public static void main(String args[]) //definition of main method { ArrayBub(3); //calling the method 'arraybub() with 3 as 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 end /* END OF PROGRAM*/