I wrote a program in java and I\'m having trouble converting it into C++. Can so
ID: 3541081 • Letter: I
Question
I wrote a program in java and I'm having trouble converting it into C++. Can someone help me convert it into to C++ please! Thank you! This program synchronizes a professor and students during office hours. the professor, takes a nap if no students are around to ask questions; if there are students who want to ask questions, they must synchronize with each other and with the professor so that.
package program.pkg4.cs.pkg4450;
import java.util.Random;
public class Program4CS4450 {
public static void main(String[] args) {
System.out.println("Available for office hours ");
Random randomGenerator = new Random();
Class c = new Class();
Professor p = new Professor();
QAPool qp = new QAPool();
c.setProf(p);
p.setClass(c);
int random = randomGenerator.nextInt(4)+1;
p.setAnswer(qp.Answers(random));
int randomInt = randomGenerator.nextInt(10);
Student s1 = new Student("Tim", qp.Questions(randomInt), c);
int randomInt2 = randomGenerator.nextInt(10);
Student s2 = new Student("Bob", qp.Questions(randomInt2), c);
int randomInt3 = randomGenerator.nextInt(10);
Student s3 = new Student("John", qp.Questions(randomInt3), c);
int randomInt4 = randomGenerator.nextInt(10);
Student s4 = new Student("Steve", qp.Questions(randomInt4), c);
int randomInt5 = randomGenerator.nextInt(10);
Student s5 = new Student("Bill", qp.Questions(randomInt5), c);
int randomInt6 = randomGenerator.nextInt(10);
Student s6 = new Student("Larry", qp.Questions(randomInt6), c);
int randomInt7 = randomGenerator.nextInt(10);
Student s7 = new Student("Mark", qp.Questions(randomInt7), c);
int randomInt8 = randomGenerator.nextInt(10);
Student s8 = new Student("Newton", qp.Questions(randomInt8), c);
s1.start();
s2.start();
s3.start();
s4.start();
s5.start();
s6.start();
s7.start();
s8.start();
}
}
class QAPool
{
String Question;
String Answer;
public String Answers (int n)
{
if (n == 1) Answer = "Yes";
if (n == 2) Answer = "I don't know";
if (n == 3) Answer = "Maybe";
if (n == 4) Answer = "It depends";
return Answer;
}
public String Questions(int n)
{
if (n == 1) Question = "Did I pass?";
if (n == 2) Question = "Can you raise my grage?";
if (n == 3) Question = "Are you teaching next quarter?";
if (n == 4) Question = "Have you graded the midterms?";
if (n == 5) Question = "Can I request for an extension on the project?";
if (n == 6) Question = "Will you be available next week?";
if (n == 7) Question = "Do you accept late work?";
if (n == 8) Question = "Did you just give me an 'F'?!";
if (n == 9) Question = "Can I sit in your other class?";
if (n == 0) Question = "Can I see your answer key?";
return Question;
}
}
class Class
{
Professor prof;
public void setProf(Professor p)
{
prof = p;
}
public synchronized void QuestionEnd(String msg, String name)
{
System.out.println(name + " says : " + msg);
prof.AnswerStart(name);
System.out.println(" ");
}
}
class Student extends Thread
{
String name;
String ques;
Class cl;
Student(String n, String q, Class c)
{
name = n;
ques = q;
cl = c;
}
public void run()
{
String message = ques;
for(int i = 0; i < 2; i++) // ask the same question twice !
{
if (i == 1){
message = "I just want to hear it again. " + ques;
}
cl.QuestionEnd(message, name);
//ask a question and wait or sleep before you ask again.
try
{
Thread.sleep(1500); // sleep for 1.5 sec.
}catch(Exception ex){System.out.println("error : " + ex);}
}
}//run
}
class Professor
{
String Prof = "Professor Simth";
Class cl;
String ans;
public void setAnswer(String n)
{
ans = n;
}
public void setClass(Class c)
{
cl = c;
}
public void AnswerStart(String name)
{
System.out.println(Prof + " says : " + ans + ", " + name);
}
}
Explanation / Answer
no need to change the variables, just write c++ header files and the basic initial commands of c++, after that in java and c++ a little difference in writng code, you can convert easily bu just having a little knowledge of c++
i hope it helps you :)