Object-Oriented Programming with Java, part I + II

cc

This material is licensed under the Creative Commons BY-NC-SA license, which means that you can use it and distribute it freely so long as you do not erase the names of the original authors. If you make changes in the material and want to distribute this altered version of the material, you have to license it with a similar free license. The use of the material for commercial use is prohibited without a separate agreement.

Authors: Arto Hellas, Matti Luukkainen
Translators to English: Emilia Hjelm, Alex H. Virtanen, Matti Luukkainen, Virpi Sumu, Birunthan Mohanathas, Etiënne Goossens
Extra material added by: Etiënne Goossens, Maurice Snoeren, Johan Talboom
Adapted for Informatica by: Ruud Hermans

The course is maintained by Technische Informatica Breda


Assignment operations

Because changing the value of a variable is a very common operation, Java has special assignment operations for it.

int length = 100;

length += 10;  // same as length = length + 10;
length -= 50;  // same as length = length - 50;

When performing the assignment operation on an existing variable, it is written as variable operation= change, for example variable += 5. Note that a variable must be defined before you can assign a value to it. Defining a variable is done by specifying the variable type and the name of the variable.

The following example will not work because the type of the variable length has not been defined.

length = length + 100;  // error!
length += 100;          // error!

When the type is defined, the operations will also work.

int length = 0;
length = length + 100;
length += 100;

// the variable length now holds the value 200

There are also other assignment operations:

int length = 100;

length *= 10;   // same as length = length * 10;
length /= 100;  // same as length = length / 100;
length %= 3;    // same as length = length % 3;

// the variable length now holds the value 1

Often during a loop, the value of a variable is calculated based on repetition. The following program calculates 3*4 somewhat clumsily as the sum 3+3+3+3:

int result = 0;

int i = 0;
while (i < 4) {
   result = result + 3;
   i++;  // means the same as i = i + 1;
}

In the beginning result = 0. During the loop, the value of the variable is incremented by 3 on each iteration. Because there are 4 iterations, the value of the variable is 3*4 in the end.

Using the assignment operator introduced above, we can achieve the same behavior as follows:

int result = 0;

int i = 0;
while (i < 4) {
   result += 3;  // this is the same as result = result + 3;
   i++;          // means the same as i = i+1;
}

Exercise assignment-shorthand-1: The sum of a set of numbers

Create a program that calculates the sum 1+2+3+…+n where n is a number entered by the user.

Example outputs:

Until what? ~~3~~
Sum is 6

The calculation above was: 1+2+3 = 6.

Until what? ~~7~~
Sum is 28

The calculation above was: 1+2+3+4+5+6+7 = 28.

Hint: Create the program using the while statement. Use a helper variable in your program to remember how many times the block has been executed. Use also another helper variable to store the sum. During each execution add the value of the helper variable that counts the executions to the variable in which you should collect the sum.

Exercise assignment-shorthand-2: The sum between two numbers

Similar to the previous exercise, except that the program should ask for both the lower and upper bound. You can assume that the users first gives the smaller number and then the greater number.

First: ~~3~~
Last: ~~5~~
The sum is 12
First: ~~2~~
Last: ~~8~~
The sum is 35

Exercise assignment-shorthand-3: Factorial

Create a program that calculates the factorial of the number n. The factorial n! is calculated using the formula 1×2×3×…×n. For example 4! = 1×2×3×4 = 24. Additionally, it is defined that 0! = 1.

Example outputs:

Type a number: ~~3~~
Factorial is 6
Type a number: ~~10~~
Factorial is 3628800

Exercise assignment-shorthand-4: Sum of the powers

Create a program that calculates the sum of , where n is a number entered by the user. The notation 2i means raising the number 2 to the power of i, for example . In Java we cannot write directly, but instead we can calculate the power with the command Math.pow(number, power). Note that the command returns a number of double type (i.e. floating point number). A double can be converted into the int type (i.e. whole number) as follows: int result = (int)Math.pow(2, 3). This assigns the value of to variable result.

Example outputs:

Type a number: ~~3~~
The result is 15
Type a number: ~~7~~
The result is 255

```