You are on page 1of 13

BOP ADDIGNMENT 1 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} ANSWER: import java.util.*;
import java.util.*; import java.io.*;

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","Smith","Johnso n","Bill","Williams","Jones","Brown","Davis","Ann","Miller","Wilson","Moore","Taylor", "Anderson","Thomas","Jackson"};

int n=name1.length;

System.out.println("\nThe Total number of Names in 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 found at position :"+(i+1)); count=count+1; }

if(count==0) { System.out.println("The Name is Not Found.\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); } }

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

ANSWER:
import java.util.*; import java.io.*; //importing the required packages

class Problem3 { public static void Increment(int[] arr) method with one integer parameter {

//creating a class

//defining the 'increment()'

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 ++) print the current values of array { System.out.print(arr[i]+"\t"); } //definition of 'for' loop to

for (int i = 0; i < len; i ++) increment the current values of array { arr[i] = arr[i] + 10; }

//definition of 'for' loop to

//add 10 to each value of array

System.out.println("\n\nAfter Increment, The Array is :"); for (int i=0; i < len; i ++) updated values of array { //definition of 'for' loop to print the

System.out.print(arr[i]+"\t"); } } definition ends //method

public static void main(String args[]) starts.. { int i,n; Console con=System.console(); console class

//definition of 'main' method

//creating an obect of

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

for(i=0;i<n;i++) loop to reading the values from console to be stored in array {

//defining a 'for'

a[i]=Integer.parseInt(con.readLine()); //reading user input from console } Increment(a); method 'increment()' with the 'array a' as its parameter } main method ends } closes // calling the // //class

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 ANSWER:

import java.lang.*; import java.math.*; import java.io.*;

public class Gcd {

public static int gcdCalc ( int a, int b ) {

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

public static void main( String args[] ) {

Console con=System.console();

System.out.print("\nEnter First Number : "); int arg1=Integer.parseInt(con.readLine()); System.out.print("\nEnter Second Number : "); int arg2=Integer.parseInt(con.readLine()); int endGcd = gcdCalc( arg1, arg2 );

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; 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; } ANSWER: class Problem1


{ static int[] a; the numbers //declaring a new integer variable 'a' of array type to store

//creating a new class

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) values in the array.. { a[nElems] = value; array at current position nElems++; position counter }

//defining the insert method to insert

//assigning the value to //incrementing the

public static void Sort() the array

//defining the method to sort

{ int out, in; variables 'out' & 'in' for(out=nElems-1; out>1; out--) { for(in=0; in<out; in++) { if( a[in] > a[in+1] ) compare the adjecent values swap(in, in+1); in specified order } } } //conditional statement to //swaping the two values //inner loop // declaring two integer //outer loop

public static void swap(int one, int two) perform swapping of elements { int temp = a[one]; values a[one] = a[two]; a[two] = temp; }

//defining 'swap' function to

//interchanging the

public static void main(String args[]) { ArrayBub(3); as parameter'

//definition of main method

//calling the method 'arraybub() with 3

insert(3); with 3 as parameter' insert(6); with 6 as parameter' insert(1); with 1 as parameter' Sort(); position swap(2,1); position } } //main method ends here // class definition ends

//calling the method 'insert() //calling the method 'insert() //calling the method 'insert() // calling the 'sort()' method

System.out.println(a[2]); //printing the current value of array at 3rd //calling the swap function

System.out.println(a[0]); //printing the current value of array at 1st

You might also like