Exercise 5: Sorting 1.

Merge Sort Combine 3 sorted lists into 1 sorted list and remove the duplicate elements in the resultant array. 2. Student Rank Find the marks for a rank from a series of marks. 3. Roll number from Rank Find the roll numbers for a rank from a given series of roll numbers and marks. 4. Sorting Fruits Finding the total quantity of each kind of fruit(represented by an integer number) sold from an ordered list containing fruits and the quantity sold. 5. Word Length Accept a sentence ad display the number of occurrences of words of different lengths 6. Palindrome Determine whether a string is a palindrome or not. Display the output as yes or no. 7. Purchase List I have a list of items on my purchase list. I then get an updated copy of the purchase list after some revisions have been done by my room mate. I need to find out what got added, updated and deleted and print the result appropriately. 8. Shorter and Taller Students In a class of n students, find the taller half of students and arrange them in the ascending order of their height and the shorter half in the descending order of their height.

Extension Find the person and list of his subordinates from a list of employee id and manager id pairs. 4. Printing Backwards Write a program that reverses a sentence. the tax is 0%. The bank pays a flat 4% for deposits of up to $1. Like interest-rate. .000. it consumes a deposit amount. it produces the actual amount of interest that the money earns in a year. Maximum of 3 numbers Find the maximum of three numbers 2. For a gross pay of $240 or less. Also validate the entries for a cyclic relation.5. 3. Exercise 3: Conditions 1.5% per year for deposits of up to $5. Employee Hierarchy . 000. which consumes the gross pay and produces the amount of tax owed. Employee Hierarchy Find the person and list of his subordinates from a list of employee id and manager id pairs represented in a two dimensional array. and a flat 5% for deposits of more than Rs. Vowels Write a software to check whether the alphabet entered by the user is a vowel or not.9. 11. Tax calculation Develop the program tax. Instead of the rate. Print the hierarchy with names of the employees. Interest Develop the program interest.000. a flat 4. 10.

Library software A library software has 5 functions that can be invoked from the . .50% for the next Rs1000 (that is. 400 a year receives Rs.25 ‡ 1/100 ‡ 500 for the first Rs.0% for everything above Rs2500. 900. Also develop netpay.25% for the first Rs500 of charges. 6. The net pay is the gross pay minus the tax. and for any pay over $480.50 ‡ 1/100 ‡ 900 = 4.1.50 for the next Rs. which is 0.25 = 0. 4. Define the program pay-back. 2. and 1. Pay back Some credit card companies pay back a small portion of the charges a customer makes over a year. 480 or less.12. . 500 and 0. 5.75. 3. and one who charges Rs1. 240 and Rs. which consumes a charge amount and computes the corresponding pay-back amount. the portion between Rs1500 and Rs2500). the tax rate is 15%. Assume the hourly pay rate is Rs. Remember to develop auxiliary programs when a definition becomes too large or too complex to manage.for over Rs. the portion between Rs500 and Rs1500). One company returns 1.75% for the next Rs1000 (that is. Determine by hand the pay-backs for a customer who charged Rs.00. . 2. Thus. The program determines the net pay of an employee from the number of hours worked. 5. the tax rate is 28%. a customer who charges Rs. which is 1. 400 a year receives Rs.25 ‡ 1/100 ‡ 400.000 and one who charged Rs2600.

return ³Magazine addition function opened´) when the function is invoked by the user.31).main module (named Library) using the code given in brackets 1. Book section addition -( S ) Return a message . month (1. etc. with 0 as sunday. Book Issue . Secondly.. for details). you divide this number by 7 with integer remainder: this now is the day of the week..g if 'J' is given. To calculate the total number of days you have to implement the following steps:Subtract 1900 from the given year and multiply the result by 365 Add the missing leaps years by adding (year . you have to subtract 1 from the previous result. you have to calculate the total number of days from 1900/1/1 to the given date (see below. The day of the week of dates between March 1900 and February 2100 can be calculated as follows: First.(I) 5. Book addition -( B ) 3.1900) / 4. Now add the all days of the year up to the given one to the result (in case of February always 28. Member Registration ( M ) 2.12) and year.function opened´ (e. Day of the week Implement a program that returns the day of the week for a given day (1. 7. Magazine addition -(J) 4. because the additional day for a leap year . If the year itself is a leap year and the month is January or February. 1 as monday.³<module name> .

2007/2/21. 5.already have been added). 4. 2007/4/8. Metropolis or city Let three variables be given containing information for a city: boolean isCapitalCity. 2. Count number of words Find the number of occurrences of given word in a sentence. 2010/4/4 Ash Wednesday: 2006/3/1. with the following meaning: isCapitalCity is true if and only if the city is a capital city.if it is either a capital city with more than 100 000 citizens or a normal city with more than 200 000 citizens and an average income of 720 000 000 per year. Count of most occurring digit Find the digit that occurs most in a given number. numberOfCitizens is the number of citizens in this city. int numberOfCitizens. taxPerCitizen is the average tax per month a citizen of the city pays. Return a boolean expression ± true if the city is a metropolis. Exercise Set 4: Iterations 1. Occurrences of a particular digit Find the number of occurrences of digit 1 in a number. We define a city to be a Metropolis . 2010/2/17 8. Here some dates with the day of the week for testing your Java program: Easter Sunday: 1916/3/23. 3. Count each word in a given problem . Count of each digit Find the number of occurrences of each digit in a number. double taxPerCitizen.

b) = a if a =b gcd(a. b-a) if b > a Exercise 5: Search . Find the sequence based on a condition Find the sequence based on an input number as below if the number is even : the next number = number / 2 if the number is odd : the next number = (number * 3) + 1 display a sentence that says how many numbers there are in the sequence. 8. b) if a >b gcd(a. Tribonacci Series Find the nth term in Tribonacci series (0. eg. Mathematical Expression Find the answer for the expression: f(n)=sqr((n-1))+1 for a given n.1. gcd(a.b) = gcd(a. 9. 11. Greatest common divisor Calculate the greatest common divisor of two positive numbers a and b. Use both recursive and iterative approach 7. input = 17.Find the number of occurrences of each word in a sentence. Ascending alphabet check problem Find whether the alphabets in a word (string) are in the ascending order.2.b) = gcd(a-b.b) is recursively defined as gcd(a. output = 17 52 26 13 40 20 10 5 16 8 4 2 1 10.13««). Fibonacci terms Find the sum of all the even-valued terms and numbers divisible by three in the Fibonacci sequence which do not exceed n.

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. Science. ³Jones´. 2. 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. ³Wilson´. 3. ³Davis´. ³Bill´. Grade calculation Write a program to store the marks in 4 subjects(English. ³Saj´. ³Sam´. Search an alphabet Write a program to accept an array of alphabets and an alphabet and check whether the alphabet is present in the array. ³Taylor. ³Miller´. Return the count of occurrence. ³Williams´. ³Smith´.1. Use the following array as input {³Dave´. ³Jackson´} Exercise 6: Arrays 1. ³Ann´. ³Thomas´. ³Kity´. ³Ayan´. Maths. ³Johnson´. ³Meery´. ³Gag´. Return ³Yes´ if the name is present in the array. Return ³Yes´ if alphabet is present in the array. ³Anderson´. ³Ted´. ³Sam´. ³Mary´. ³Brown´. . ³George´. ³Dev´. ³Moore´. ³Agati´.

<marks for Social>. The program should have two functions to To calculate the average marks for each subject To calculate the total marks for each student. <marks for Science>. Assume arrays of marks for each student in the predefined subject order(e. Stack Implementation ± Chocolate box problem You have a set chocolates of different makes (KitKat. Arts) of 5 students. Only one chocolate can be placed inside the box at one instance and only one can be retrieved at any instance. Write a program to insert a set of chocolates in the order in which is it received (random) into a box (that can hold 10 chocolates) and retrieve specific chocolates from the box. 3.<marks for Maths>. return -1 4.Cadbury. already exist´.Social. There is one box where all the chocolates need to be placed. The chocolate which is placed last will be at the top of the box while the chocolate which is place first will be at the bottom of the box. If the string is already present in the array. Toblerone). return a message saying ³<String>. Find second largest number Write a program to find the second largest number in an array of unsorted numbers. If there is no second largest number.g { <marks for English>.<marks for Arts>} 2. Insert Strings Write a program to insert Strings to an array of size 10. After .

µreading¶. for any insert action.. 9.and <task 4> .. 7. 6. 2. Exercise 7: Common Problems 1.´ (tasks listed in ascending order). µwriting¶«.) and return the string ³My favourite tasks are <task1>. Replace strings Write a program to replace an array of alphabets (A ± J) in upper case with corresponding lower case alphabets. 8. <task2>. Favorite tasks Write a program to store the names of favourite tasks (say µpainting¶.g ³$´) on every 3 rd position in an array of strings. 5. return a message ³No more Strings can be stored´. Introduce a delimiter Write a program to insert a delimter (e. <task3>. Find occurrence of an integer Write a program to find the count of a particular value in a array of integers. Replace a string Write a program to replace a particular string if it is there in an array of strings. Concatenate a string Write a program to concatenate an array of string into a single string and return the string.the 10th element.«. Array addition Write a program to accept two arrays of alphabets and combine the .

³append´. the extra participants are distributed among those batches with a lower . create the result array with {<1st element in 1st array>. Batch strength distribution Write a program to accept an array of count of participants in batches and a reference count so that the if any batch has additional participants. mark4 is 20% Weightage of mark5 is 10% Maximum marks is 100 for each subject. ³common´ If ³merge´ is given. <1st element in second array>. 3.<2nd element in first array>. the result array appending second array to the end of first array If ³common´ is given. mark2 is 25% Weightage of mark3. Batch grade Write a program to calculate the exam pass percentage of a batch consisting of 4 students (each having 5 marks entered in order) based on the following rule.<last element in last array>} If ³append´ is given.arrays as per the input flag ± ³merge´.<2nd element in second array>««. Batch grade is A if. Weigtage of mark1. the result array should only have elements common in both arrays.<last element in first array>.. batch percentage is above 80% and B if between 40% and 80% and C if less than 40% If a student didn¶t appear for an exam(that is marks = 0). then his other marks should not be considered for the batch grade calculation 4.

A return message with defiecent(-) /surplus(+) participant count should be returned e. ³Household´. Eg. The program should return an array of unallocated room numbers The room number is a 3 digit number(e. 5. Room allocation Write a program to accept three arrays ± Room numbers. 30. Cosmetic items have a tax of 12%. product quantity and product price and return the total amount required for procurement.g Input can be { 10. The program should ensure to allocate the rooms in lower floors first.ladies and familes(which consists of a male and a female). Coins required Write a program to accept an array of count of coins of denomination 10. .10. Product Type(³Cosmetic´. 20} and value 30.30. 7. a male and female should not be allocated to a room. Unless a family.20} and the -10 is returned.5} 6. room capacity and an array of size 3 representing number of gents. Items procurement Write a program to accept three arrays. Accept {5. The result array can be {30. 300 will return {0.5}.2.g 123) and ³1´ represents the floor number. 50.30. ³Medicinal´ ). for Household 4% and no tax for medicinal goods.value(so as to make the other close to the reference value). 45. 15. The program will find the least number of coins required to satisfy this amount and return the array.25 and 50 respectively and an amount(in paise).

60 M / F SelfEmployed / Professional > 50000 40000 Self Employed 35000 ± Age * 100 > 60 M/F Retired > 25000 25000 ± Age * 100 The loan selection program should accept the age.40 M 25000 F SelfEmployed / Professional > 40000 30000 41 . Shopping List Write a program to accept item list from 3 users and prepare an array with items and total count (store it as string instead of integer). jobstatus and assets and return the eligible loan amount. The result array can have the format . Loan selection Write a program to automate the following loan policy. gender. 9.8. Age category Gender Profession Personal assets Loan amount eligible 16 -25 M /F SelfEmployed >25000 10000 Professional 15000 26 .

in. in++) if( a[in] > a[in+1] ) swap(in. <item1 count>.<item2 count>. int two) { long temp = a[one]. a[one] = a[two]. a[two] = temp. out--) for(in=0.<item3 name>. Improve the understandability of the code class Problem1 { int[] a. for(out=nElems-1. public ArrayBub(int max) { a = new int[max].} Exercise 8: GPS 1. out>1. } public void swap(int one. } public void insert(int value) { a[nElems] = value. in+1). . int nElems.<item2 name>.{<item1 name>. } public void Sort() { int out. in<out. <item4 count>«. nElems++.

out.} 2. public static void incrementElements (int[] integerArray) { int arraylen = integerArray. } .*. i < arraylen. i ++) { integerArray[i] = integerArray[i] + 10. } for (int i=0. Refactor the code class marks { public static int total (int mark1. int mark5. } } 3. i < arraylen. percentagemark = totalMarks / 600.out.println(integerArray[i]).println(integerArray[i]). i ++) { System. for (int i = 0. Refactor the code import java. return percentagemark. int mark4. i < arraylen. class problem3 { int[] numArray = new int[10]. } for (int i = 0.util. int mark6) { int totalMarks = 0. int mark3. i ++) { System.length. int mark2. totalMarks = mark1 + mark2 + mark3 + mark4 + mark5 + mark6 .

} } .