Workshop
Code challenge 1
You have been provided with two classes. In each of the classes, define some fields that you believe are appropriate and define the characteristics of that class.
Answer
public class Room {
// Fields
private String roomNumber; // Unique identifier for the room
private String type; // Type of room: Lecture Theatre, Tutorial Room, Consultation Area, etc.
private int capacity; // Maximum number of people the room can accommodate
private boolean hasProjector; // Whether the room has a projector
private boolean hasWhiteboard; // Whether the room has a whiteboard
private boolean isAvailable; // Room availability status
// No-argument constructor
public Room() {
this.roomNumber = "Unknown";
this.type = "General";
this.capacity = 30;
this.hasProjector = false;
this.hasWhiteboard = false;
this.isAvailable = true;
}
// Parameterized constructor
public Room(String roomNumber, String type, int capacity, boolean hasProjector, boolean hasWhiteboard, boolean isAvailable) {
this.roomNumber = roomNumber;
this.type = type;
this.capacity = capacity;
this.hasProjector = hasProjector;
this.hasWhiteboard = hasWhiteboard;
this.isAvailable = isAvailable;
}
// Books the room if it is available.
public void bookRoom() {
if (isAvailable) {
isAvailable = false;
System.out.println("Room " + roomNumber + " has been booked.");
} else {
System.out.println("Room " + roomNumber + " is already booked.");
}
}
// Checks if the room is available
public boolean checkIsAvailable() {
return isAvailable;
}
// Displays the details of the room.
public void display() {
System.out.println("Room Number: " + roomNumber);
System.out.println("Type: " + type);
System.out.println("Capacity: " + capacity);
System.out.println("Has Projector: " + hasProjector);
System.out.println("Has Whiteboard: " + hasWhiteboard);
System.out.println("Available: " + isAvailable);
}
public int getCapacity() {
return capacity;
}
public String getRoomNumber() {
return roomNumber;
}
public String getType() {
return type;
}
public boolean hasProjector() {
return hasProjector;
}
public boolean hasWhiteboard() {
return hasWhiteboard;
}
// main
public static void main(String[] args) {
Room room1 = new Room("A101", "Lecture Theatre", 100, true, true, true);
room1.display();
room1.bookRoom();
room1.display();
room1.releaseRoom();
}
// Releases the room, making it available for booking
public void releaseRoom() {
isAvailable = true;
System.out.println("Room " + roomNumber + " is now available.");
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public void setHasProjector(boolean hasProjector) {
this.hasProjector = hasProjector;
}
public void setHasWhiteboard(boolean hasWhiteboard) {
this.hasWhiteboard = hasWhiteboard;
}
public void setRoomNumber(String roomNumber) {
this.roomNumber = roomNumber;
}
public void setType(String type) {
this.type = type;
}
}
Code challenge 2
You have been given a class, which has a few methods defined in it. However, it is missing the fields. Can you identify the fields and include them so the class can execute and provide an output?
Answer
public class Swap
{
//Define the fields here
private int numOne;
private int numTwo;
public static void main(String[] args)
{
Swap objSwap = new Swap();
objSwap.swapNumbers();
}
public void swapNumbers()
{
numOne = 10;
numTwo = 20;
System.out.println("Before Swap:");
System.out.println("numOne = " + numOne + " numTwo = " + numTwo);
int numTemp = numOne;
numOne = numTwo;
numTwo = numTemp;
System.out.println("After Swap:");
System.out.println("numOne = " + numOne + " numTwo = " + numTwo);
}
}