Skip to content

Instantly share code, notes, and snippets.

@suminb
Created August 28, 2012 19:54
Show Gist options
  • Save suminb/3503460 to your computer and use it in GitHub Desktop.
Save suminb/3503460 to your computer and use it in GitHub Desktop.
Basic Java programming exercise
import java.util.Arrays;
import java.util.List;
/**
* This is a basic Java programming exercise. Fill out all unimplemented class
* methods.
*
* @author Sumin Byeon <suminb@gmail.com>
*
*/
public final class Exercise1 {
private void execute() {
List<Integer> unsortedList = Arrays.asList(8, 3, 6, 5, 1, 2, 9, 4, 7);
List<Integer> sortedList1 = Arrays.asList(2, 3, 5, 7, 11, 13);
List<Integer> sortedList2 = Arrays.asList(2, 4, 6, 8, 10, 12);
// Expected output: 1
System.out.printf("min(unsortedList) = %d\n", min(unsortedList));
// Expected output: 9
System.out.printf("max(unsortedList) = %d\n", max(unsortedList));
// Expected output: 41
System.out.printf("sum(sortedList1) = %d\n", sum(sortedList1));
// Expected output: 42
System.out.printf("sum(sortedList2) = %d\n", sum(sortedList2));
// Expected output: 6.833
System.out.printf("average(sortedList1) = %.3f\n", average(sortedList1));
// Expected output: [2, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13);
System.out.printf("merged list = %s\n", merge(sortedList1, sortedList2));
// Expected output: [13, 11, 7, 5, 3, 2]
System.out.printf("reversed list = %s\n", reverse(sortedList1));
// Expected output: [2, 3, 5, 7, 11, 13]
System.out.printf("original list = %s\n", sortedList1);
}
/**
* Returns the minimum element in {@code list}
*
* @param list
* An integer list
*/
private int min(List<Integer> list) {
return 0;
}
/**
* Returns the maximum element in {@code list}
*
* @param list
* An integer list
*/
private int max(List<Integer> list) {
return 0;
}
/**
* Returns the sum of the elements in {@code list}
*
* @param list
* An integer list
*/
private int sum(List<Integer> list) {
int sum = 0;
for (int i : list) {
sum += i;
}
return sum;
}
/**
* Calculates the average value of all elements in {@code list}.
*
* @param list
* An integer list
*/
private double average(List<Integer> list) {
return 0.0;
}
/**
* Merges two lists
*
* @param list1
* An integer list
* @param list2
* Another integer list
*/
private List<Integer> merge(List<Integer> list1, List<Integer> list2) {
return null;
}
/**
* Returns a new instance of {@code List<Integer>} containing all elements
* from {@code list} in reverse-order. The original {@code list} remains
* unchanged.
*
* @param list
* An integer list
*/
private List<Integer> reverse(List<Integer> list) {
return null;
}
/**
* Entry point of the program
*
* @param args
* Runtime arguments
*/
public static void main(String[] args) {
new Exercise1().execute();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment