Create a class called Classroom that represents the desks in a classroom. It sho
ID: 3594583 • 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.
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.
Explanation / Answer
public class Classroom {
String[][] desks;
public Classroom(int row,int column)
{
desks=new String[row][column];
}
public boolean isDeskTaken(int row,int column)
{
if(row<0|| column<0 || row>desks.length || column>desks[row].length)
return false;
else
{
if(this.getStudent(row, column).equals(""))
return false;
else
return true;
}
}
public String getRow(int row)
{
if(row>desks.length)
return "";
else
{
String s="";
int i;
for(i=0;i<desks[row].length-1;i++)
s=s+desks[row][i]+",";
s+=desks[row][i];
return s;
}
}
public String getStudent(int row, int column)
{
if(row<0 || column<0 || row>desks.length || column>desks[row].length)
return "";
else
{
return desks[row][column];
}
}
public boolean placeStudent(int row, int column,
String studentName)
{
if(row<0 ||column<0 || row>desks.length || column>desks[row].length)
return false;
else
{
if(this.isDeskTaken(row, column))
return false;
else
{
desks[row][column]=studentName;
return true;
}
}
}
}
public class ClassroomTest {
public static void main(String[] args) {
Classroom a=new Classroom(2,2);
a.desks[0][0]="A";
a.desks[0][1]="B";
a.desks[1][0]="";
a.desks[1][1]="";//empty desk should be represented by "" string.
System.out.println(a.getRow(0));
System.out.println(a.isDeskTaken(1, 1));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.