Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Exercises

1.
Write a program that asks the user to type in two integer values. Test these two numbers to determine whether the first is evenly divisible by the second and then display an appropriate message at the terminal.

2.
Program 6.8A displays the value in the accumulator even if an invalid operator is entered or division by zero is attempted. Fix that problem.

3.
Modify the print method from the Fraction class so that whole numbers are displayed as such (so the fraction 5/1 should display as simply 5). Also modify the method to display fractions with a numerator of 0 as simply zero.

4.
Write a program that acts as a simple printing calculator. The program should allow the user to type in expressions of the following form:

    number  operator
The program should recognize the following operators:
    +   -   *   /   S   E

The S operator tells the program to set the accumulator to the typed-in number, and the E operator tells the program that execution is to end. The arithmetic operations are performed on the contents of the accumulator, with the number that was keyed in acting as the second operand. The following is a sample run showing how the program should operate:

Begin Calculations
10 S            Set Accumulator to 10
= 10.000000     Contents of Accumulator
2 /             Divide by 2
= 5.000000      Contents of Accumulator
55 -            Subtract 55
= -50.000000
100.25 S        Set Accumulator to 100.25
= 100.250000
4 *             Multiply by 4
= 401.000000
0 E             End of program
= 401.000000
End of Calculations.

Make sure that the program detects division by 0 and also checks for unknown operators. Use the Calculator class developed in Program 6.8 for performing your calculations.

5.
We developed Program 5.9 to reverse the digits of an integer typed in from the terminal. However, this program does not function well if you type in a negative number. Find out what happens in such a case, and then modify the program so that negative numbers are correctly handled. By this, we mean that if the number -8645 were typed in, for example, the output of the program should be 5468-.

6.
Write a program that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in English. So if the user types in 932, the program should display the following:

nine
three
two

(Remember to display zero if the user types in just 0.) Note: This exercise is a hard one!

7.
Program 6.10 has several inefficiencies. One inefficiency results from checking even numbers. Because any even number greater than 2 obviously cannot be prime, the program could simply skip all even numbers as possible primes and as possible divisors. The inner for loop is also inefficient because the value of p is always divided by all values of d from 2 through p–1. You can avoid this inefficiency if you add a test for the value of isPrime in the conditions of the for loop. In this manner, you can set up the for loop to continue as long as no divisor is found and the value of d is less than p. Modify Program 6.10 to incorporate these two changes; then run the program to verify its operation.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint