Anda di halaman 1dari 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","Johnson","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.*; //importing the required packages

import java.io.*;

class Problem3 //creating a class

public static void Increment(int[] arr) //defining the 'increment()' method with one
integer parameter

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 ++) //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

arr[i] = arr[i] + 10; //add 10 to each value of array

System.out.println("\n\nAfter Increment, The Array is :");

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

public static void main(String args[]) //definition of 'main' method starts..

int i,n;

Console con=System.console(); //creating an obect of console class

System.out.print("\nEnter The Length of Array : ");

n=Integer.parseInt(con.readLine()); //reading the value for


no. of elements from console entered by user

int[] a = new int[n]; //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()); //reading user input from console

Increment(a); // calling the method


'increment()' with the 'array a' as its parameter

} // main
method ends

} //class
closes
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 //creating a new class

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..

a[nElems] = value; //assigning the value to array at


current position

nElems++; //incrementing the


position counter

public static void Sort() //defining the method to sort the array
{

int out, in; // declaring two integer


variables '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 to compare the


adjecent values

swap(in, in+1); //swaping the two values in specified


order

public static void swap(int one, int two) //defining 'swap' function to perform swapping of
elements

int temp = a[one]; //interchanging the


values

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 3rd position

swap(2,1); //calling the swap function

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

} //main method ends here

} // class definition ends

Anda mungkin juga menyukai