Saturday 5 November 2011

Basics of programming - Aspire Assignment


My friend was preparing for his aspire assignments , he informed me that this materials would help many students.Thanks to my friend and ilp community.This material i hope will inculcate a good idea for creating your own program.The material provided is only for idea gathering purpose only.Please dont try to mimic the programs,because  i believe you can write even more efficient programs.


QUESTION 1:NAME FINDING PROGRAM


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



import java.io.*;
import java.util.StringTokenizer;
public class namefinding
{
 public static void main(String args[])throws Exception
{
 InputStreamReader isr=new InputStreamReader(System.in);
 BufferedReader br=new BufferedReader(isr);
System.out.println(".........................WELCOME TO THE NAME FINDING PROGRAM.......................");
System.out.println("enter names by using space inbetween:");
String arrNames=br.readLine();
StringTokenizer st1=new StringTokenizer(arrNames);
System.out.println("enter the name to be searched:");
String name=br.readLine();
int countNoOccurance=0;
while(st1.hasMoreTokens())
{
if(name.equalsIgnoreCase(st1.nextToken()))
{
countNoOccurance++;
}
}
System.out.println("THE NAME "+name+" HAS APPEARED "+countNoOccurance+" TIMES");
}
}



IMPROVE UNDERSTANDABILITY OF THE BELOW GIVEN CODE:


import java.util.*;

class problem3
{

int[] numArray=new int[10];  //an array named numarray is assigned containing 10 elements

public static void incrementElements (int[] integerArray) //the function is static it can be activated either by using class name or by an object. Passing array of integers as an argument

{
int arraylen = integerArray.length;//the array length is calculated using integerArray.length

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

{

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

}

for (int i = 0; i < arraylen; i ++)//Using for loop we set a loop from 0 to the obtained length of the array

{

integerArray[i] = integerArray[i] + 10;//The present integer value is incremented, by a value of 10

}

for (int i=0; i < arraylen; i ++) // result is displayed using an array


{


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


}


}







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


import java.io.*;
import java.util.Scanner;
class gcdfinder
{
 public static void main(String args[])
   {
Scanner sr= new Scanner(System.in);
System.out.println("................welcome to the gcd finder program...................................");
     System.out.println("Enter the value for a");
     int a=sr.nextInt();
  System.out.println("Enter the value for 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 numbers " + a + " and " + b + " is " + gcd);
System.out.println("Thanks for executing the program");
    }
 public static int findgcd(int c,int d)
  {
  if (d == 0)
     return c;
  return findgcd(d, c % d);
  }
  }




IMPROVE UNDERSTANDABILITY:



class Problem1 //class name is 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() //sorting is done like bubble sort algorithm

{

int out, in;

for(out=nElems-1; out>1; out--)

for(in=0; in<out; in++)

if( a[in] > a[in+1] ) // comparing  one number with another.

swap(in, in+1); }

public void swap(int one, int two) //swapping takes place

{

long temp = a[one];

a[one] = a[two];

a[two] = temp;

}


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...