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

The following complete program contains a method that sums thenumbers 1-10; howe

ID: 3614858 • Letter: T

Question

The following complete program contains a method that sums thenumbers 1-10; however, the
sum of those numbers does not display correctly inside main; changethe code so that main,
which calls the method, will display the sum properly. Themain method must not calculate the
sum but must output the sum. Do not use global variables aspart of your solution. Edit the
comments in the program to include the answer to the question posedby the comment.

public class Lab8_Ex1
{
public static void main(String [] args)
{
int sum = 0;
int n = 10;
compute_sum(n);    // (1) What does this linerepresent?
System.out.println(“The sum is “ + sum);
System.exit(0);
}            
  
public static void compute_sum(int x) // (2) What is this lineand
//the code that follows?
{  
int sum = 0;
int num = 1;
while (num <=x)
{
sum += num;
num++;
}
}
}

Explanation / Answer

The int is aprimitive data type, so we need to return it. public class Lab8_Ex1 { publicstatic void main(String []args) { int sum = 0; int n = 10; //set sum equal to the output of the method sum = compute_sum(n); System.out.println("The sumis " + sum); System.exit(0); } // return an int of theoutput of this method publicstatic int compute_sum(intx) { int sum = 0; int num = 1; while (num
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote