Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
  
  
Lab 1B: Numeric Calculations 
 
Introduction: 
 
Computers are ideal for numeric calculations, so in this section we will work to develop a 
program to compute the mean and variance of several input values. The mean is simply the 
average of the N input values. It is calculated using the following formula: 
 
mean = (value1 + value2 + ... + valueN) / N 
 
The variance is a measure of the average distance squared between N values and their mean. If 
the N values are very close to the mean value, the variance will be small. If one or more values 
are far away from the mean, the variance will be large. The variance is calculated using the 
following formula: 
 
variance = ((value1-mean)*(value1-mean)  
          + (value2-mean)*(value2-mean) + ...  
          + (valueN-mean)*(valueN-mean)) / N 
 
For example, if we have four values 8, 10, 12, 14, we have: 
 
mean  = (8 + 10 + 12 + 14)/4  
      = 44/4 = 11.  
 
variance = ((8-11)*(8-11) + (10-11)*(10-11) +  
            (12-11)*(12-11) + (14-11)*(14-11))/4  
          = ((-3)*(-3) + (1)*(-1) + (1)*(1) + (3)*(3))/4  
          = (9 + 1 + 1 + 9)/4  
          = 20/4 = 5. 
 
As you can see, this calculation is rather long and boring, so it is an ideal task to automate in a 
small program. In the next section, you will be creating a program to read four values, and 
calculate and print the mean and variance. 
 
Instructions: 
 
The Java program below declares and initializes four variables n1, n2, n3, n4. The program then 
declares and initializes two variables "mean" and "variance" that are later printed. 
 
 
 
import java.util.Scanner; 
public class Main 
{ 
  public static void main (String[]args) 
  { 
    Scanner scnr = new Scanner (System.in); 
 
    // Input values 
    float n1 = 8; 
    float n2 = 10; 
    float n3 = 12; 
    float n4 = 14; 
 
    // Calculate mean of 4 values 
    float mean = 11; 
 
    // Calculate variance of 4 values 
    float variance = 5; 
 
    // Print the output 
    System.out.println("mean = " + mean); 
    System.out.println("variance = " + variance); 
 
  } 
} 
 
Step 1: Go to onlinegdb.com, select the “Java” compiler, and copy and paste the program above 
into the editor window and compile the program. Hopefully you will not get any error messages. 
 
Step 2: Run your program to see what it will output. Notice that you are printing the correct 
mean and variance for the four input values, but the answer is "hard coded" for these values. This 
is clearly not the best way to solve this problem. 
 
Step 3: Edit your program to implement the correct formula for calculating the mean. We are 
breaking this process into small pieces on purpose so don't implement the variance formula yet. 
 
Step 4: Compile and run your program. You should get the same output as before, but this time 
your mean calculation is no longer hard coded. Now change the value of n1 to 100 and recompile 
and run your program. Hopefully your mean value has increased. 
 
Step 5: Edit your program to implement the correct formula for calculating the variance. Since 
the formula is rather long, you may want to split it into several lines with a semicolon at the very 
end. Just be careful that the round brackets are matching properly. 
 
Step 6: Change the value of n1 back to 8 again, and compile and run your program. Hopefully 
your program is now calculating mean and variance correctly. Now change the values of the 
input variables to 80, 100, 120, 140 and recompile and run the program to see what the new 
mean and variance are. 
 
Step 7: Editing a program to change input values is a slow and painful process. To make the 
program more general purpose, we can ask the user to type in the desired data values when they 
run the program. To do this, replace your n1, n2, n3, n4 declarations with the following code: 
 
float n1, n2, n3, n4; 
System.out.print("Enter four numbers:"); 
n1= scnr.nextFloat(); 
n2= scnr.nextFloat(); 
n3= scnr.nextFloat(); 
n4= scnr.nextFloat(); 
 
Step 8: Compile and run your program. When you enter the values 8 10 12 14 you should get 
mean=11, variance=5. When you enter the values 80 100 120 140 you should get mean=110, 
variance=500. If this is not what your program outputs, go back to check your mean and variance 
formulas. 
 
Step 9: Once you think your program is working correctly, upload your final program and a copy 
of your program output into Blackboard for grading.