0% found this document useful (0 votes)
48 views1 page

Sesion 02 Statisticsexample

This document contains code for a Statistics class with a method to calculate the average of a variable number of integer arguments. The main method then tests the average method by calling it with different numbers of arguments, from zero to five integers, and prints out the resulting averages.

Uploaded by

carsistemas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views1 page

Sesion 02 Statisticsexample

This document contains code for a Statistics class with a method to calculate the average of a variable number of integer arguments. The main method then tests the average method by calling it with different numbers of arguments, from zero to five integers, and prints out the resulting averages.

Uploaded by

carsistemas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

SESION 02

package com.example;
StatisticsExample
package com.example;
public class StatisticsTest {

public class Statistics {


public static void main(String[] args) {

Statistics stats = new Statistics ();


public float average(int... nums) {
// Test edge cases also
int sum = 0;
float avg0 = stats.average();

float avg1 = stats.average(100);


float result = 0;
float avg2 = stats.average(100, 200);
if (nums.length > 0) {
float avg3 = stats.average(100, 200, 300);
for (int x : nums) { // iterate int array nums
float avg4 = stats.average(100, 200, 300, 400);
sum += x;
float avg5 = stats.average(100, 200, 300, 400,
}
500);
result = (float) sum / nums.length;
System.out.println("Average with no parameters:
} " + avg0);

return (result); System.out.println("Average of 100 is: " + avg1);

} System.out.println ("Average of 100 and 200 is: "


+ avg2);
}
System.out.println ("Average of 100, 200 and 300
is: " + avg3);

System.out.println ("Average of 100, 200, 300 and


400 is: " + avg4);

System.out.println ("Average of 100, 200, 300,


400 and 500 is: " + avg5);

You might also like