Can you correct this java code? I get a errors when i compile it. import java.ut
ID: 3749866 • Letter: C
Question
Can you correct this java code? I get a errors when i compile it.
import java.util.concurrent.*;
public class Office {
public static void main(String[] args) {
// ArrayBlock size=5 (number of office chairs)
BlockingQueue<Student> sharedQueue = new ArrayBlockingQueue<>(5);
// Professor and StudentScheduler instance creation
StudentScheduler studentScheduler = new StudentScheduler(sharedQueue);
Professor professor = new Professor(sharedQueue);
// set thread priority
studentScheduler.setPriority(Thread.MAX_PRIORITY);
professor.setPriority(Thread.MAX_PRIORITY);
// start threads
studentScheduler.start();
professor.start();
try
{
// studentScheduler thread completion standby
studentScheduler.join();
// professor thread completion standby
professor.join();
// if studentScheduler thread not active, print exit message
if (!studentScheduler.isAlive()) {
System.out.println("Professor has locked up and left the building...");
}
}
catch (InterruptedException err) {
err.printStackTrace();
}
}
}
---------------------------------------------------------------------------------------------------------------
import java.util.concurrent.*;
public class Professor extends Thread {
// ArrayBlocking instance creation
private BlockingQueue<Student> sharedQueue;
// shared queue creation
public Professor(BlockingQueue<Student> sharedQueue) {
this.sharedQueue = sharedQueue;
}
// run() method
public void run() {
try {
// declare variables
Student student;
Boolean flag = Boolean.TRUE;
int recObject = 0;
// shared queue message consumption
while (true) {
// if Student queue empty and bool=true, professor plays Halo...
// even though Battlefield is more fun
if (sharedQueue.isEmpty() && flag == Boolean.TRUE) {
System.out.println("Professor starts playing Halo... ");
flag = Boolean.FALSE;
}
else
{
// remove Student from queue; add to receiving object
student = sharedQueue.take();
recObject++;
System.out.println("Professor starts meeting with " + student.getStudName());
sleep(student.getTimerAmt());
flag = Boolean.TRUE;
}
if (recObject == 301) {
break;
}
}
}
catch (InterruptedException err) {
err.printStackTrace();
}
}
}
-------------------------------------------------------------------------------------------------
public class Student {
// declare variables
private String studName;
private int timerAmt;
// Student set method
public void setStudName(String studName) {
this.studName = studName;
}
// Student get method
public String getStudName() {
return studName;
}
// Timer set method
public void setTimerAmt(int timerAmt) {
this.timerAmt = timerAmt;
}
// Timer get method
public int getTimerAmt() {
return timerAmt;
}
}
---------------------------------------------------------------------------------------------
import java.util.concurrent.*;
class InsObject {
// insert Student object into queue
public void insert(BlockingQueue<Student> sharedQueue) {
for (int i=0; i <= 300; i++) {
// random value between 1 and 5000 for timer
int randomNum = ThreadLocalRandom.current().nextInt(1, 5000 + 1);
// create Student object, set name, and set timer;
Student student = new Student();
student.setStudName("Student_" +i);
student.setTimerAmt(i + randomNum);
try
{
Thread.sleep(15);
// put Student into queue
sharedQueue.put(student);
System.out.println("Meanwhile, Student_" +i + " sits down in the waiting area...");
}
catch (InterruptedException err)
{
err.printStackTrace();
}
}
}
}
public class StudentScheduler extends Thread {
// ArrayBlocking queue object
private final BlockingQueue<Student> sharedQueue;
// InsObject class object
InsObject insObject = new InsObject();
// StudentScheduler constructor
public StudentScheduler(BlockingQueue<Student> sharedQueue) {
super();
this.sharedQueue = sharedQueue;
}
// run() method at start of thread
public void run() {
// synchronize insObject with synchronized block
synchronized (insObject) {
insObject.insert(sharedQueue);
}
}
}
Explanation / Answer
The code is not producing any compilation error. Out put from the same code which is shared in the question is written below. Please check if a have created classes with the name Office, Professor, Student and StudentScheduler under same package before compilation.
Output from the code given in the question is as follows
-------------------------------------------------------------------------
Professor starts playing Halo...
Meanwhile, Student_0 sits down in the waiting area...
Professor starts meeting with Student_0
Meanwhile, Student_1 sits down in the waiting area...
Meanwhile, Student_2 sits down in the waiting area...
Meanwhile, Student_3 sits down in the waiting area...
Meanwhile, Student_4 sits down in the waiting area...
Meanwhile, Student_5 sits down in the waiting area...
Meanwhile, Student_6 sits down in the waiting area...
Professor starts meeting with Student_1
Meanwhile, Student_7 sits down in the waiting area...
Professor starts meeting with Student_2
Professor starts meeting with Student_3
Meanwhile, Student_8 sits down in the waiting area...
Meanwhile, Student_9 sits down in the waiting area...
Professor starts meeting with Student_4
Professor starts meeting with Student_5
Meanwhile, Student_10 sits down in the waiting area...
Professor starts meeting with Student_6
Meanwhile, Student_11 sits down in the waiting area...
Professor starts meeting with Student_7
Meanwhile, Student_12 sits down in the waiting area...
Professor starts meeting with Student_8
Meanwhile, Student_13 sits down in the waiting area...
Professor starts meeting with Student_9
Meanwhile, Student_14 sits down in the waiting area...
Meanwhile, Student_15 sits down in the waiting area...
Professor starts meeting with Student_10
Meanwhile, Student_16 sits down in the waiting area...
Professor starts meeting with Student_11
Professor starts meeting with Student_12
Meanwhile, Student_17 sits down in the waiting area...
Meanwhile, Student_18 sits down in the waiting area...
Professor starts meeting with Student_13
Professor starts meeting with Student_14
Meanwhile, Student_19 sits down in the waiting area...
Meanwhile, Student_20 sits down in the waiting area...
Professor starts meeting with Student_15
Meanwhile, Student_21 sits down in the waiting area...
Professor starts meeting with Student_16
Professor starts meeting with Student_17
Meanwhile, Student_22 sits down in the waiting area...
Meanwhile, Student_23 sits down in the waiting area...
Professor starts meeting with Student_18
Professor starts meeting with Student_19
Meanwhile, Student_24 sits down in the waiting area...
Professor starts meeting with Student_20
Meanwhile, Student_25 sits down in the waiting area...
Professor starts meeting with Student_21
Meanwhile, Student_26 sits down in the waiting area...
Professor starts meeting with Student_22
Meanwhile, Student_27 sits down in the waiting area...
Meanwhile, Student_28 sits down in the waiting area...
Professor starts meeting with Student_23
Meanwhile, Student_29 sits down in the waiting area...
Professor starts meeting with Student_24
Professor starts meeting with Student_25
Meanwhile, Student_30 sits down in the waiting area...
Professor starts meeting with Student_26
Meanwhile, Student_31 sits down in the waiting area...
Meanwhile, Student_32 sits down in the waiting area...
Professor starts meeting with Student_27
Meanwhile, Student_33 sits down in the waiting area...
Professor starts meeting with Student_28
Professor starts meeting with Student_29
Meanwhile, Student_34 sits down in the waiting area...
Professor starts meeting with Student_30
Meanwhile, Student_35 sits down in the waiting area...
Professor starts meeting with Student_31
Meanwhile, Student_36 sits down in the waiting area...
Meanwhile, Student_37 sits down in the waiting area...
Professor starts meeting with Student_32
Professor starts meeting with Student_33
Meanwhile, Student_38 sits down in the waiting area...
Meanwhile, Student_39 sits down in the waiting area...
Professor starts meeting with Student_34
Professor starts meeting with Student_35
Meanwhile, Student_40 sits down in the waiting area...
Professor starts meeting with Student_36
Meanwhile, Student_41 sits down in the waiting area...
Professor starts meeting with Student_37
Meanwhile, Student_42 sits down in the waiting area...
Professor starts meeting with Student_38
Meanwhile, Student_43 sits down in the waiting area...
Meanwhile, Student_44 sits down in the waiting area...
Professor starts meeting with Student_39
Meanwhile, Student_45 sits down in the waiting area...
Professor starts meeting with Student_40
Professor starts meeting with Student_41
Meanwhile, Student_46 sits down in the waiting area...
Meanwhile, Student_47 sits down in the waiting area...
Professor starts meeting with Student_42
Professor starts meeting with Student_43
Meanwhile, Student_48 sits down in the waiting area...
Meanwhile, Student_49 sits down in the waiting area...
Professor starts meeting with Student_44
Professor starts meeting with Student_45
Meanwhile, Student_50 sits down in the waiting area...
Meanwhile, Student_51 sits down in the waiting area...
Professor starts meeting with Student_46
Professor starts meeting with Student_47
Meanwhile, Student_52 sits down in the waiting area...
Professor starts meeting with Student_48
Meanwhile, Student_53 sits down in the waiting area...
Professor starts meeting with Student_49
Meanwhile, Student_54 sits down in the waiting area...
Professor starts meeting with Student_50
Meanwhile, Student_55 sits down in the waiting area...
Professor starts meeting with Student_51
Meanwhile, Student_56 sits down in the waiting area...
Professor starts meeting with Student_52
Meanwhile, Student_57 sits down in the waiting area...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.