8
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”} Code: import java.io.*; class Main { 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"}; System .out.println("Enter the name you want to find"); int count=0; try { java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System .in)); String name=r.readLine(); for(int i=0;i<names.length;i++) { if(names[i].equalsIgnoreCase(name)) count++; } }

Basics of Programming

Embed Size (px)

DESCRIPTION

Basics of programming concept in a brief way.

Citation preview

Page 1: Basics of Programming

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”}

Code:

import java.io.*;class Main{        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"};                System.out.println("Enter the name you want to find");                        int count=0;                try                {                        java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));                        String name=r.readLine();                                        for(int i=0;i<names.length;i++)                        {                                if(names[i].equalsIgnoreCase(name))                                count++;                        }                }                catch(Exception e){                    System.out.print("Error occurred"+e);                }                if(count!=0)                System.out.println("The Number of times name is found = " + count );                else                System.out.println("name unavailable");        } }

Page 2: Basics of Programming

Question 1

Part 2

Improve the understandability of the below given code:

The code below takes an array of 10 integer values as an argument,prints its values,increments it by 10 and again prints the incremented value

import java.util.*;

//importing the required packages

class problem3

//defining a class

{

int[] numArray = new int[10];

// allocating memory to 10 integer elements in an array named 'numArray'

public static void incrementElements (int[] integerArray)

//a static method that takes an interger array as an input and return none.

{

int arraylen = integerArray.length;

// initializing an integer variable 'arraylen' with the length, i.e. the no of elements in array

Page 3: Basics of Programming

for (int i = 0; i < arraylen; i ++)

//definition of 'for' loop to print the current values of array

{

System.out.println(integerArray[i]);

}// end of 'for' loop

for (int i = 0; i < arraylen; i ++)

//definition of 'for' loop to increment the current values of array

{

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

}// end of 'for' loop

for (int i=0; i < arraylen; i ++)

//definition of 'for' loop to print the updated values of array

{

System.out.println(integerArray[i]);

}//end 'for' loop

} //end of class definition

Page 4: Basics of Programming

Question 2

Part 1

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

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 gcdtest1

Page 5: Basics of Programming

{

public static void main(String arr[]) throws IOException

{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter a::");

int a=Integer.parseInt(br.readLine());

System.out.println("Enter b::");

int b=Integer.parseInt(br.readLine());

findgcd g=new findgcd();

System.out.println("Gcd of “+a+ “and b is::"+g.gcd(a,b));

}

}

Question 2

Part 2

Improve the understandability of the below given code:

The code explains BUBBLE SORT implentation.

class Problem1

//defining a class

{

int[] a; //initialize a int array.

int nElems; //declaring an integer variable 'nElems' to hold no of elements in the array

public ArrayBub(int max)

//defining a default construct with an intger type input parameter

Page 6: Basics of Programming

{

a = new int[max];

}

public 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 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--) //beginning of outer loop

for(in=0; in<out; in++) //beginning of inner loop

if( a[in] > a[in+1] ) //conditional statement to compare the adjacent values

swap(in, in+1); //swaping the two values by calling the 'swap()' function

Page 7: Basics of Programming

}

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

{

//interchanging the values

long temp = a[one];

a[one] = a[two];

a[two] = temp;

}

}

// class definition ends