Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(a) Learning debugging is important if you like to be a programmer. To verify a

ID: 3742646 • Letter: #

Question

(a) Learning debugging is important if you like to be a programmer. To verify a program is doing what it should, a programmer should know the expected (correct) values of certain variables at specific places of the program. Therefore make sure you know how to perform the instructions by hand to obtain these values. Remember, you should master the technique(s) of debugging Create a new project TMA02 in NetBeans and copy the following program into a new Java class. The author of the program intends to find the sum of the numbers 1, 2, and 4 public class TMA02Q1a public static void main (String[l args) ( int from1 int to int sum0 for (int i-from ; i

Explanation / Answer

(i) Output: The sum is 4

(ii) Expected value = 1 + 2 + 4 = 7

(iii)

After 2

sum = sum + 2 = 0 + 2 = 2

After 4

sum = sum + 4 = 2 + 4 = 6

After 8

sum = sum + 8 = 6 + 8 = 14

(iv)

public static void main(String[] args) {

int from = 1;

int to = 4;

int sum = 0;

for(int i=from; i<=to; i++)

{

sum = i;

System.out.println("The sum is "+sum);

}

System.out.println("The sum is "+sum);

}

// Output is 1, 2, 3, 4

(v)

public static void main(String[] args) {

int from = 1;

int to = 4;

int sum = 0;

// since we want multiples of 2, we gotta multiply i with 2 each time

for(int i=from; i<=to; i=i*2)

{

sum = sum + i; // each time it has to be added up to the existing sum

}

System.out.println("The sum is "+sum);

}

// Output: The sum is 7

// Hit the thumbs up if you are fine with the answer. Happy Learning!