CS174: OOP - Drills - Array 3 Sort
Developed by Professor Tralie and Professor Mongan.
Exercise Goals
The goals of this exercise are:- To do proper array indexing
- To allocate and populate new arrays
- To use conditional statements appropriately
- To properly define methods
sort3Elements
that takes in 3 integers and returns a 3-element array containing those integers in sorted order. For instance, if it receives the integers 7, 1, and 3, it should return the array {1, 3, 7}
Enter your Ursinus netid before clicking run. This is not your ID number or your email. For example, my netid is ctralie
(non Ursinus students can simply enter their name to get this to run, but they won't get an e-mail record or any form of credit)
Netid |
ArrayUtils.java
public class ArrayUtils {
/**
* Prints out the elements of an array, separated by commas
*
* @param arr The array to print
*/
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
if (i < arr.length-1) {
System.out.print(",");
}
}
}
}
Tester.java
public class Tester {
public static void main(String[] args) {
int[] arr1 = ArrayUtils.sort3Elements(7, 3, 1);
int[] arr2 = ArrayUtils.sort3Elements(9, 1, 8);
ArrayUtils.printArray(arr1);
System.out.print(".");
ArrayUtils.printArray(arr2);
}
}
Tester.main(null);