You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

5.1 KiB

Exercise 1

Exercise 1: Debugging in Java

1. Open the program Uebung1.java in the editor

The program Uebung1.java is a simple program that reads in a natural number and checks whether it is a prime number. A prime number is a number that is only divisible by 1 and itself.

2. Start the program multiple times ("run as Java Application") and enter different natural numbers.

When you start the program for the first time, it will display a prompt where you can enter a natural number. Enter any number, for example 12. The program will then output "number 12 passed check: true".

Start the program again now and enter a different number, for example 10. The program will then output "number 10 passed check: false".

3. Activate lines 36-38 by removing the two slashes at the beginning of the line

If you activate lines 36-38, the following outputs will be added:

  • "input: , loop variable: , result "

Start the program again and enter any number. You should now see the following outputs:

enter an integer number: 12
input: 12, loop variable: 2, result 0
number 12 passed check: true

4. Set a breakpoint on line 35

A breakpoint is a point in the code where the execution of the program will be paused.

5. Start the program as usual

If we start the program now, it will be paused on line 35. You can now check the values of the variables.

In this case, the values of the variables are as follows:

  • nextInt: The input value entered by the user.
  • i: The current loop variable.
  • result: The result of the division of nextInt by i.

6. Start the program in debug mode and enter the number 45

To start the program in debug mode, select "debug as Java Application" from the context menu.

Now enter the number 45. The program will be paused on line 35.

7. Note the contents of the variables

In this case, the values of the variables are as follows:

  • nextInt: 45
  • i: 2
  • result: 0

8. Continue running the program until the next breakpoint ("resume")

Press the F8 key to continue running the program. The program will be paused on line 36.

9. Note the contents of the variables

In this case, the values of the variables are as follows:

  • nextInt: 45
  • i: 3
  • result: 15

10. Continue running the program until the next breakpoint ("resume")

Press the F8 key again to continue running the program. The program will be paused on line 37.

11. Note the contents of the variables

In this case, the values of the variables are as follows:

  • nextInt: 45
  • i: 4
  • result: 9

12. Terminate the debugger ("Terminate")

Press the F10 key to terminate the debugger.


Exercise 2

Exercise 2: Debugging in Java

1. Open the program Uebung2.java in the editor

The program Uebung2.java is a simple program that reads in a natural number and checks whether it is a prime number. A prime number is a number that is only divisible by 1 and itself.

2. Start the program multiple times ("run as Java Application") and enter different natural numbers.

When you start the program for the first time, it will display a prompt where you can enter a natural number. Enter any number, for example 12. The program will then output "number 12 passed check: true".

Start the program now again and enter a different number, for example 10. The program will then output "number 10 passed check: false".

3. Set a breakpoint on line 40

A breakpoint is a point in the code where the execution of the program will be paused.

4. Start the program as usual

If you start the program now, it will be paused on line 40. You can now check the values of the variables.

5. Start the program in debug mode ("debug as Java Application") and enter the number 45

To start the program in debug mode, select "debug as Java Application" from the context menu.

Now enter the number 45. The program will be paused on line 40.

6. Note the contents of the variables

In this case, the values of the variables are as follows:

  • nextInt: 45
  • count: 2

7. Note the number of entries in the Debug View

The Debug View contains a list of variables and their values. In this case, the Debug View contains two entries:

  • nextInt: 45
  • count: 2

8. Continue running the program one step at a time ("step over").

Press the F8 key to continue running the program one step at a time. The program will be paused on line 41.

9. In which line is the debugger?

The debugger is on line 41.

10. Note the number of entries in the Debug View

The Debug View still contains two entries:

  • nextInt: 45
  • count: 2

11. Terminate the debugger ("Terminate")

Press the F10 key to terminate the debugger.

12. Continue running the program one step at a time ("step into").

Press the F7 key to continue running the program one step at a time. The program will be paused on line 40.

13. In which line is the debugger?

The debugger is back on line 40.

14. Note the number of entries in the Debug View

The Debug View now contains three entries:

  • nextInt: 45
  • count: 2
  • return: false

15. Terminate the debugger ("Terminate")

Press the F10 key to terminate the debugger.