Last active
August 29, 2015 13:56
-
-
Save ericfode/9010242 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Alice Miller | |
// 2/13/14 | |
//Assingment 4, Median and average | |
//Notes: The individual parts should work but the whole thing isn't cohesive. Sorry. | |
import java.util.Scanner; | |
public class ArrayTraversal { | |
public static void main(String[] args) { | |
//Initialize optoin | |
int optoin = 0; | |
while (option != 6){ | |
System.out.println("Array Traversal"); | |
System.out.println("1. Add a number to the array."); | |
System.out.println("2. Display the mean."); | |
System.out.println("3. Display the median."); | |
System.out.println("4. Print the array to the screen."); | |
System.out.println("5. Print the array in reverse order."); | |
System.out.println("6. Quit."); | |
System.out.println("Please select your choice: "); | |
Scanner kb = new Scanner(System.in); | |
int option = kb.nextInt(); | |
int[] numbers; | |
switch (option) { | |
case 1://Add a number to the array | |
int[] array = numbers[]; | |
break; | |
case 2://Display mean | |
double mean = getMean(); | |
break; | |
case 3://Display median | |
int median = getMedian(); | |
break; | |
case 4://Print in order | |
int print = displayLowHigh(); | |
break; | |
case 5://Print in reverse order | |
int print = displayHighLow(); | |
break; | |
case 6://quit | |
break; | |
default: | |
System.out.println("Invalid selection."); | |
} | |
} | |
} | |
////////////////////////////////////////////////////////////////// | |
// TODO This function should take an array and sort it | |
// USE this instead of writing sort in to multiple function | |
////////////////////////////////////////////////////////////////// | |
public static int[] sort(int[] numbers){ | |
} | |
//Add int to array | |
public static int[] addInt (int[] numbers) { | |
//Creating new array | |
int oldLength = numbers.length; | |
int newLength = oldLength + 1; | |
int[] newNumbers = new int[newLength]; | |
//Duplicating numbers | |
for (int i = 0; i < oldLength; i++) { | |
newNumbers[i] = numbers[i]; | |
} | |
System.out.println ("Enter new integer; "); //Asking for new number | |
Scanner input = new Scanner(System.in); | |
newNumbers[newLength - 1]= input.nextInt(); //New number is set at the end of array | |
return newNumbers; | |
} | |
public static void getMean(int[] numbers) {//Get mean | |
double mean = 0;//initialize mean and sum | |
double sum = 0; | |
//Sum up the numbers in a the list | |
for (int index = 0; index < numbers.length; index++) { | |
sum = numbers[index] + sum; | |
} | |
//Get average | |
mean = sum / numbers.length; | |
System.out.printf("The mean is %4.2f.", mean); //Print average | |
} | |
////////////////////////////////////////////////////////// | |
// TODO: Rewrite this using the sort method that you wrote | |
// above | |
// TODO: line 105 only handles *half* of the cases, a | |
// list of numbers still has a median even if it is an odd len | |
////////////////////////////////////////////////////////// | |
public static void getMedian(int[] numbers) { //Get median | |
int key = numbers[0]; | |
for (int index = 1; index < numbers.length; index++) { //Sort | |
int position = index; | |
while (position > 0 && key < numbers[position - 1]) { | |
numbers[position] = numbers[position - 1]; | |
position--; | |
} | |
numbers[position] = key; | |
} | |
int median = 0; //Get median | |
if (numbers.length % 2 == 1) { | |
median = numbers[numbers.length / 2]; | |
System.out.println("Median is " + median); | |
} | |
} | |
////////////////////////////////////////////////////////// | |
// TODO: Rewrite this using the sort method that you wrote | |
// above... It should be about 5 lines | |
////////////////////////////////////////////////////////// | |
public static void displayLowHigh(int[] numbers) { //Display numbers from low to high | |
for (int index = 1; index < numbers.length; index ++) { //Sort | |
int key = numbers[index]; | |
int position = index; | |
while (position > 0 && key < numbers[position - 1]) { | |
numbers[position] = numbers[position -1]; | |
position --; | |
} //end while | |
numbers[position] = key; | |
} | |
for (int index = 1; index < numbers.length; index ++) { //Display numbers from low to high | |
System.out.println(numbers[index]); | |
} | |
} | |
////////////////////////////////////////////////////////// | |
// TODO: Rewrite this using the sort method that you wrote | |
// above... It should be about 5 lines | |
////////////////////////////////////////////////////////// | |
public static void displayHighLow(int[] numbers) { //Display numbers from High to low | |
for (int index = 1; index < numbers.length; index ++) { //Sort | |
int key = numbers[index]; | |
int position = index; | |
while (position > 0 && key < numbers[position - 1]) { | |
numbers[position] = numbers[position -1]; | |
position --; | |
} //end while | |
numbers[position] = key; | |
} | |
for (int index = numbers.length; index > 1; index --) { //Display numbers from low to high | |
System.out.println(numbers[index]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Put comments on the line above what they are relevant to unless the line is less then 20 characters