With the below MyArraylist.java and Test.java , write two new methods for the MyArrayList. The first method
Question:
With the below MyArraylist.java and Test.java , write two new methods for the MyArrayList.
The first method is called checkChar(). This method will receive a 5 letter word and return an array list with characters of your own choosing. If for example you decide:
A = valid characters and in its correct position
B = valid characters but not in its correct position
C = Not a valid character.
If the word to be guest is 'admin' and the word entered is 'atlas' the returned array should be [A,C,C,B,C]
Please make sure to tell the meaning of the chars to the user so that he will know what they mean.
The second method is a small method called checkWin() and need to return True if all the letter inthe array is valid and in its correct position.
Complete the test program so that you have a working game!
public class MyArrayList { private int size; // Number of elements in the list private E[] data; /** Add an empty list */ public MyArrayList() { data = (E[])new Object[100];// cannot create array of generics size = 0; // Number of elements in the list } public void add(int index, E e) { // Ensure the index is in the right range if (index < 0 || index > size) throw new IndexOutOfBoundsException ("Index: " + index + ", Size: " + size); // Move the elements to the right after the specified index for (int i = size - 1; i >= index; i--) data[i + 1] = data[i]; // Insert new element to data[index] data[index] = e; // Increase size by 1 size++; } public boolean contains(Object e) { for (int i = 0; i < size; i++) if (e.equals(data[i])) return true; return false; } public E get(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException ("Index: " + index + ", Size: " + size); return data[index]; } public E remove(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException ("Index: " + index + ", Size: " + size); E e = data[index]; // Shift data to the left for (int j = index; j < size - 1; j++) data[j] = data[j + 1]; data[size - 1] = null; // This element is now null // Decrement size size--; return e; } public String toString() { String result=" "; for (int i = 0; i < size; i++) { result+= data[i]; if (i < size - 1) result+=", "; } return result.toString() + "]"; } public int size() { return size; } public boolean sortList() { E hold; for (int i = 0; i < size-1; i++) { for (int j = 0; j0) { hold= data[j+1]; data[j+1]=data[j]; data[j]=hold; } } } return true; } }
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.Scanner; public class Test { public static void main(String arg[]) { Scanner in = new Scanner(System.in); //Add a new array list with default word. //Add a new array list to store returned list. String wordE, wordFF; //Verables to store the word entered and the word from the list. File f1=new File("input.txt"); Boolean valid = false; for(int x=0;x<5;x++)//run 5 time user get 5 guesses. { System.out.println("Guess the five-letter word."); wordE = in.nextLine(); if(wordE.length()==5) //Check if word entered is a 5 letter word. { try{ //Open the file to read the words in the list. FileReader fr = new FileReader(f1); BufferedReader br = new BufferedReader(fr); while((wordFF=br.readLine())!=null) { if(wordFF.equals(wordE))//check if the word entered is in the list { valid = true; } } if(valid) { //Call checkChar to check the validity of the chars and store the info. } fr.close(); }catch (IOException e) { System.out.println("File Read Error");} }else valid = false; System.out.println("The entered word is a valid 5 letter word: " + valid); if(valid) { //display the feedback of the entered word //display the result of the feedback (Winner, Try again, Looser) } valid = false; output.clear(); } } }
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill