Complete the code:
package hw4;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;
/*
* This class is used by:
* 1. FindSpacing.java
* 2. FindSpacingDriver.java
* 3. WordGame.java
* 4. WordGameDriver.java
*/
public class WordGameHelperClass {
/*
* Returns true if an only the string s
* is equal to one of the strings in dict.
* Assumes dict is in alphabetical order.
*/
public static boolean inDictionary(String [] dict, String s) {
// TODO Implement using binary search
// Use String's compareTo method to compare
// two strings. Do not run compareTo more than
// once on the same pair of strings during the search --
// instead, run it once and save the result in a variable
// if you need it again.
// You may want to see https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
return false;
}
/*
* Returns true if and only if
* dict is in alphabetical order
*/
public static boolean checkAlphaOrder(String [] dict) {
// TODO replace the "return true" below
// with a check that dict is in alphabetical order
// Use compareTo method of the String class
// (you may want to see https://docs.oracle.com/javase/8/docs/api/java/lang/String.html)
//
// Empty dictionary and one-word dictionary
// are considered okay
return true;
}
/*
* Reads in a dictionary file (one word per line) and checks if it's
* in alphabetical order. Returns null in case of failure.
*/
public static String[] readDictionary (String dictionaryFileName) {
Scanner fileScanner;
// Open the dictionary file
try {
fileScanner = new Scanner (new File (dictionaryFileName));
}
catch (IOException e) {
System.err.println("Unable to open dictionary file. "+e.getMessage());
System.err.println("Currently in directory "+ System.getProperty("user.dir"));
return null;
}
// read the dictionary file
LinkedList<String> prelimDict = new LinkedList<String>();
while (fileScanner.hasNext()) {
prelimDict.add(fileScanner.next().toLowerCase());
}
String [] dict = prelimDict.toArray(new String[0]);
fileScanner.close();
if(checkAlphaOrder(dict)) {
return dict;
}
else {
System.err.println("Error: dictionary not in alphabetical order.");
return null;
}
}
/*
* Returns true if and only if board is a rectangular array:
* that is, board[i].length is the same for every
* i from 0 to board.length-1
*/
public static boolean checkIfRectangle(char [][] board) {
// TODO replace the "return true" below
// with an appropriate check
// Empty board and one-line board
// should be considered rectangles
return true;
}
/*
* Converts a text file into a two-dimensional
* array of characters, where a[i][j] is the
* character in row i from the top (starting at 0)
* and column j from the left (starting at 0).
* Newline characters are not included in the array.
* Checks that the array is rectangular.
* Returns null in case of failure.
*/
public static char[][] readBoard (String boardFileName) {
Scanner fileScanner;
// open the board file
try {
fileScanner = new Scanner (new File (boardFileName));
}
catch (IOException e) {
System.out.println("Unable to open board file "+e.getMessage());
System.out.println("Currently in directory "+ System.getProperty("user.dir"));
return null;
}
// read the board file
LinkedList<char[]> prelimBoard = new LinkedList<char[]>();
while (fileScanner.hasNext()) {
prelimBoard.add(fileScanner.next().toLowerCase().toCharArray());
}
char [][] board = prelimBoard.toArray(new char[0][]);
fileScanner.close();
if(checkIfRectangle(board)) {
return board;
}
else {
System.err.println("Error: board is not rectangular.");
return null;
}
}
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;
public class HW4IO {
/*
* Returns true if an only the string s is equal to one
of the strings in dict. Assumes dict is in
* alphabetical order.
*/
public static boolean inDictionary(String[] dict, String s) {
int start = 0;
int end = dict.length;
int temp = 0;
int mid = 0;
while (start < dict.length) {
temp = mid;
mid = (start + end) / 2;
if (s.compareTo(dict[mid]) == 0)
{
return true;
} else if (s.compareTo(dict[mid])
> 0) {
start = mid + 1;
if (mid == temp) {
return
false;
}
} else if (s.compareTo(dict[mid])
< 0) {
end = mid - 1;
if (mid == temp) {
return
false;
}
}
}
return false;
}
/*
* Returns true if and only if dict is in alphabetical
order
*/
public static boolean checkAlphaOrder(String[] dict) {
for (int i = 0; i < dict.length - 1; i++)
{
if (dict[i].compareTo(dict[i + 1])
> 0) {
return false;
}
}
return true;
}
/*
* Reads in a dictionary file (one word per line) and
checks if it's in alphabetical order.
* Returns null in case of failure.
*/
public static String[] readDictionary(String dictionaryFileName)
{
Scanner fileScanner;
// Open the dictionary file
try {
fileScanner = new Scanner(new
File(dictionaryFileName));
} catch (IOException e) {
System.err.println("Unable to open
dictionary file. " + e.getMessage());
System.err.println("Currently in
directory " + System.getProperty("user.dir"));
return null;
}
// read the dictionary file
LinkedList<String> prelimDict = new
LinkedList<String>();
while (fileScanner.hasNext()) {
prelimDict.add(fileScanner.next().toLowerCase());
}
String[] dict = prelimDict.toArray(new
String[0]);
fileScanner.close();
if (checkAlphaOrder(dict)) {
return dict;
} else {
System.err.println("Error:
dictionary not in alphabetical order.");
return null;
}
}
/*
* Returns true if and only if board is a rectangular
array: that is, board[i].length is the same
* for every i from 0 to board.length-1
*/
public static boolean checkIfRectangle(char[][] board) {
for (int i = 0; i < board.length - 1; i++)
{
if (board[i].length > board[i +
1].length || board[i].length < board[i + 1].length) {
return false;
}
}
return true;
}
/*
* Converts a text file into a two-dimensional array of
characters, where a[i][j] is the character
* in row i from the top (starting at 0) and column j
from the left (starting at 0). Newline
* characters are not included in the array. Checks
that the array is rectangular. Returns null in
* case of failure.
*/
public static char[][] readBoard(String boardFileName) {
Scanner fileScanner;
// open the board file
try {
fileScanner = new Scanner(new
File(boardFileName));
} catch (IOException e) {
System.out.println("Unable to open
board file " + e.getMessage());
System.out.println("Currently in
directory " + System.getProperty("user.dir"));
return null;
}
// read the board file
LinkedList<char[]> prelimBoard = new
LinkedList<char[]>();
while (fileScanner.hasNext()) {
prelimBoard.add(fileScanner.next().toLowerCase().toCharArray());
}
char[][] board = prelimBoard.toArray(new
char[0][]);
fileScanner.close();
if (checkIfRectangle(board)) {
return board;
} else {
System.err.println("Error: board is
not rectangular.");
return null;
}
}
}
Complete the code: package hw4; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.Scanner; /* * This...
Complete code and answer question in comments: Package hw4; import java.util.ArrayList; public class WordGame { /* * Returns all strings that appear * as a consecutive horizontal or vertical sequence of letters * (left-right, right-left, up-down, or down-up) * in the array board and also appear in dict. * Note that the same word may appear multiple times * on the board, and will then be multiple times in * the returned array. * * dict is assumed to be...
Complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * This returns a string containing the char ch, count times. * For example calling with 'a', 5 would return: * "aaaaa" * * @param ch The character to repeat. * @param count The number of the character. * @return A string with count number of ch. */ public static String charToString(char ch, int count) { return null; //TODO }
complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Picks the first unguessed word to show. * Updates the guessed array indicating the selected word is shown. * * @param wordSet The set of words. * @param guessed Whether a word has been guessed. * @return The word to show or null if all have been guessed. */ public static String pickWordToShow(ArrayList<String> wordSet, boolean []guessed) { return null;...
complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Picks the first unguessed word to show. * Updates the guessed array indicating the selected word is shown. * * @param wordSet The set of words. * @param guessed Whether a word has been guessed. * @return The word to show or null if all have been guessed. */ public static String pickWordToShow(ArrayList<String> wordSet, boolean []guessed) { return null; //TODO...
PLEASE COMPLETE THE CODES. package javaprogram; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; /** * Movie class definition. * * @author David Brown * @version 2019-01-22 */ public class Movie implements Comparable { // Constants public static final int FIRST_YEAR = 1888; public static final String[] GENRES = { "science fiction", "fantasy", "drama", "romance", "comedy", "zombie", "action", "historical", "horror", "war" }; public static final int MAX_RATING = 10; public static final int MIN_RATING = 0; /** * Converts a string of...
complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Finds the specified set of words in the specified file and returns them * as an ArrayList. This finds the specified set in the file which is on the * line number of the set. The first line and set in the file is 1. * * This returns an ArrayList with the keyword first, then : and then followed...
import java.util.Arrays; import java.util.Random; import java.util.Scanner; /** * TODO Write a summary of the role of this class in the * MasterMind program. * * @author TODO add your name here */ public class MasterMind { /** * Prompts the user for a value by displaying prompt. * Note: This method should not add a new line to the output of prompt. * * After prompting the user, the method will consume an entire * line of input while reading...
Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...
package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values * * Complete each function below. * Write each function as a separate recursive definition (do not use more than one helper per function). * Depth of root==0. * Height of leaf==0. * Size of empty tree==0. * Height of empty tree=-1. * * TODO: complete the functions in this file. * DO NOT change the Node class. * DO NOT change the name...
Please make the following code modular. Therefore contained in a package with several classes and not just one. import java.util.*; public class Q { public static LinkedList<String> dogs = new LinkedList<String> (); public static LinkedList<String> cats = new LinkedList<String> (); public static LinkedList<String> animals = new LinkedList<String> (); public static void enqueueCats(){ System.out.println("Enter name"); Scanner sc=new Scanner(System.in); String name = sc.next(); cats.addLast(name); animals.addLast(name); } ...