Skip to content

Instantly share code, notes, and snippets.

@Aroueterra
Created June 24, 2021 10:06
Show Gist options
  • Save Aroueterra/20c1b2a7d8fed206278323b41871cfa3 to your computer and use it in GitHub Desktop.
Save Aroueterra/20c1b2a7d8fed206278323b41871cfa3 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
//add class definitions below this line
class Median {
public double calculateMedian(int num1, int num2, int num3, int num4, int num5){
int[] arr = { num1, num2, num3, num4, num5 };
double median = 0.0;
Arrays.sort(arr);
if (arr.length % 2 == 0){
median = ((double)arr[arr.length/2] + (double)arr[arr.length/2 - 1])/2;
}
else {
median = (double) arr[arr.length/2];
}
return median;
}
public double calculateMedian(int num1, int num2,int num3, int num4){
int[] arr = { num1, num2, num3, num4 };
double median = 0.0;
Arrays.sort(arr);
if (arr.length % 2 == 0){
median = ((double)arr[arr.length/2] + (double)arr[arr.length/2 - 1])/2;
}
else {
median = (double) arr[arr.length/2];
}
return median;
}
public double calculateMedian(int num1, int num2, int num3){
int[] arr = { num1, num2, num3 };
double median = 0.0;
Arrays.sort(arr);
if (arr.length % 2 == 0){
median = ((double)arr[arr.length/2] + (double)arr[arr.length/2 - 1])/2;
}
else {
median = (double) arr[arr.length/2];
}
return median;
}
public double calculateMedian(int num1, int num2){
int[] arr = { num1, num2 };
double median = 0.0;
Arrays.sort(arr);
if (arr.length % 2 == 0){
median = ((double)arr[arr.length/2] + (double)arr[arr.length/2 - 1])/2;
}
else {
median = (double) arr[arr.length/2];
}
return median;
}
}
//add class definitions above this line
public class Exercise4 {
public static void main(String[] args) {
//add code below this line
var m = new Median();
var x = m.calculateMedian(3, 5, 1, 4, 2);
System.out.println(x);
//add code above this line
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment