Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class called Classroom that represents the desks in a classroom. It sho

ID: 3595131 • Letter: C

Question

Create a class called Classroom that represents the desks in a classroom. It should have an instance variable called desks that is a two-dimensional array of Strings. The class’s constructor should take two parameters, an integer row and an integer column, indicating how many rows and columns of desks are in the classroom. It should initialize the instance variable to the specified size, with empty Stings. The class should have the following methods: • public boolean isDeskTaken(int row, int column) that returns true if a student is sitting at the desk, or false if the desk is open.

• public String getRow(int row) that returns a comma separated list of names of the students sitting in the specified row. THE CODE SHOULD USE Arrays.toString method which includes brackets in the String it produces

• public String getStudent(int row, int column) that returns the name of the student sitting at the desk, or an empty string if there desk is open.

• public boolean placeStudent (int row, int column, String studentName) that attempts to place the student at the specified desk. If the desk is not taken, the method should update the instance variable with the student’s name and return true. If the desk is taken, the method should return false.

• All methods should ensure that the specified row and/or column parameter(s) are within the range of rows and columns in the classroom. If they are not, the method should return false or an empty string.

• You can assume that the constructor will be called with positive integers, so no testing is needed there.

Make sure the Classroom class is well documented.

Write a class called ClassroomTest that tests your Classroom class.

Classroom.Java

public class Classroom {
private String [][] desks;
private int numRows = 0;
private int numColumns = 0;
  
public Classroom(int row, int column) {
this.desks = new String[row][column];
  
this.numRows = row;
this.numColumns = column;
  
// fill each element with an empty string ""
// initialize each element in desks to ""
for (int i = 0; i < row; i++){
for(int j = 0; j < column; j++){
desks[i][j] = "";
}
}
}
  
  
// determine true statement if a student is sitting at the desk
// determine false statement if a student is NOT sitting at the desk
public boolean isDeskTaken(int row, int column) {
this.checkRowValue(row - 1);
this.checkColumnValue(column - 1);
String student = this.getStudent(row, column);
boolean taken = true;
  
// detemine if desks[row - 1][column - 1] is taken.
if (student.isEmpty()){
taken = false;
}
return taken;
}
  
// determine a list of names of students sitting in the specified row
public String getRow(int row) {
this.checkRowValue(row - 1);
String students ="";
int r = row - 1;
this.checkRowValue(r);
  
  
for (int i = 0; i < this.numColumns; i++){
// if the desk is not empty let us get the name
if(!desks[r][i].equals("")){
students += desks[r][i] + ",";
}
}
return students;
}
  
  
// determine the name of the student sitting at the desk, or an
// empty string if there desk is open
public String getStudent(int row, int column) {
String student = "";
this.checkRowValue(row - 1);
this.checkColumnValue(column - 1);
return this.desks[row - 1][column - 1];

}
  
// determine the student placement, if desk is not already taken,
// if desk taken, the method should return false.
public boolean placeStudent(int row, int column, String studentName) {
boolean studentPlaced = false;
  
checkRowValue(row - 1);
checkColumnValue(column - 1);
  
// Determine if the desk is empty (desks[row - 1][column - 1] contains "")
// if desk is empty desks[row - 1][column - 1] = name and set placed = true
if(!isDeskTaken(row, column)){
desks[row - 1][column - 1] = studentName;
studentPlaced = true;
}
return studentPlaced;
}
  
public void checkRowValue(int row) {
if (row > desks.length) {
throw new IllegalArgumentException("row [" + (row + 1) + "} is to high.");
}
  
if (row < 0) {
throw new IllegalArgumentException("row [" + (row + 1) + "] is to low.");
}
}
  
public void checkColumnValue(int column) {
if (column > desks[0].length) {
throw new IllegalArgumentException("column [" + (column + 1) + "] is to high.");
}
  
if (column < 0) {
throw new IllegalArgumentException("column [" + (column + 1) + "] is to low.");
}
}
}

ClassroomTest.Java

public class ClassroomTest {
public static void main(String[] args) {
Classroom classroom = new Classroom(5, 4);
  
System.out.println("getRow(5): " + classroom.getRow(5));
System.out.println("isDeskTaken(5,2): " + classroom.isDeskTaken(5, 2));
System.out.println("placeStudent(5,2): " + classroom.placeStudent(5, 2, "Mike"));
System.out.println("isDeskTaken(5,2): " + classroom.isDeskTaken(5, 2));
System.out.println("getStudent(5,2): " + classroom.getStudent(5, 2));
System.out.println("placeStudent(5,2): " + classroom.placeStudent(5, 2, "Bob"));
System.out.println("getStudent(5,2): " + classroom.getStudent(5, 2));
System.out.println("getRow(5): " + classroom.getRow(5));
  
// Place four students with names "Tom n" in row 1
for (int c = 1; c <=4; c++) {
String name = "Tom " + c;
classroom.placeStudent(1, c, name);
}

System.out.println("getRow(1): " + classroom.getRow(1));
  
System.out.println("---- Error checking ----");
  
try {
classroom.getRow(100);
} catch(IllegalArgumentException e) {
System.out.println(e.getMessage());
}
  
try {
classroom.getRow(-200);
} catch(IllegalArgumentException e) {
System.out.println(e.getMessage());
}
  
try {
classroom.getStudent(2, 100);
} catch(IllegalArgumentException e) {
System.out.println(e.getMessage());
}
  
try {
classroom.getStudent(2, -200);
} catch(IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}   
}
  
  

I NEED HELP WITH ARRAYS.toString METHOD, in the getRow method!


  

  

  

Explanation / Answer

A brief descption about Arrays.toString() method:

This method takes SINGLE DIMENTIONAL ARRAY of any valid types(ie. string, integer, boolean arrays) and converts it to the comma seperated strings along with prefixing and suffixing the opening and closing brackets.

Ex: If the array is String[] stringArray = ("A1", "A2", "A3"); then the Arrays.toString(stringArray) would produce the string which will look like :

"[A1, A2, A3]"

Now coming to your question, the updated method will look like:


//GetRow of Students updated
public String getRowUpdated(int row) {
this.checkRowValue(row - 1);
String students ="";
int r = row - 1;
this.checkRowValue(r);
  
students = Arrays.toString(desks[r]); //This will treat desks as single dim array and convert it to string
return students;
}
  

The sample run after modifying the getRow() method is :

getRow(5): [, , , ]
isDeskTaken(5,2): false
placeStudent(5,2): true
isDeskTaken(5,2): true
getStudent(5,2): Mike
placeStudent(5,2): false
getStudent(5,2): Mike
getRow(5): [, Mike, , ]
getRow(1): [Tom 1, Tom 2, Tom 3, Tom 4]
---- Error checking ----
row [100} is to high.
row [-200] is to low.
column [100] is to high.
column [-200] is to low.

==================================================================================

Cheers!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote