TCS Aspire Basics of Programming Assignment Solutions
TCS Aspire Basics of Programming Assignment Solutions
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.util.*; import java.io.*; class name_search //class to search the names in the given input { String[] input;//string array to store the input int length=0; public void ArrayBub(int MAX)//method to define the size of array { input = new String[MAX]; } public void insert(String s)//method to insert the string into the array { input[length]=s; length++; } public int search(String key)//method to search the name based on the key provided { int i=0; int count=0; for(i=0;i<length;i++) { if(input[i].equals(key)) count++; } return count; } public void display()//method to display the contents of array { int i; System.out.println(); System.out.println("The input names are:"); System.out.println();
for(i=0;i<input.length;i++) System.out.println(input[i]); System.out.println(); } } class demo_search//class to demonstrate the search { public static void main(String args[]) throws IOException { String key="null",value; int i,MAX,count; name_search l = new name_search();//creating the instance of the class name_search System.out.println("Enter the number of names you want to enter"); Scanner in= new Scanner(System.in); MAX=in.nextInt();//to scant the input from Standard input file and stores it in MAX l.ArrayBub(MAX); System.out.println("Enter the names"); for(i=0;i<MAX;i++) { //method to read the stream of input from the standard input BufferedReader b= new BufferedReader(new InputStreamReader(System.in)); value=b.readLine(); l.insert(value); } l.display(); System.out.println("Enter a name to search in the gien names:"); System.out.println(); //method to read the stream of input from the standard input BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { key= br.readLine(); }catch(Exception e){} count=l.search(key); System.out.println(); if(count>0) System.out.println("The given name "+key+" appears "+count+" times"); else System.out.println("Name "+key+" doesnot found"); }
Improve the understandability of the below given code: //Program to display the content of array and increment the each element by 10 and display the new values. import java.util.*; //importing the java package util that contains the assortment of classes and interfaces that support a broad range of functionality, like generate pseudorandom numbers, manage date and time, observe events, manipulate sets of bits, tokenize strings, and handle formatted data class problem3 //class definition { int[] integerArray = new int[10]; //declaring the integer array with size 10 public static void incrementElements () //method to increment the array element { int arraylen = integerArray.length; //to display the content of array for (int i = 0; i < arraylen; i ++) { System.out.println(integerArray[i]); } //to increment each element of the array by 10 for (int i = 0; i < arraylen; i ++) { integerArray[i] = integerArray[i] + 10; } //to display the new contents of the array for (int i=0; i < arraylen; i ++) { 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.util.*; class GCD { int gcd(int a,int b) { int r; if(a==b) r=a; else if(a>b) r=gcd(a-b,b); else r=gcd(a,b-a); return r; } } class demo_gcd { public static void main(String args[]) { int a,b; System.out.println(); System.out.println("Enter the two positive integer numbers a and b"); Scanner in = new Scanner(System.in); System.out.println("Enter the positive integer number a"); a=in.nextInt(); while(a<0) { System.out.println("Enter only the Positive integer for a"); a=in.nextInt(); } System.out.println("Enter the positive integer number b"); b=in.nextInt(); while(b<0)
{ System.out.println("Enter only the Positive integer for b"); b=in.nextInt(); } in.close(); GCD g=new GCD(); System.out.println("GCD of two numbers "+a+" and "+b+" is "+g.gcd(a,b)); } } Improve the understandability of the below given code: //The program to sort the array by defining the method to assign the size, to insert the array element. class Problem1 { int[] a; //intereg array int nElems; public ArrayBub(int max) { a = new int[max]; //assigning the size of the array } public void insert(int value) //inserting the array elements { a[nElems] = value; nElems++; } public void Sort() //sorting the array contents { 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); } //swap function to exchange the contents using 3rd variavle temp public void swap(int one, int two) { int temp = a[one]; a[one] = a[two]; a[two] = temp;
} }