Vous êtes sur la page 1sur 7

Problem

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}

Solution
import java.io.*; import java.util.Scanner; class ListSearch { 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"}; Scanner sr= new Scanner(System.in); System.out.println("Enter a name to check"); String name=sr.nextLine(); int count=0; for(int i=0;i<names.length;i++) { if(names[i].equalsIgnoreCase(name)) count++; } if(count!=0) System.out.println("The number of occurences of " + name + " in given array is " + count); else System.out.println("Word not Found"); }

Output:
Enter a name to check Anne Word not found Enter a name to check Sam The number of occurrences of Sam in given array is 2

Problem
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]); } }
}

Solution
The class program3 has as a data member an integer array of 10 elements. The static method in the class takes an integer array as its parameter. It first prints the elements of the array, increments each array element by 10 and finally prints the incremented array elements. To increase the understandability of the code I am going to rewrite the code with a number of comment lines and proper indentation. import java.util.*; //Import the java.util package class problem3 //Class declaration { //Allocating memory of 10 integers to array named numArray int[] numArray = new int[10]; //define 'incrementElements()' method with array 'integerArray' as the parameter public static void incrementElements (int[] integerArray) { //Store the length of the array in variable arraylen int arraylen = integerArray.length; //for loop to print all the element of the array passed as argument for (int i = 0; i < arraylen; i ++) { System.out.println(integerArray[i]); } //for loop to increment all the element of the array by 10 for (int i = 0; i < arraylen; i ++) { integerArray[i] = integerArray[i] + 10; //Increments each element by 10 } //for loop to print the incremented element of the array for (int i=0; i < arraylen; i ++) { System.out.println(integerArray[i]); } }

Problem
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.*; import java.util.Scanner; 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 ProbGCD { public static void main(String arr[]) throws IOException { Scanner sr=new Scanner(System.in); System.out.println("Enter first number"); int a=sr.nextInt(); System.out.println("Enter second number"); int b= sr.nextInt(); FindGCD g=new FindGCD(); System.out.println("Gcd of " + a + " and " + b + " is "+g.gcd(a,b)); } }

Output:

Enter first number 12 Enter second number 18 Gcd of 12 and 18 is 6

Problem
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; } }

Solution
The code implements bubble sort. To increase the understandability of the code I am going to rewrite the code with a number of comment lines and proper indentation. class Problem1 //Class declaration { int[] a; //Declare an integer array data member a to store the numbers int nElems; //Declares an integer data member nElems to hold the index of array //Declare a method to initialize the array with its size passed as argument public ArrayBub(int max) { a = new int[max]; //Allocate array a memory to hold max no. of integers } //Declare a method to insert values into the array with the value passed as argument public void insert(int value) { a[nElems] = value; //assign value to the array element nElems++; //increments array index so new value is assigned to next element } //Declare a method to bubble sort the array public void Sort() { //Declare two variables to hold the outer and inner loop limits int out, in; //Outer loop that makes nElemns-1 passes over the array for(out=nElems-1; out>1; out--) /*Inner loop that sorts the element over every pass such that at the end of each pass the largest number is in the last element of the pass i.e. in a[out] element*/

for(in=0; in<out; in++) if( a[in] > a[in+1] ) //condition to compare the adjacent values swap(in, in+1); //swapping two elements by calling swap() } /* Declare a method to swap two array elements with arguments as indices of the elements in the array */ public void swap(int one, int two) { long temp = a[one]; a[one] = a[two]; a[two] = temp; } }

Vous aimerez peut-être aussi