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

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 classs 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.

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 students 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.

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 ""

for (int i = 0; i < row; i++){

for(int j = 0; j < column; j++){

desks[i][j] = "";

}

}

System.out.println(desks.length);

}

public boolean isDeskTaken(int row, int column) {

boolean taken = false;

this.checkRowValue(row - 1);

this.checkColumnValue(column - 1);

// detemine if desks[row - 1][column - 1] is taken.

if (!desks[row - 1][column - 1].equals("")){

taken = true;

}

return taken;

}

public String getRow(int row) {

String names = "";

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("")){

names += desks[r][i] + ", ";

}

}

return names;

}

public String getStudent(int row, int column) {

String student = "";

this.checkRowValue(row - 1);

this.checkColumnValue(column - 1);

student = desks[row - 1][column - 1];

return student;

}

public boolean placeStudent(int row, int column, String name) {

boolean placed = 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] = name;

placed = true;

}

return placed;

}

public void checkRowValue(int row) {

if (row > desks.length) {

throw new IllegalArgumentException("row [" + row + "} is to high.");

}

if (row < 0) {

throw new IllegalArgumentException("row [" + row + "] is to low.");

}

}

public void checkColumnValue(int column) {

if (column > desks[0].length) {

throw new IllegalArgumentException("column [" + column + "] is to high.");

}

if (column < 0) {

throw new IllegalArgumentException("column [" + column + "] is to low.");

}

}

}

PLEASE HELP!!!!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!