Computer Engineering Department

Bilkent University

CS101: Algorithms and Programming I

Assignment No. 8

March 22, 2002

Notes: 1. Please READ the assignment.  2. Always bring your textbook to the lab for reference purposes.  3. Study the programs before coming to the lab and try to solve them as much as possible.  Keep them in your H drive so that it would be easy to access them in the labs.  You may also keep them in diskette.  4. Your assistants may slightly change the programs as you work on them.

 

A. Purpose of Assignment

Introduction to top down program design using methods.

 

B. Description of Assignment

At the beginning of each program you must indicate your name, date, and the purpose of the program.  Your programs must have proper indentation, and make sure that 1. your variable names are meaningful, 2. your program has enough comments and blank lines for readability. 3. Your programs must have an easy to follow user interface, and for each printed number your program must provide its explanation.  3.  In this assignment test the methods by implementing appropriate main( ) method for each case.  I am assuming that you will test them separately by creating a different class for each program, if you like you may test all in the same class.

 

Program 1: Implement a method that returns the reverse of a number using int arithmetic.

Examples: 1234 becomes 4321, 1200 becomes 21  (i.e., ignore the leading zeros).

In your implementation you are not allowed to use the String class.  

Hint: Note that you can construct a number from its individual digits.  Initialize sum as 0, and execute the following in a loop for each nextDigit: sum= 10 *sum +  nextDigit.

 

Program 2: Implement a method that finds the reverse of a number using the String class.

Examples: 1234 becomes 4321, 1200 becomes 0021  (i.e., do not  ignore the leading zeros). This time in your implementation you must use the String class (please learn its use from the textbook).   Note that you can read a String input using Keyboard.readString( ).

 

Program 3: Implement a boolean method to check if a number is a palindrome.

Examples: 121, 123321 are palindromes (their reverse is the same as the original number).  This method returns either true or false.  Note that for the implementation of this method you may use one of the previous methods.  Keep in mind that a method may invoke another method!  Copy  the method you want to use from one of the previous programs and paste it to the class of Program 3.

 

Program 4: Write an integer in terms of product of powers of primes. 

Every integer can be written as product of powers of primes, for example 2= 2, 13= 13, 18= 2 x 3 x 3 (21.32), 36= 2 x 2 x 3 x 3. In this program you must have two methods:  1. An int type method that gets the user input with proper interaction with the user.  The user input must be a non-negative integer, if the user enters a negative number the control stays in this method and asks the user to enter a non-negative int number.  2. A boolean method that checks if an integer number is a prime number or not. 

Hint: You may change the value of an int variable from 1 to n and as change the value of this variable you may test if it is a prime number if so you may check if it is possible to perform a division without a remainder, etc.

Hint: Your main( ) will involve a loop structure, will get the first input and will check if it is non-zero, if so it will show its prime factors, then will get the next input and will do the same and will stop when the user input is zero.

 

C. When and How to Submit Your Work

Make sure that your programs are all seen and approved by your assistant.  If they have different instructions regarding grading please follow their instructions.