Object-Oriented Programming
Programming exercises

Week 1. Basic data types and control structures.

Exercices adapted from Deitel & Deitel’s Java How to Program 6th Ed, Chapters 2 & 4.

Note: Keep it simple! You are NOT required to use more than one class and more than one method in the following exercises.

1.
a. Compile, and run the following program :

 1  // Fig. 2.7: Addition.java
 2  // Addition program that displays the sum of two numbers.
 3  import java.util.Scanner; // program uses class Scanner
 4
 5  public class Addition
 6  {
 7     // main method begins execution of Java application
 8     public static void main( String args[] )
 9     {
10        // create Scanner to obtain input from command window
11        Scanner input = new Scanner( System.in );            
12
13        int number1; // first number to add   
14        int number2; // second number to add  
15        int sum; // sum of number1 and number2
16
17        System.out.print( "Enter first integer: " ); // prompt
18        number1 = input.nextInt(); // read first number from user
19
20        System.out.print( "Enter second integer: " ); // prompt
21        number2 = input.nextInt(); // read second number from user
22
23        sum = number1 + number2; // add numbers
24
25        System.out.printf( "Sum is %d\n", sum ); // display sum
26
27     } // end method main
28
29  } // end class Addition
 

b. Write an application that asks the user to enter two integers, obtains them from the user and prints their sum, product, difference and quotient (division). Use the techniques shown in a.

 

2.

Write an application that inputs three integers from the user and displays the sum, average, product, smallest and largest of the numbers. [Note: The calculation of the average in this exercise should result in an integer representation of the average. So if the sum of the values is 7, the average should be 2, not 2.3333....]

 

 

3.

Write an application that reads two integers, determines whether the first is a multiple of the second and prints the result. [Hint : Use the remainder operator.]

 

4.
Write an application that inputs from the user the radius of a circle as an integer and prints the circle's diameter, circumference and area using the floating-point value 3.14159 for p [Note: You may also use the predefined constant Math.PI for the value of p. This constant is more precise than the value 3.14159. Class Math is defined in package java.lang. Classes in that package are imported automatically, so you do not need to import class Math to use it.] Use the following formulas (r is the radius):

diameter = 2r
circumference = 2pr
area  = pr2

Do not store the results of each calculation in a variable. Rather, specify each calculation as the value that will be output in a System.out.printf statement. Note that the values produced by the circumference and area calculations are floating-point numbers. Such values can be output with the format specifier %f in a System.out.printf statement.

5.

Write an application that inputs one number consisting of five digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 42339, the program should print

 

4    2    3    3    9

Assume that the user enters the correct number of digits. What happens when you execute the program and type a number with more than five digits? What happens when you execute the program and type a number with fewer than five digits? [Hint: It is possible to do this exercise with the techniques you learned in this chapter. You will need to use both division and remainder operations to "pick off" each digit.]

6.
Write an application that calculates the squares and cubes of the numbers from 0 to 10 and prints the resulting values in table format, as shown below. [Note: This program does not require any input from the user.]

number  square  cube
0       0       0
1       1       1
2       4       8
3       9       27
4       16      64
5       25      125
6       36      216
7       49      343
8       64      512
9       81      729
10      100     1000

7.
Write a program that inputs five numbers and determines and prints the number of negative numbers input, the number of positive numbers input and the number of zeros input.

8.
Write an application that outputs the following figure:

a)                                        b)

*                     *
**                   * *
***                 * * *
****               * * * *
*****             * * * * *
******           * * * * * *
*******         * * * * * * *
********       * * * * * * * *

9.
Write an application that inputs an array of no more than 20 integers, then outputs them in ascending order. The program should stop taking inputs either when a zero value is entered, the zero should not be counted as an input, or when the number of input values reaches 20. The rest of inputs, if there are, are to be ignored.

[Hint: to keep the code simple, you might want to use the bubble sort algorithm.

bubbleSort( A : list of sortable items ):
  do
    swapped := false
    for each i in 0 to length(A) - 1 inclusive do:
      if A[i] > A[i+1] then
        swap( A[i], A[i+1] )
        swapped := true
      end if
    end for
  while swapped

]

 

Non-programming exercises

Self-Review Exercises in Chapter 2, 4. Java How to Program 6th Edition.