Você está na página 1de 13

Basics of Programming-Assignment 1

QUESTION 1

a. 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.io.*; import java.util.Scanner; class program1 { 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].equals(name)) count++; } if(count!=0) System.out.println("The Number of occurance of the Name is " + count ); else System.out.println("Word not Found"); } }

output:
C:\Program Files\Java\jdk1.6.0_22\bin>java Program1 Enter a name to check Sam The Number of occurance of the Name is 2

(b) 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.*; class Problem3 {

//importing the required packages

//creating a class

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

{ int len = arr.length; elements in array // initializing a integer variable 'len' with the length, i.e. the no of

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()); entered by user int[] a = new int[n];

//reading the value for no. of elements from console

//initializing the array

System.out.println("\nEnter Array values :");

for(i=0;i<n a="" loop="" to="" reading="" the="" values="" from="" console="" 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

Ans.

Program 2 finding GCD:

import java.io.*;

import java.util.Scanner;

class program2

public static void main(String args[])

Scanner sr= new Scanner(System.in);

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 two positive numbers " + a + " and " + b + " is " + gcd);

public static int findgcd(int c,int d)

if (d == 0)

return c;

return findgcd(d, c % d);

Output

C:\Program Files\Java\jdk1.6.0_22\bin>javac Program2.java

C:\Program Files\Java\jdk1.6.0_22\bin>java program2

Enter The number a

36

Enter The number b

24

The greatest common divisor of two positive numbers 36 and 24 is 12

(b) 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; nElems++; } public static void Sort() { int out, in; // declaring two integer variables 'out' & 'in' //defining the method to sort the array //assigning the value to array at current position

//incrementing the position counter

for(out=nElems-1; out>1; out--) //outer loop { for(in=0; in<out in="" 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]; a[one] = a[two]; a[two] = temp; } public static void main(String args[]) //definition of main method { ArrayBub(3); insert(3); insert(6); insert(1); Sort(); //calling the method 'arraybub() with 3 as parameter' //interchanging the values

//calling the method 'insert() with 3 as parameter' //calling the method 'insert() with 6 as parameter' //calling the method 'insert() with 1 as parameter' // 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

</out> </out> </n> </names.length>

Você também pode gostar