Program is below if possible please explain a little so when

Program is below, if possible please explain a little, so when i do it again i can understand more clearer, thank you.

Lab0601.java should sum and display the values between 1 and a limit entered by the user. For example, if when prompted the user entered a limit of 3, the program should display the value 6 (6 =1 + 2 + 3). Perform the calculations by hand for the following values and enter the anticipated sum in the appropriate column next to each value:
Value
Entered   Sum of 0…value
5  
8  
12  
21  
30  

1.   Lab0601.java is not complete. You need to supply the appropriate relational expression in the while statement and complete body of the loop. Run Lab0601.java five times, inputting a value from the table above each time. Note the sum given by the program for each value given above. Were the results of the test runs consistent with your predictions?


2.   Revise Lab0601.java (save it as Lab0602.java) and insert the statements to find and display the average of the sum of the values. Replace System.out.println() with a call to a formatting method (printf()) for the line which displays the sum and in the line which will display the average (to 2 decimal places). Since the number of values is the limit, you can use that value as the divisor. Run with the data from 1 above and record the average in the corresponding space(s) below:

Value
Entered   Average of the sum
5  
8  
12  
21  
30  

Hardcopy Lab0602.java to present with this Answer Sheet for review and evaluation.

3.   Does Lab0601.java (and therefore Lab0602.java) control loop execution via a counter (a count-controlled loop) or by detecting an event (an event-controlled loop)? Explain.

The program prompts for a value which will determine the   number of repetitions for the loop. Within the loop the   program will prompt for a number and add the value   value entered to an accumulator.


import java.util.Scanner;public class Lab0601 {

public static void main(String[] args){

int limit   = 0;int counter = 0;int valu    = 0;int sum     = 0;float avg   = 0.0F;
// Prompt for a repetition limit
        Scanner kBd = new Scanner(System.in);        System.out.print(\"Sum from 0 to...? \");        limit = kBd.nextInt();
// Develop the loop to sum the values between one (1) and the limit entered bythe user//        while (conditional expression)        {

}System.out.println(\"\ The sum of the numbers between 0 and \" + limit

+ \" is...\" + sum);

System.out.println(\"\ \ **** end of Lab0601 ****\ \ \");

} // end of main} // end of Lab0601 class

Solution

1.

import java.util.Scanner;

public class Lab0601 {
public static void main(String[] args){
int limit = 0;int counter = 0;int valu = 0;int sum = 0;float avg = 0.0F;
// Prompt for a repetition limit
Scanner kBd = new Scanner(System.in); System.out.print(\"Sum from 0 to...? \"); limit = kBd.nextInt();
  
while (counter<= limit){
sum+=counter;
counter++;
}

System.out.println(\"\ The sum of the numbers between 0 and \" + limit
+ \" is...\" + sum);
System.out.println(\"\ \ **** end of Lab0601 ****\ \ \");
}}

The main thing is while loop and to sum numbers till limit we need a counter and you can see that the adding counter can get the sum. So the condition becomes while counter is less and equal to the limit. In the body of while we will add counter to previous value of sum and increment the counter by one.

To calculate average you can use same code just divide the calculated sum with limit and store in a float variable like

avg=(1.0f*sum)/limit

This is done to make calculation to decimal level

Program is below, if possible please explain a little, so when i do it again i can understand more clearer, thank you. Lab0601.java should sum and display the v
Program is below, if possible please explain a little, so when i do it again i can understand more clearer, thank you. Lab0601.java should sum and display the v

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site