Using Java add the specified methods to the letterPuzzle cla

Using Java add the specified methods to the letterPuzzle class and add tester code to the main method

checkRow

checkColumn

scramble

test code in main

public class LetterPuzzle {
private char [][] puzzle;
private char [] letters;
public static Random rg = new Random();
/**
* Default constructor: creates 4x4 puzzle using first 4
* letters of English alphabet
*/
public LetterPuzzle() {
puzzle = new char[4][4];
letters = new char[4];
  
for (int x=0; x<letters.length; x++) {
letters = makeRow();
puzzle[x] = letters;
}
scramble();
}
  
private char[] makeRow()
{
char [] let = new char[4];
for (char c=\'A\'; c<\'E\'; c++)
let[(int)(c-\'A\')] = c;
return let;
  
}
  
/**
* Represents puzzle as 4x4 matrix with row & column headings
* @return puzzle as matrix String
*/
public String toString() {
String s = \"\";
for (int x=0; x<puzzle.length; x++) {
for (int y=0; y<puzzle.length; y++)
s = s + \" \" + puzzle[x][y];
s = s + \"\ \";
}
return s;
}
  
/******TO BE COMPLETED BY STUDENT*****/
/**
* Checks for duplicates in a row
* @param row is the row to be checked: must be a value 0 .. puzzle.length
* @return false if there are duplicates, true if not
* @throws IllegalArgumentException if row is out of bounds
*/
public boolean checkRow(int row) {
  
return true;
}
  
/**
* Checks for duplicates in a column
* @param col is the column to be checked: must be a value 0 .. puzzle.length
* @return false if there are duplicates, true if not
* @throws IllegalArgumentException if row is out of bounds
*/
public boolean checkColumn(int col) {
  
return false;
}
  
/**
* Scrambles puzzle so that letters appear in random order
*/
public void scramble() {

// write your code here
}
  
public static void main (String [] args) {
LetterPuzzle puzz = new LetterPuzzle();
System.out.println(puzz);
// Add code here to test your methods & display results
}
}

Solution

import java.util.*;

public class LetterPuzzle {
private char [][] puzzle;
private char [] letters;
public static Random rg = new Random();
/**
* Default constructor: creates 4x4 puzzle using first 4
* letters of English alphabet
*/
public LetterPuzzle() {
puzzle = new char[4][4];
letters = new char[4];
  
for (int x=0; x<letters.length; x++) {
letters = makeRow();
puzzle[x] = letters;
}
  
scramble();
}
  
private char[] makeRow()
{
char [] let = new char[4];
for (char c=\'A\'; c<\'E\'; c++)
let[(int)(c-\'A\')] = c;
return let;
  
}
  
/**
* Represents puzzle as 4x4 matrix with row & column headings
* @return puzzle as matrix String
*/
public String toString() {
String s = \"\";
for (int x=0; x<puzzle.length; x++) {
for (int y=0; y<puzzle.length; y++)
s = s + \" \" + puzzle[x][y];
s = s + \"\ \";
}
return s;
}
  
/******TO BE COMPLETED BY STUDENT*****/
/**
* Checks for duplicates in a row
* @param row is the row to be checked: must be a value 0 .. puzzle.length
* @return false if there are duplicates, true if not
* @throws IllegalArgumentException if row is out of bounds
*/
public boolean checkRow(int row) {
  
if(row > puzzle.length) throw new IllegalArgumentException(\"Invalid index\");
else
for(int i=0; i<puzzle.length;i++)
{
for(int j=i+1;j<puzzle.length;j++)
{
if(puzzle[row][i]==puzzle[row][j]) return false;
}
}

return true;
  
}
  
/**
* Checks for duplicates in a column
* @param col is the column to be checked: must be a value 0 .. puzzle.length
* @return false if there are duplicates, true if not
* @throws IllegalArgumentException if row is out of bounds
*/
public boolean checkColumn(int col) {
if(col > puzzle.length) throw new IllegalArgumentException(\"Invalid index\");
else
for(int i=0; i<puzzle.length;i++)
{
for(int j=i+1;j<puzzle.length;j++)
{
if(puzzle[i][col]==puzzle[j][col]) return false;
}
}

return true;
}
  
/**
* Scrambles puzzle so that letters appear in random order
*/
public void scramble() {
int p1,p2;
char t;
for(int i=0;i<puzzle.length; i++)
{
for(int j=0;j<puzzle.length/2;j=j+2)
{
p1=rg.nextInt(4);
p2=rg.nextInt(4);

t=puzzle[i][p1];
puzzle[i][p1]=puzzle[i][p2];
puzzle[i][p2]=t;
}
  
  
  
}
}
  
public static void main (String [] args) {
LetterPuzzle puzz = new LetterPuzzle();
if(!puzz.checkRow(0)) System.out.println(\"Row 1 contains duplicate elements\");
System.out.println(puzz);
// Add code here to test your methods & display results
}
}

Using Java add the specified methods to the letterPuzzle class and add tester code to the main method checkRow checkColumn scramble test code in main public cla
Using Java add the specified methods to the letterPuzzle class and add tester code to the main method checkRow checkColumn scramble test code in main public cla
Using Java add the specified methods to the letterPuzzle class and add tester code to the main method checkRow checkColumn scramble test code in main public cla
Using Java add the specified methods to the letterPuzzle class and add tester code to the main method checkRow checkColumn scramble test code in main public cla

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site