Tuesday, November 1, 2011

TCS Pre ILP Assignment1- Solutions

Question 1

Question 1

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

Program:

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

{

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");

}

}

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]);

}

}

CODE DESCRIPTION:

The code defines that when any element of the integer array is occurred then it is printed first and after that it's incremented by 10 and then the resultant array is printed, the same code is being defined in the component classes of java.util.*-The Component class is the abstract super class of the nonmenu-related Abstract Window Toolkit components. Class Component can also be extended directly to create a lightweight component. A lightweight component is a component that is not associated with a native opaque window. The code is predefined in the package and when ever we want to execute any application which wants the code...den without writing the whole code we can directly import the package and execute that application. Here if we explain the code step by step, we can see that an array named numarray is assigned containing 10 elements, which defines the size of the array;

int num[]array=new int[10];

Next the array length is calculated using integerArray.length, and next using for loop we set a loop from 0 to the obtained length of the array

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

Next the present integer value is incremented, by a value of 10 and then the control passes to the next element in the array.

next we have the display the resulted array using the final code:

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

{

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

}

No comments:

Post a Comment