Repository navigation
Expand file tree
/
Copy pathJavaMethods2.java
More file actions
64 lines (53 loc) · 1.61 KB
/
Copy pathJavaMethods2.java
File metadata and controls
64 lines (53 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package day21_JavaMethods;
import java.util.Scanner;
public class JavaMethods2 {
// 2. Write a Java method to compute the average of three numbers. Go to the editor
// Test Data:
// Input the first number: 25
// Input the second number: 45
// Input the third number: 65
// Expected Output:
//
// The average value is 45.0
public static void main(String[] args) {
//do not use void, but return the value from your method
double[] nums = getDoubles();
average(34, 3);
average(34);
average(34, 667);
average(34, 45);
System.out.print("The average value is " + average(nums));
}
private static double[] getDoubles() {
double[] nums = new double[3];
Scanner in = new Scanner(System.in);
System.out.print(" first number: ");
nums[1] = in.nextDouble();
System.out.print("second number: ");
nums[2] = in.nextDouble();
System.out.print("third number: ");
nums[3] = in.nextDouble();
return nums;
}
public static double average(double[] numbers) {
if (numbers.length == 0) return 0.0;
int sum = 0;
for (double n : numbers) {
sum += n;
}
return sum / numbers.length;
}
//method overloading
public static int average(double x, String operation) {
return -1;
}
public static double average(int x, int i) {
return (x) / 3;
}
public static double average(double x) {
return (x) / 3;
}
public static double average(double x, double y) {
return (x + y) / 3;
}
}