Java Programming Please help turn the following command prompt program into an i
ID: 3692139 • Letter: J
Question
Java Programming
Please help turn the following command prompt program into an intro level Java GUI program that consists of a single JLabel and JButton.
JLabel will display a welcome message and display random questions and answers when the JButton is pressed.
JButton will begin say Start (this will tell JLabel to display the first question when clicked), change to Answer (this will tell JLabel to display the answer to the current displayed question), and then change to Next Question (this will tell JLabel to display the next question)
After all questions have been answered, the main button should be disabled and a dialog box should ask the user if they want to go through the questions again or not. Yes will restart program and No will exit the program.
The code below runs great in a command prompt, I just need to convert it to a GUI application. The questions are displayed randomly after the user command has been input. If you could comment out my work and add comments to what you added, that would help my development as a programmer immensely.
Thank you for your help! I really appreciate it!
//CardStack Class File
import java.util.ArrayList;
import java.util.Collections;
class CardStack{
ArrayList< FlashCard > flashCards; //ArrayList for Flash Cards
int f;
public CardStack(){
flashCards = new ArrayList< FlashCard >();
f = 0;
}
public void addFlashCard( FlashCard c1 ){ //Adds Flash Cards to the Collection
flashCards.add( c1 );
}
public FlashCard getFirstFlashCard(){ //Gets the Flash Cards
return flashCards.get( 0 );
}
public void shuffleFlashCards(){ //Shuffles the Flash Cards
Collections.shuffle( flashCards );
}
public FlashCard nextCard(){ //Gets the next Flash Card
if( f < flashCards.size() - 2 ){
f = f + 1;
return flashCards.get( f );
}
return null;
}
}
//FlashCard Class File
abstract class FlashCard{
//Two methods to return Strings
public abstract String getQuestion();
public abstract String getAnswer();
}
//MathCard Class File
import java.util.Random;
class MathCard extends FlashCard{ //Inherits from the FlashCard class file
int n1, n2;
char op;
Random s;
public MathCard( char op1 ){ //Constructors to randomize math operator and limits random used up to 100
n1 = ( int )( Math.random() * 100 );
n2 = ( int )( Math.random() * 100 );
op = op1;
}
public String getQuestion(){ //Formats the question
String q = "";
q = q + "What is " + n1 + " " + String.valueOf( op ) + " " + n2 + "?";
return q;
}
public String getAnswer(){ //Displays the answer and also defaults impossible math to 0
String s = "";
int s1 = 0;
if( op == '+' )
s1 = n1 + n2;
else if( op == '-' )
s1 = n1 - n2;
else if( op== '*' )
s1 = n1 * n2;
else if( op == '/' )
s1 = n1 / n2;
else
s1 = 0;
s = s + s1;
return s;
}
}
//VocabCard Class File
class VocabCard extends FlashCard{ //Inherits from FlashCard
String ques;
String ans;
public VocabCard( String Question, String Answer ){ //Constructors for Question and Answer
ques = Question;
ans = Answer;
}
public String getQuestion(){
return ques;
}
public String getAnswer(){
return ans;
}
}
//Driver Class File
import java.util.Scanner;
public class Driver{
public static void main(String[] args){
Scanner flashIn = new Scanner( System.in );
CardStack cStack = new CardStack(); //Creates the Card Stack object
MathCard mathCard1 = new MathCard( '+' ); //Creates four Math Cards objects with different operators
MathCard mathCard2 = new MathCard( '-' );
MathCard mathCard3 = new MathCard( '*' );
MathCard mathCard4 = new MathCard( '/' );
VocabCard vocabCard1 = new VocabCard( "What is George's favorite creature?", "Wombat" ); //Creates four Vocab Card objects
VocabCard vocabCard2 = new VocabCard( "How many Star Wars current movies are there?", "8 (if you include Clone Wars)" );
VocabCard vocabCard3 = new VocabCard( "What is the capital of the United States of America?", "Washington D.C." );
VocabCard vocabCard4 = new VocabCard( "What is the most popular song of all time according to Billboard?", ""The Twist" by Chubby Checker" );
cStack.addFlashCard( mathCard1 ); //Adds 4 Math Cards and 4 Vocab Cards to the Card Stack
cStack.addFlashCard( mathCard2 );
cStack.addFlashCard( mathCard3 );
cStack.addFlashCard( mathCard4 );
cStack.addFlashCard( vocabCard1 );
cStack.addFlashCard( vocabCard2 );
cStack.addFlashCard( vocabCard3 );
cStack.addFlashCard( vocabCard4 );
cStack.shuffleFlashCards(); //Shuffles the Flash Cards
FlashCard f = cStack.getFirstFlashCard(); //Gets the first Flash Card
if( f == null )
System.exit( 0 );
System.out.println( "Question: " + f.getQuestion() ); //Asks a question
while(true){ //Displays actions and gives user ability to select their action
System.out.println( "Enter your Action (a or A = Get Answer, n or N = Next Question, q or Q = Quit): " );
char i = flashIn.nextLine().charAt( 0 );
if( i == 'a' ) //Displays Answer
System.out.println("Answer: " + f.getAnswer() );
if( i == 'A' )
System.out.println("Answer: " + f.getAnswer() );
else if( i == 'n' ){ //Gets next card
f = cStack.nextCard();
if( f == null )
System.exit( 0 );
System.out.println( "Question: " + f.getQuestion() );
continue;
}else if( i == 'N' ){
f = cStack.nextCard();
if( f == null ) //Exits program when no more cards left
System.exit( 0 );
System.out.println( "Question: " + f.getQuestion() );
continue;
}else if( i == 'q' ) //Quit the program
System.exit( 0 );
else if( i == 'Q' )
System.exit( 0 );
}
}
}
Explanation / Answer
see the above code and apply in your program. do changes where necessary
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.