Saturday 5 May 2012

BASICS OF PROGRAMMING ASSIGNMENT 1


Question 1a
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”}

SOLUTION:
import java.io.*;
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”};
System.out.println("Enter a name to check");
InputStreamReader Inp=new InputStreamReader(System.in);
BufferedReader Br=new BufferedReader(Inp);
String name=Br.readLine();
 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>javac program1.java
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


Question 1 b
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 
       //creating a class
{
int[] numArray = new int[10];
       // allocating memory to 10 integer elements in an array named 'numArray'
public static void incrementElements (int[] integerArray) 
      //defining the 'incrementElements()' method with one integer type array 'integerArray' as the parameter
{
int arraylen = integerArray.length;
      // initializing an integer variable 'arraylen' with the length, i.e. the no of elements in array
for (int i = 0; i < arraylen; i ++)
      //definition of 'for' loop to print the current values of array
{
System.out.println(integerArray[i]);
}
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
}
for (int i=0; i < arraylen; i ++)
      //definition of 'for' loop to print the updated values of array
{
System.out.println(integerArray[i]);
}
}  //class closes


Question 2a
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
  {
 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));
}
  }

Output:
C:\Program Files\Java\jdk1.6.0_22\bin>javac gcdtest1.java
C:\Program Files\Java\jdk1.6.0_22\bin>java gcdtest1
 Enter a::
 36
 Enter b::
 24
 Gcd of a and b is:: 12


Question 2b
Improve the understandability of the below given code:
The code explains BUBBLE SORT implentation.
class Problem1
     //creating a new class
{
int[] a; //declaring a new array 'a' of integer type to store the numbers
int nElems; //declaring an integer variable 'nElems' to hold no of elements in the array
public ArrayBub(int max) 
       //defining a new parameterised method to initialize the array with 'max' as size of the array
  {
   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--) //outer loop
for(in=0; in<out; in++) //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
}
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

3 comments:

Unknown said...

Lolz u can get fired for this :P Great stuff anywayz ;) Thank u on behalf of lot of people ;)

admin said...

:-):-):-)

Anonymous said...

Thank yo so much!