Week 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
import java.util.ArrayList;
public class WordChecker
{
/**
* Initialized in the constructor and contains no null elements.
*/
private ArrayList<String> wordList;
/**
* Constructor that initializes wordList.
* @param words the list of words for this WordChecker
*/
public WordChecker(ArrayList<String> words)
{
wordList = words;
}
/**
* Returns true if each element of wordList (except the first) contains
* the previous element as a substring and returns false otherwise,
* as described in part (a).
*
* Precondition: wordList contains at least two elements.
* Postcondition: wordList is unchanged.
*/
public boolean isWordChain()
{
// TODO: Implement part (a)
return false; // placeholder return
}
/**
* Returns an ArrayList<String> based on strings from wordList that start
* with target, as described in part (b). Each element of the returned ArrayList
* has had the initial occurrence of target removed.
*
* Postconditions: wordList is unchanged.
* Items appear in the returned list in the same order as they appear in wordList.
*
* @param target the substring to match at the start of each word
* @return an ArrayList of words with the initial occurrence of target removed
*/
public ArrayList<String> createList(String target)
{
// TODO: Implement part (b)
return new ArrayList<String>(); // placeholder return
}
/**
* Test the WordChecker class within the same file.
*/
public static void main(String[] args)
{
// Sample data
ArrayList<String> words = new ArrayList<>();
words.add("an");
words.add("band");
words.add("band");
words.add("abandon");
// Create a WordChecker object
WordChecker checker = new WordChecker(words);
// Test isWordChain (part a)
System.out.println("isWordChain? " + checker.isWordChain());
// Test createList (part b)
System.out.println("createList(\"ban\"): " + checker.createList("ban"));
}
}
SpellCheckerSearch.java
import java.io.*;
import java.nio.file.*;
import java.util.*;
public class SpellCheckerSearch
{
private ArrayList<String> dictionary;
/* Constructor populates the dictionary ArrayList from the file dictionary.txt*/
public SpellCheckerSearch() throws IOException
{
List<String> lines = Files.readAllLines(Paths.get("dictionary.txt"));
dictionary = new ArrayList<String>(lines);
}
/**
* Write a linearSearch(word) method that finds a word
* in the ArrayList dictionary. It should also keep
* a count of the number of words checked.
*
* @param String word to be found in elements.
* @return a count of how many words checked before returning.
*/
public int linearSearch(String word)
{
return -1;
}
/**
* Write a binarySearch(word) method that finds the word
* in the ArrayList dictionary. It should also keep
* a count of the number of words checked.
*
* @param String word to be found in elements.
* @return a count of how many words checked before returning.
*/
public int binarySearch(String word)
{
return -1;
}
public void print() {
System.out.println("index\tword");
for (int i = 0; i < dictionary.size(); i++) {
System.out.println(i + "\t" + dictionary.get(i));
}
}
public static void main(String[] args) throws IOException
{
SpellCheckerSearch checker = new SpellCheckerSearch();
checker.print();
String word = "zebra"; // example
int i = checker.linearSearch(word);
System.out.println("Linear search steps for " + word + " = " + i);
int count = checker.binarySearch(word);
System.out.println("Binary search steps for " + word + " = " + count);
}
}
dictionary.txt
apple
banana
cat
dog
elephant
fish
grape
house
igloo
jungle
kangaroo
lemon
monkey
nest
orange
penguin
queen
rabbit
snake
tiger
umbrella
violin
whale
xylophone
yacht
zebra
Last updated
Was this helpful?