Question

(IN JAVA) Write a program named Review.java and implement the following methods: public static boolean isValidTicTacToeBoardString(String...

(IN JAVA) Write a program named Review.java and implement the following methods:

public static boolean isValidTicTacToeBoardString(String str)
This method takes as its parameter a String that contains 9 characters representing the status of the Tic Tact Toe game. The String is mixed with X, O, x, o, and other letters. The first three letters represent the first row, letter 4 through letter 6 represent the second row, and the rest for the last row. To be considered as a valid status, the String must meet the following condistions:

1. The absolute difference between the number of Xs and the number of Os must be either 0 or 1
2. There is only one or none player meeting the "win" condition (row, colum, diagonal)

public static char[][] buildTicTactToe2DBoard(String str)

This method takes as its parameter a String that contains 9 characters. The String is mixed with X, O, x, o, and other letters. If the argument string is valid board string, then construct a 2D arrray to represent the board. Use a peroid (.) to replace all other letters in the 2D array. For example, the string "XX9pOoOxR" should return the following 2D array:
X  X  .
.    O  O
O  X   .


public static int countWord(String word, File file)
This method counts and returns the number of occurrence of the word in the file. A data file: wordCOunt.txt is provided for testing.

public static int[] coutingGrades(File file)
This method takes as its parameter a file containing grades for a class, then counts how many grades are in each of the following categories and returns them as a 5-element int array:

[90 - 100]
[80 - 89]
[70 - 79]
[60 - 69]
[Below 60]

This method returns 
Design a java program named GradeCount.java to read data from a gradebook (gradebook.txt contains grades only) and count how many students are in each of the following categories:
90 - 100
80 - 89
70 - 79
60 - 69
Below 60

Here is the sample data file:

82 100 88 90 56 76 67 60 2000
98 65 90 85 67 81 93 86 88B
87 70 88 78 84 76 81 89 

Please note the file may contains some invalid data and your program should discard those invalid data: less than 0 or greater than 100, or non-numerical data.

Once completed, please run Grader.class to test your program and record the result in result.txt under the assignment folder.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answered the first question regarding methods for Tic tac toe game as per Chegg's answering guidelines.

Hope this helps! Comment in case of a doubt.

All the explanation is in the code comments.

Code:

public class Main
{
// 1st required function
public static boolean isValidTicTacToeBoardString(String str) {
  
// first check for the length of the string
if(str.length() != 9)
return false;
  
// now count the Xs and Os
int count_x = 0, count_o = 0;
for(int i=0; i<9; i++) {
  
char ch = str.charAt(i);
  
// for X character
if(ch == 'X' || ch == 'x')
count_x++;
  
// for O character
if(ch == 'O' || ch == 'o')
count_o++;
}
  
// check for absolute difference
if(!(Math.abs(count_o - count_x) == 1 || (count_o - count_x) == 0))
return false;
  
// finally checking for winning conditions
boolean win_x = false, win_o = false;
char ch1, ch2, ch3;
  
// check for the horizontal rows
for(int i=0; i<9; i+=3) {
ch1 = str.charAt(i);
ch2 = str.charAt(i+1);
ch3 = str.charAt(i+2);
  
// all characters match
if(ch1 == ch2 && ch2 == ch3) {
  
if(ch1 == 'X' || ch1 == 'x')
win_x = true;
if(ch1 == 'O' || ch1 == 'o')
win_o = true;
}
}
  
// check for the vertical columns
for(int i=0; i<3; i++) {
ch1 = str.charAt(i);
ch2 = str.charAt(i+3);
ch3 = str.charAt(i+6);
  
// all characters match
if(ch1 == ch2 && ch2 == ch3) {
if(ch1 == 'X' || ch1 == 'x')
win_x = true;
if(ch1 == 'O' || ch1 == 'o')
win_o = true;
}
}
  
// for the 2 diagonals
ch1 = str.charAt(0);
ch2 = str.charAt(4);
ch3 = str.charAt(8);
// all characters match
if(ch1 == ch2 && ch2 == ch3) {
if(ch1 == 'X' || ch1 == 'x')
win_x = true;
if(ch1 == 'O' || ch1 == 'o')
win_o = true;
}
  
ch1 = str.charAt(2);
ch2 = str.charAt(4);
ch3 = str.charAt(6);
// all characters match
if(ch1 == ch2 && ch2 == ch3) {
if(ch1 == 'X' || ch1 == 'x')
win_x = true;
if(ch1 == 'O' || ch1 == 'o')
win_o = true;
}
  
// only one or none should be true => if both are true, invalid
if(win_x && win_o)
return false;
  
return true;
}
  
public static char[][] buildTicTactToe2DBoard(String str) {
  
// first check for valid board
if(!isValidTicTacToeBoardString(str))
return null;
  
// construct a 2D arrray to represent the board
char board[][] = new char[3][3];
  
// loop for all characters
for(int i=0; i<9; i++) {
  
char ch = str.charAt(i);
  
// for X character
if(ch == 'X' || ch == 'x')
board[i/3][i%3] = 'X';
  
// for O character
else if(ch == 'O' || ch == 'o')
board[i/3][i%3] = 'O';
  
else // all other characters
board[i/3][i%3] = '.';
}
  
// return the character board
return board;
}
  
   public static void main(String[] args) {
      
       // sample run
       String str = "XX9pOoOxR";
      
       // call the buildTicTactToe2DBoard() method
       char board[][] = buildTicTactToe2DBoard(str);
      
       // if board is null => invalid str board string
       if(board == null) {
       System.out.println("Invalid TicTacToe Board String");
       } else {
       // print the Board
       for(int i=0; i<3; i++) {
       for(int j=0; j<3; j++) {
       System.out.print(board[i][j] + " ");
       }
       System.out.println();
       }
       }
      
   }
}

Sample run:

For str = "XX9pOoOxR"

Code screenshots:

Add a comment
Know the answer?
Add Answer to:
(IN JAVA) Write a program named Review.java and implement the following methods: public static boolean isValidTicTacToeBoardString(String...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program named Review.java and implement the following two methods: public static int NumberOfElementsLessThanAverage(int[] arr)...

    Write a program named Review.java and implement the following two methods: public static int NumberOfElementsLessThanAverage(int[] arr) that returns the number of elements that are less than the average of all elements of the array. public static int[] countChars(File file), this method reads data from a file and counts the frequency of each alphabetic letters, and returns those numbers as an int array. the data file name is "data.txt"

  • Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO:...

    Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size // TODO: Create first row // TODO: Generate remaining rows // TODO: Display matrix } /** * firstRow * * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return...

  • import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args)...

    import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args) { int[] grades = randomIntArr(10); printIntArray("grades", grades); ArrayList<Integer> indexesF_AL = selectIndexes_1(grades); System.out.println(" indexesF_AL: " + indexesF_AL); int[] indexesF_Arr = selectIndexes_2(grades); printIntArray("indexesF_Arr",indexesF_Arr); } public static int[] randomIntArr(int N){ int[] res = new int[N]; Random r = new Random(0); for(int i = 0; i < res.length; i++){ res[i] = r.nextInt(101); // r.nextInt(101) returns an in in range [0, 100] } return res; } public static void...

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • Write a program that instantiates an array of integers named scores. Let the size of the...

    Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...

  • Java Program 1. Write a class that reads in a group of test scores (positive integers...

    Java Program 1. Write a class that reads in a group of test scores (positive integers from 1 to 100) from the keyboard. The main method of the class calls a few other methods to calculate the average of all the scores as well as the highest and lowest score. The number of scores is undetermined but there will be no more than 50. A negative value is used to end the input loop. import java.util.Scanner; public class GradeStat {...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • By using Java public static methods solve following problem Write a method called vowelCount that accepts...

    By using Java public static methods solve following problem Write a method called vowelCount that accepts a String as its only parameter, and returns an array int[5] containing the count of vowels a,e,i,o,u in that String, using ignoreCase. For example, vowelCount("") returns {0,0,0,0,0}, and for the callvowelCount("Bill Iverson") your method returns {0,1,2,1,0} because there is one 'e' and two 'i' vowels (ignore case) and one 'o' with zero counts for 'a' and 'u' vowels.

  • Submit Chapter7.java with four (4) public static methods as follows: A. Write a method called mostCommon...

    Submit Chapter7.java with four (4) public static methods as follows: A. Write a method called mostCommon that accepts an array of integers as its only parameter, and returns the int that occurs most frequently. Break ties by returning the lower value For example, {1,2,2,3,4,4} would return 2 as the most common int. B. Write mostCommon (same as above) that accepts an array of doubles, and returns the double that occurs most frequently. Consider any double values that are within 0.1%...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT