Sie sind auf Seite 1von 11

PROGRAMMING PROBLEMS IN ADVANCE JAVA (Prepared by Armel D. De los San os!

Pro"e# $% &a# or'al The factorial of a positive number refers to the product of all the whole numbers (integers) from 1 through the number itself. A factorial is indicated by the symbol "!". Thus 6! 1!"!#!$!%!6 &"'. (actorial )ero* '!* is defined as 1. +rite a program with a method named (a# or'al(!). The method (a# or'al(!) accepts one argument n* computes the factorial of n* and returns the factorial of n to the calling routine. The following is a sample run of the program, -nput a number, . The factorial of . is $'#"'. Pro"e# *% Pr'me N+mber En+mera 'on A prime number is a whole number that can only be divided without a remainder by itself and one. 1* "* #* "#* and /& are e0amples of prime numbers. +rite a program* with a method 'sPr'me(!)* that as1s a user to input a number n* and displays the first n prime numbers starting from 1. The method 'sPr'me(!) shall accept an integer argument and returns true if the integer argument is a prime number2 otherwise* it shall return false. The following is a sample run of the program, 3ow many prime numbers, 1' The first 1' prime numbers are, 1* "* #* %* &* 11* 1#* 1&* 1/* "#. Pro"e# ,% Per(e# N+mber En+mera 'on A perfect number is defined as an integer which is the sum of its proper positive divisors* e0cluding itself. 4i0 (6) is the first perfect number* because 1* " and # are its proper positive divisors and 15"5# 6. 6reate a program* with a method 'sPer(e# (!)* that displays the perfect numbers from 17%''. The method 'sPer(e# (!) shall accept an integer argument and returns true if the argument is a perfect number2 otherwise* it shall return false. The numbers should be displayed hori)ontally and separated by commas. Pro"e# -% Joana and .e Odd N+mbers 8oana loves playing with odd numbers. -n the other day* she started writing* in each line* an odd number of odd numbers. -t loo1ed as follows, 1 # % &

/ 11 1# 1% 1& 1/ "1 "# "% "& "/ #1 ... 9n a certain line 8oana wrote %% odd numbers. 6an you discover the sum of the last three numbers written in that line: 6an you do this more generally for a given ;uantity of odd numbers: Problem <iven the number N of odd numbers in a certain line* your tas1 is to determine the sum of the last three numbers of that line. Inp+ The input is a se;uence of lines* one odd number N (1<N<1000000000) per line O+ p+ (or each input line write the sum of the last three odd numbers written by 8oana in that line with N numbers. This sum is guaranteed to be less than "6#. Sample Inp+ # % & Sample O+ p+ 1% $% .& Pro"e# /% 01ly N+mbers =gly numbers are numbers whose only prime factors are "* # or %. The se;uence 1* "* #* $* %* 6* .* /* 1'* 1"* 1%* ... shows the first 11 ugly numbers. >y convention* 1 is included. +rite a program* with a method 's01ly(!) to find and print the 1''?th ugly number. The method 's01ly(!) accepts an integer argument and returns true if the argument is an ugly number2 otherwise* it returns false. Inp+ and O+ p+ There is no input to this program. 9utput should consist of a single line as shown below* with @numberA replaced by the number computed.

Sample o+ p+ The 1''?th ugly number is @numberA.

ARRA2 PROBLEMS Pro"e# 3% S4+are Roo and C+be Roo +rite a program that accepts a series of numbers* stores the numbers into an array* writes the numbers* s;uare root and the cube root of the numbers* the sum of the numbers* the sum of the s;uare root of the numbers* and the sum of the cube root of the numbers. Bound off floating point numbers up to the second decimal place. The following is a sample run of the program, 3ow many numbers will you input: % -nput % numbers, # 6 / 1" 1% Cumbers # 6 / 1" 1% $% 4;uare Boot 1.&# ".$% #.'' #.$6 #..& 1$.%" 6ube Boot 1.$$ 1.." ".'. "."/ ".$& 1'.'/

Total

Try again DEFCG: C Than1 you. Pro"e# 5% Sor 'n1 N+mbers +rite a program that as1s a user to input the value for n* as1s the user to input n numbers* stores the numbers into an array* sorts the numbers in ascending order* and outputs the sorted numbers on screen. The following is a sample run of the program, 3ow many numbers will you input: % -nput % numbers, # 6 1# 1" % 4orted numbers, #* %* 6* 1"* 1# Try again DEFCG: E 3ow many numbers will you input: & -nput & numbers, # 6 1 1#

1" %' % 4orted numbers, 1* #* %* 6* 1"* 1#* %' Try again DEFCG: C Than1 you. Pro"e# 6% Men+ Sele# 'on +rite a program that displays a menu selection on screen similar to the following, 1. ". #. $. %. HIC= -nput the numbers 6ompute the sum 6ompute the average (ind the highest I0it

-nput your choice, +hen the user chooses J-nput the numbersK* the program as1s for the number of numbers to be entered and then proceeds to as1 the numbers. +hen the user chooses J6ompute the sumK* the program displays the sum of the numbers. +hen the user chooses J6ompute the averageK* the program displays the average of the numbers. +hen the user chooses J(ind the highestK* the program displays the highest number in the array. +hen the user chooses JI0itK* the program terminates. Pro"e# 7% De#'mal o B'nary Con8ers'on +rite a program that as1s a user to input a floating point decimal number* converts the number into its e;uivalent binary number* and displays the result on screen. -f the floating point part is an infinite number then end the conversion up to the "'th decimal place. The following is a sample run of the program, -nput a decimal number, %6.#% %6.#% is 111'''.'1'11''11''11''11''1 in binary. Pro"e# $9% &ra# 'ons o &loa 'n1 Po'n +rite a program that will accept a fraction of the form CFL* where C is the numerator and L is the denominator* and prints out the floating point representation. -f the floating point representation has a repeating se;uence of digits* it should be indicated by enclosing it in parentheses. (or e0ample* 1F# .########... is denoted as .(#)* and $1F### .1"#1"#1"#... is denoted as .(1"#). Typical conversions are, 1F# .(#)

""F% 1F& #F. $%F%6

$.$ .(1$".%&) .#&% ..'#(%&1$".)

Test your program with the fractions above and the fraction 11F%/. 4ample Bun ICTIB C*L , 1*& 1F& .(1$".%&)

Pro"e# $$% Roman Ro+le e The historian (lavius 8osephus relates how* in the Bomano78ewish conflict of 6& A.L.* the Bomans too1 the town of 8otapata which he was commanding. Iscaping* 8osephus found himself trapped in a cave with $' companions. The Bomans discovered his whereabouts and invited him to surrender* but his companions refused to allow him to do so. 3e therefore suggested that they 1ill each other* one by one* the order to be decided by lot. Tradition has it that the means for effecting the lot was to stand in a circle* and* beginning at some point* count round* every third person being 1illed in turn. The sole survivor of this process was 8osephus* who then surrendered to the Bomans. +hich begs the ;uestion, had 8osephus previously practiced ;uietly with $1 stones in a dar1 corner* or had he calculated mathematically that he should adopt the #1st position in order to survive: 3aving read an account of this gruesome event you become obsessed with the fear that you will find yourself in a similar situation at some time in the future. -n order to prepare yourself for such an eventuality you decide to write a program to run on your hand7held M6 which will determine the position that the counting process should start in order to ensure that you will be the sole survivor. -n particular* your program should be able to handle the following variation of the processes described by 8osephus. n A ' people are initially arranged in a circle* facing inwards* and numbered from 1 to n. The numbering from 1 to n proceeds consecutively in a cloc1wise direction. Eour allocated number is 1. 4tarting with person number i* counting starts in a cloc1wise direction* until we get to person number 1 (1 A ')* who is promptly 1illed. +e then proceed to count a further 1 people in a cloc1wise direction* starting with the person immediately to the left of the victim. The person number 1 so selected has the Nob of burying the victim* and then returning to the position in the circle that the victim had previously occupied. 6ounting then proceeds from the person to his immediate left* with the 1th person being 1illed* and so on* until only one person remains. (or e0ample* when n The survivor is $. Inp+ and O+ p+ %* and 1 "* and i 1* the order of e0ecution is "* %* #* and 1.

Oalues for n and 1 and output the number of the person with which the counting should begin in order to ensure that you are the sole survivor. (or e0ample* in the above case the safe starting position is #. Eour program may assume a ma0imum of 1'' people ta1ing part in this event. Sample Inp+ 11 1% '' Sample O+ p+ 1 1 Pro"e# $*% Sor 'n1 Al1or' .ms +rite a program that displays the following menu selection on screen, HIC= 1. ". #. $. %. 6. &. >ubble sort 4election sort -nsertion sort 4hell sort Herge sort Puic1 sort I0it

-nput your choice, +hen the user chooses 1 to 6* the program will as1 the user to input a series of numbers and then sorts it accordingly. 9utput the original numbers as well as the sorted numbers.

S:RING PROBLEMS Pro"e# $,% S r'n1 Sor 'n1 +rite a program that accepts a string as an input* arranges the string alphabetically and outputs the result on screen. -nclude a loop that will allow the user to repeat the process for as long as heFshe li1es. A sample run of the program loo1s li1e the following, -nput a string, =niversityof>aguio The sorted string is, a>efgiiinoorst=uvy Try again DEFCG: C Than1 you. Pro"e# $-% Roman N+mber Add' 'on +rite a program that shall as1 a user to input two Boman numbers* adds the two numbers then output the result on screen. The following is a sample run of the program, -nput two Boman numbers separated by a space, -O Q-O plus Q- is QO. Lo you want to add another number DEFCG: E -nput two Boman numbers separated by a space, R Q6 R plus Q6 is 6QQQ. Lo you want to add another number DEFCG: C Than1 you. Pro"e# $/% N+mber o ;ord Con8ers'on +rite a program that shall as1 a user to input an integer number* output on screen the word e;uivalent of the number. The following is a sample run of the program, -nput an integer number, #' Thirty Lo you want to try again DEFCG: E -nput an integer number, #"&6& Thirty two thousand seven hundred si0ty seven Lo you want to try again DEFCG: C Than1 you. Pro"e# $3% S r'n1 Sear#. +rite a program that as1s a user to input two strings* string1 and string"* where string1 is shorter or e;ual to string". The program searches string" for any occurrence of string1 then reports the result to the user. A sample run of the program loo1s li1e the following, -nput string 1, =niversity -nput string ", The =niversity of >aguio J=niversityK is found in JThe =niversity of >aguioK. Try again DEFCG: E

-nput string 1, 6ity -nput string ", >aguio 6ity* Mhilippines 6ity is found in >aguio 6ity* Mhilippines. Try again DEFCG: C Than1 you. Pro"e# $5% S r'n1 O##+rren#e Co+n +rite a program that as1s a user to input two strings* string1 and string"* where string1 is shorter or e;ual to string". The program searches string" for any occurrence of string1 then reports the reports the number of occurrence of string1. A sample run of the program loo1s li1e the following, -nput string 1, =niversity -nput string ", The =niversity of >aguio Cumber of occurrence, 1 Try again DEFCG: E -nput string 1, 6ity -nput string ", 6ebu 6ity* >aguio 6ity* 4an 6arlos 6ity Cumber of occurrence, # Try again DEFCG: E -nput string 1, 0 -nput string ", university Cumber of occurrence, ' Try again DEFCG: C Than1 you.

CLASS PROBLEMS Pro"e# $6% Address Boo< En ry Eour tas1 is to create a class that contains an address boo1 entry. The following table describes the information that an address boo1 entry has. A r'b+ es Came Address Telephone Cumber Imail Address Des#r'p 'on Came of the person in the address boo1 Address of the person Telephone number of the person MersonSs Imail address

(or the methods* create the following, 1. Mrovide the necessary accessor and mutator methods for all the attributes. ". 6onstructors. Came the class as AddressBoo<En ry."a8a. Pro"e# $7% :es Address Boo< En ry 6reate a class Address>oo1 that can contain 1'' entries of Address>oo1Intry obNects (use the class you created in MroNect 1.). Eou should provide the following methods for the Address>oo1. 1. ". #. $. Add an entry Lelete an entry Oiew all entries Idit an entry

4ave your class as AddressBoo<."a8a. Pro"e# *9% Employee Re#ord Eour tas1 is to create a class that contains an employee record. The following table describes the information that an employee record has. A r'b+ es -L Cumber Came Address Begular rate Cumber of days wor1ed Cumber of hours overtime 4alary Des#r'p 'on -dentification number Came of the person in the address boo1 Address of the person 4alary of the employee for one day Cumber of days the employee wor1ed Cumber of hours the employee wor1ed overtime. The number of minutes is converted to decimal (e.g. 1 hour #' mins is e;ual to 1.%. 9vertime rate is regular rate plus %'T of regular rate. The salary of the employee including the overtime

(or the methods* create the following,

1. Mrovide the necessary accessor and mutator methods for all the attributes. ". 6onstructors. #. Mrovide a method that computes the salary of the employee. 4ave your file as EmployeeRe#ordClass."a8a. Pro"e# *$% :es Employee Re#ord 6reate a class ImployeeBecord that can contain 1'' entries of ImployeeBecord6lass obNects (use the class you created in MroNect "'). Eou should provide the following methods for the ImployeeBecord. 1. ". #. $. Add an entry Lelete an entry Idit an entry Oiew all entries

The Lisplay report method displays a report in table form that contains the following information, -d Cumber* name* and salary. 4ave your file as EmployeeRe#ord."a8a.

Das könnte Ihnen auch gefallen