Please convert this C++ code into Java Langauge with create the class of GUI. Th
ID: 3594464 • Letter: P
Question
Please convert this C++ code into Java Langauge with create the class of GUI. Thanks
#include <iostream>
#include <string>
using namespace std;
int Guess;
int Total;
class Question
{
private:
string Question_Text;
string Answer_1;
string Answer_2;
string Answer_3;
string Answer_4;
int Correct_Answer;
int Question_Score;
public:
void setValues (string,string,string,string,string, int, int);
void askQuestion();
};
int main()
{
cout<<"Welcome to the Quiz Master"<<endl;
cout<<"Synonym & Antonym"<<endl;
cout<<"INSRUCTION"<<endl;
cout<<"*********************************************************"<<endl;
cout<<"* Choose one of the possible answer out *"<<endl;
cout<<"* of four.You have to get at least 70 or *"<<endl;
cout<<"* above to pass the quiz. *"<<endl;
cout<<"*********************************************************"<<endl;
cout<<"Press ENTER to start the quiz"<<endl;
cin.get();
string Name;
cout<<"What is your name?"<<endl;
cin>> Name;
string Respond;
cout<<"Are you ready to take the quiz, "<<Name<<"? Yes/No?"<<endl;
cin>>Respond;
if (Respond == "yes")
{
cout<<endl;
cout<<"Good Luck!"<<endl;
cout<<endl;
}
else if (Respond == "no")
{
cout<<"Goodbye!"<<endl;
return 0;
}
Question q1;
Question q2;
Question q3;
Question q4;
Question q5;
Question q6;
Question q7;
Question q8;
Question q9;
Question q10;
q1.setValues("what is the synonym of stupid?",
"dumb",
"smart",
"clever",
"genious",
1,
10);
q2.setValues("what is the synonym of beautiful?",
"helpful",
"happy",
"ugly",
"pretty",
4,
10);
q3.setValues("what is the synonym of stretch?",
"fetch",
"intense",
"expand",
"tease",
3,
10);
q4.setValues("what is the synonym of happy?",
"negative",
"impossible",
"joyful",
"hateful",
3,
10);
q5.setValues("what is the synonym of good?",
"dream",
"excellent",
"recycle",
"mean",
2,
10);
q6.setValues("what is the antonym of dark?",
"bright",
"green",
"dim",
"cloudy",
1,
10);
q7.setValues("what is the antonym of sucessful?",
"stright",
"pass",
"fullfil",
"fail",
4,
10);
q8.setValues("what is the antonym of tight?",
"tiny",
"small",
"loose",
"stress",
3,
10);
q9.setValues("what is the antonym of advantage?",
"benefit",
"disadvantage",
"favor",
"improvement",
2,
10);
q10.setValues("what is the antonym of strong?",
"hard",
"soft",
"weak",
"power",
3,
10);
q1.askQuestion();
q2.askQuestion();
q3.askQuestion();
q4.askQuestion();
q5.askQuestion();
q6.askQuestion();
q7.askQuestion();
q8.askQuestion();
q9.askQuestion();
q10.askQuestion();
cout<<"Your total score is "<<Total<<" out of 100"<<endl;
cout<<endl;
if(Total >= 70)
{
cout<<"Great, you passed the quiz!"<<endl;
cout<<endl;
cout<<"Congratulation"<<endl;}
else
{
cout<<"Sorry, you failed the quiz"<<endl;
cout<<"Better luck next time!"<<endl;
}
return 0;
}
void Question::setValues(string q, string a1, string a2, string a3, string a4, int ca, int qs)
{
Question_Text = q;
Answer_1 = a1;
Answer_2 = a2;
Answer_3 = a3;
Answer_4 = a4;
Correct_Answer = ca;
Question_Score = qs;
}
void Question:: askQuestion()
{
cout<<endl;
cout<< Question_Text <<endl;
cout<<"1."<<Answer_1<<endl;
cout<<"2."<<Answer_2<<endl;
cout<<"3."<<Answer_3<<endl;
cout<<"4."<<Answer_4<<endl;
cout<<endl;
cout<<"what is your answer?"<<endl;
cin>>Guess;
if (Guess == Correct_Answer)
{
cout<<endl;
cout<<"Correct!"<<endl;
Total = Total + Question_Score;
}
else
{
cout<<endl;
cout<<"Incorect!"<<endl;
cout<<"The correct answer is "<< Correct_Answer<<"."<<endl;
}
}
Explanation / Answer
First create the java class for Question and save it in Question.java file:
import javax.swing.JOptionPane;
public class Question {
private String question_Text;
private String answer_1;
private String answer_2;
private String answer_3;
private String answer_4;
private int correct_Answer;
private int question_Score;
/*Set the values through this Constructor*/
public Question (String question_Text,String answer_1,String answer_2,String answer_3,String answer_4, int correct_Answer, int question_Score){
this.question_Text = question_Text;
this.answer_1 = answer_1;
this.answer_2 = answer_2;
this.answer_3 = answer_3;
this.answer_4 = answer_4;
this.correct_Answer = correct_Answer;
this.question_Score = question_Score;
}
int askQuestion(){
int guess = Integer.parseInt(JOptionPane.showInputDialog("<html>"+question_Text+"<br>1. "+answer_1+"<br>2. "+answer_2+"<br>3. "+answer_3+"<br>4. "+answer_4+"<br>What is your answer?</htm>"));
if(guess == correct_Answer){
JOptionPane.showMessageDialog(null, "Correct!");
return question_Score;
}else{
JOptionPane.showMessageDialog(null, "<html><center>Incorect!<br>The correct answer is "+correct_Answer+"</center></html>");
return 0;
}
}
}
Now create the java class for GUI and save it in GUI.java file:
import javax.swing.JOptionPane;
public class GUI {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "<html><center>Welcome to the Quiz Master<br>Synonym & Antonym<br>INSRUCTION<br>"+
"*********************************************************<br>Choose one of the possible answer out<br>"+
"of four.You have to get at least 70 or<br>above to pass the quiz."+
"<br>*********************************************************</center></html>");
String name = JOptionPane.showInputDialog("What is your name?");
int choice = JOptionPane.showConfirmDialog(null, "Are you ready to take the quiz, "+name+"?");
if(choice == JOptionPane.YES_OPTION){
JOptionPane.showMessageDialog(null, "Good Luck!");
int total=0;
Question q1 = new Question("what is the synonym of stupid?","dumb","smart","clever","genious",1,10);
total += q1.askQuestion();
Question q2 = new Question("what is the synonym of beautiful?","helpful","happy","ugly","pretty",4,10);
total += q2.askQuestion();
Question q3 = new Question("what is the synonym of stretch?","fetch","intense","expand","tease",3,10);
total += q3.askQuestion();
Question q4 = new Question("what is the synonym of happy?","negative","impossible","joyful","hateful",3,10);
total += q4.askQuestion();
Question q5 = new Question("what is the synonym of good?","dream","excellent","recycle","mean",2,10);
total += q5.askQuestion();
Question q6 = new Question("what is the antonym of dark?","bright","green","dim","cloudy",1,10);
total += q6.askQuestion();
Question q7 = new Question("what is the antonym of sucessful?","stright","pass","fullfil","fail",4,10);
total += q7.askQuestion();
Question q8 = new Question("what is the antonym of tight?","tiny","small","loose","stress",3,10);
total += q8.askQuestion();
Question q9 = new Question("what is the antonym of advantage?","benefit","disadvantage","favor","improvement",2,10);
total += q9.askQuestion();
Question q10 = new Question("what is the antonym of strong?","hard","soft","weak","power",3,10);
total += q10.askQuestion();
if(total >= 70){
JOptionPane.showMessageDialog(null, "<html><center>Your total score is "+total+" out of 100<br>"+
"Great, you passed the quiz!<br>Congratulation</html></center>");
}else{
JOptionPane.showMessageDialog(null, "<html><center>Your total score is "+total+" out of 100<br>"+
"Sorry, you failed the quiz<br>Better luck next time!</html></center>");
}
}else{
JOptionPane.showMessageDialog(null, "Goodbye!");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.