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: 3595073 • 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.

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] = "";
}
}
System.out.println(desks.length);
}
  
  
// 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) {
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());
}
}   
}
  
  
  
  

  

Explanation / Answer

import java.io.*;
import java.util.*;

class Classroom {
private String [][] desks;
private int rowSize = 0;
private int columnSize = 0;
  
public Classroom(int row, int column) {
desks = new String[row][column];
rowSize = row;
columnSize = column;
  
// initialize each element in desks to ""
for (int i = 0; i < rowSize; i++){
for(int j = 0; j < columnSize; j++){
desks[i][j] = "";
}
}
}
  
//check if desk is taken or not
public boolean isDeskTaken(int row, int column) {
  
//return false if row/column is invalid or if desks[row][column] is empty
if(!checkRowValue(row) || !checkColumnValue(column) || desks[row][column].equals(""))
return false;
  
return true;
}
  
// return list of names of students sitting in the specified row
public String getRow(int row) {
  
//return empty string if row is invalid
if(!checkRowValue(row))
return "";
  
String studentListString ="";
  
  
for (int i = 0; i < columnSize; i++){
// if the desk is not empty add student to student list
if(!getStudent(row,i).equals("")){
studentListString += getStudent(row,i) + ",";
}
}
return studentListString;
}
  
//return student present at desk , return empty string if desk is not taken
public String getStudent(int row, int column) {
  
//return student if desk is taken else return empty string
if(isDeskTaken(row,column))
return desks[row][column];
  
return "";
}
  

//place student at row,column , return false if desk already taken
public boolean placeStudent(int row, int column, String studentName) {

//return false if desk already taken
if(isDeskTaken(row, column))
return false;
  
//assign desk to student and return true
desks[row][column] = studentName;
return true;
}
  
//boundary check for row , return true if valid value
public boolean checkRowValue(int row) {
if (row >= rowSize || row < 0) {
return false;
}
return true;
}
  
//boundary check for row , return true if valid value
public boolean checkColumnValue(int column) {
if (column >= columnSize || column < 0) {
return false;
}
return true;
}
}

public class ClassroomTest {
  
public static void main(String[] args) {
Classroom classroom = new Classroom(20,20);
  
//placed 4 students in row 0
System.out.println(classroom.placeStudent(0,0,"Student 1"));
System.out.println(classroom.placeStudent(0,1,"Student 2"));
System.out.println(classroom.placeStudent(0,2,"Student 3"));
System.out.println(classroom.placeStudent(0,3,"Student 4"));
  
//desk taken
System.out.println("Is desk 0,3 take ? : " + classroom.isDeskTaken(0,3));
  
//desk not taken
System.out.println("Is desk 0,5 take ? : " + classroom.isDeskTaken(0,5));
  
  
//get row 0 students
System.out.println("Row 0 Students : " + classroom.getRow(0));
  
//get row 1 students
System.out.println("Row 1 Students : " + classroom.getRow(1));
  
//get student at 0,3
System.out.println("Student at 0,3 is : " + classroom.getStudent(0,3));
  
//get student at 2,3
System.out.println("Student at 2,3 is : " + classroom.getStudent(2,3));
  
  
}
}

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