Session 18
Source Code
import java.util.*;
public class Week18 {
public static void main(String[] args) {
String myName = ""; // add your name
System.out.println("Week 18" + " - " + myName);
// Menu
System.out.println("Menu");
System.out.println("E1 - Example 1");
System.out.println("Q - Quit");
// setup Scanner
Scanner in = new Scanner(System.in);
System.out.print("Choice: ");
String choice = in.nextLine();
// switch choices
switch (choice) {
case "E1":
System.out.println("Example 1");
example1();
break;
case "E2":
System.out.println("Example 2");
ArrayList<Integer> list1 = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
list1.add(i);
}
list1.add(5);
// list2
ArrayList<Integer> list2 = new ArrayList<>();
for (int i = 0; i <= 5; i++) {
list2.add(i);
}
// result1
// result2
break;
case "E3":
System.out.println("Example 3");
// list3
ArrayList<Double> list3 = new ArrayList<>();
for (int i = 0; i <= 5; i++) {
list3.add((double) i);
}
for (int i = 4; i >= 1; i--) {
list3.add((double) i);
}
// list4
ArrayList<Double> list4 = new ArrayList<>();
for (int i = 0; i <= 10; i++) {
list4.add(Math.random() * 10);
}
// result3
// result4
break;
case "E4":
System.out.println("Example 4");
int[] arr1 = { 3, -2, 9, 38, -23 };
// sequentialSearch Tests
System.out.println("Tests of Array sequentialSearch");
break;
case "E5":
System.out.println("Example 5");
ArrayList<Integer> list5 = new ArrayList<Integer>();
list5.add(3);
list5.add(-2);
list5.add(9);
list5.add(38);
list5.add(-23);
// sequentialSearch ArrayList Tests
System.out.println("Tests of ArrayList sequentialSearch");
break;
case "E6":
System.out.println("Example 6");
String[] arr2 = { "blue", "red", "purple", "green" };
// test when the target is in the array
// test when the target is not in the array
break;
case "E7":
System.out.println("Example 7");
int[] arr3 = { -20, 3, 15, 81, 432 };
// test when the target is in the middle
// test when the target is the first item in the array
// test when the target is in the array - last
// test when the target is not in the array
break;
case "E8":
int[] arr4 = { 3, 86, -20, 14, 40 };
System.out.println("Original: " + Arrays.toString(arr4));
selectionSort(arr4);
System.out.println("Sorted: " + Arrays.toString(arr4));
break;
case "E9":
int[] arr5 = { 3, 86, -20, 14, 40 };
System.out.println("Original: " + Arrays.toString(arr5));
insertionSort(arr5);
System.out.println("Sorted: " + Arrays.toString(arr5));
break;
case "Q":
System.out.println("Quitting..!");
break;
}
}
// example1 method
public static void example1() {
// create ArrayList of 0 - 5
ArrayList<Integer> arr = new ArrayList<Integer>();
for (int i = 0; i < 5; i++) {
arr.add(i);
}
// intention is to remove every other element
for (int i = 0; i < arr.size(); i++) {
if (i % 2 == 0) {
System.out.println("Removing element " + i + " : " + arr.get(i));
arr.remove(i);
}
}
// print the array
System.out.println(arr);
}
// hasConsecutiveDuplicates method
public static boolean hasConsecutiveDuplicates(ArrayList<Integer> list) {
// Compare consecutive elements
// Duplicate found
return false; // otherwise return false
}
// hasDuplicates method
public static boolean hasDuplicates(ArrayList<Double> list) {
// Compare each element with every element after it
// Found a duplicate
// No duplicates found
return false;
}
// sequentialSearch Array
public static int sequentialSearch(int[] elements, int target) {
return -1;
}
// sequentialSearch ArrayList
public static int sequentialSearch(ArrayList<Integer> elements, int target) {
return -1;
}
// sequentialSearch on an Array of Strings
public static int sequentialSearch(String[] elements, String target) {
return -1;
}
// binarySearch method
public static int binarySearch(int[] elements, int target) {
int left = 0;
int right = elements.length - 1;
while (left <= right) {
int middle = (left + right) / 2;
if (target < elements[middle]) {
right = middle - 1;
} else if (target > elements[middle]) {
left = middle + 1;
} else {
return middle;
}
}
return -1;
}
/**
* Sorts an array of integers in ascending order using the selection sort
* algorithm.
*
* @param elements The array of integers to be sorted. The array is modified in
* place,
* and the sorted array is the same as the input array with its
* elements rearranged.
*/
public static void selectionSort(int[] elements) {
for (int j = 0; j < elements.length - 1; j++) {
int minIndex = j;
for (int k = j + 1; k < elements.length; k++) {
if (elements[k] < elements[minIndex]) {
minIndex = k;
}
}
int temp = elements[j];
elements[j] = elements[minIndex];
elements[minIndex] = temp;
System.out.println(Arrays.toString(elements));
}
}
/**
* Sorts an array of integers in ascending order using the insertion sort
*
* @param elements The array of integers to be sorted. The array is modified in
* place,
* meaning the sorted array is the same as the input array with
* its elements rearranged.
*/
public static void insertionSort(int[] elements) {
for (int j = 1; j < elements.length; j++) {
int temp = elements[j];
int possibleIndex = j;
while (possibleIndex > 0 && temp < elements[possibleIndex - 1]) {
elements[possibleIndex] = elements[possibleIndex - 1];
possibleIndex--;
}
elements[possibleIndex] = temp;
System.out.println(Arrays.toString(elements));
}
}
}WordChecker.java
SpellCheckerSearch.java
dictionary.txt
Last updated
Was this helpful?