Thursday, November 16, 2017

Java Class Labs



Lab 1

1) Create a method that accepts an int parameter.  Within the method add 1 to the int parameter and print the value.

private static void bumpOneByValue(int number) {
   // TODO add one to the value and print it
}

2) Create a method that accepts an int[] parameter.  Within the method add 1 to the first element in the array and print the first element.

private static void bumpOneByReference(int[] numbers) {
   // TODO add one to the first element in the array and print the element
}

3) Within the public static void main method, add the following:

public static void main(String[] args) {
    int number = 51;
    bumpOneByValue(number);
    System.out.println("By value return from method call = " + number);

    int[] numbers = { 51 };
    bumpOneByReference(numbers);
    System.out.println("By reference return from method call = " + numbers[0]);
}

Run the lab.  You should see the original 51 number stay the same after the call to bumpOneByValue.  You should see the original 51 go to 52 after the call to bumpOneByReference.


Lab 2


String[] states = {"California", "Wyoming", "West Virginia", "Virginia", "Alaska", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Arizona", "Washington", "Arkansas ", "Wisconsin", "Alabama" };

1) Write a program that uses a for loop to loop through the states array and print each state to the console.  Put this print logic in its own method.

2) Sort the array using the Arrays.sort method.  See ( https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html ).

3) Print the sorted array calling the print method you wrote in the first step.


Lab 3

1) Prompt the user to input a state.

Scanner scanner = new Scanner(System.in);
String state = scanner.next();

2) Search the array using the Collections.binarySearch() method and print the returned index.

 - The Collections.binarySearch() method returns the index within the array where the state is located.  If the input is not found in the array, then Collections.binarySearch() prints a negative value.  Print the output from Collections.binarySearch().

- YOU MUST sort your array before calling the binary search.

- The binary search method expects a Collection to supplied to it (not an array).  Because of this, you can use the Arrays.asList() method to convert your array to a Collection.

// states is the array containing the state names
// state is the state name inputed by the user
int index = Collections.binarySearch(Arrays.asList(states), state);
System.out.println(index);
if (index < 0) {
    System.out.println("Not found");
} else {
    System.out.println("Found at " + index + ". " + states[index]);
}