Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is for an intro to java class: The goal of this project is to create a GUI

ID: 3831789 • Letter: T

Question

This is for an intro to java class: The goal of this project is to create a GUI flashcard program that reads data from a text file. It is expected that you will base your work on your previous projects. Create a data structure to hold a series of flashcard objects. For this project, read the data from a text file. It is recommended but not required to either have the data in CSV format or have the questions and answers on alternating lines. To select the file, use a JFileChooser as discussed in class examples. Also submit an example file of your questions and answers. There should be at least 5 questions and answers in the text file.

When the program is launched the user should be presented with a JFileChooser which they will be use to select the data file to read. The GUI should consist of a single JLabel and JButton. On starting, the JLabel should display a welcome message and the JButton should display “Start.” After starting the first question should been displayed and the button should display “Click for Answer.” Upon the next click the label should display the answer and the button should display “Next Question.”

After the last question has been answered the program should display a dialog box asking the user if they want to go through the questions again. The main button should be disabled.

Just to be clear, in this project I want the data to be read from a text file. It is fine and expected to base your work on your previous project.

This is the program I'm starting with:

package fc;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

class FlashCard{
String question;
String answer;

public FlashCard( String q, String a ){
question = q;
answer = a;
}
}

public class FlashCardProgram{

static int q = 0;
static int nums[] = { 0,1,2,3 };

static int[] shuffleArray( int[] shuffle ){

Random rnd = new Random();
for (int i = shuffle.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
int a = shuffle[index];
shuffle[index] = shuffle[i];
shuffle[i] = a;
}
return shuffle;
}

public static void main( String[] args ){
ArrayList< FlashCard > fc = new ArrayList<>();
fc.add( new FlashCard( "Question 1", "Answer 1" ) );
fc.add( new FlashCard( "Question 2", "Answer 2" ) );
fc.add( new FlashCard( "Question 3", "Answer 3" ) );
fc.add( new FlashCard( "Question 4", "Answer 4" ) );

nums = shuffleArray( nums );
JFrame f = new JFrame( "Flash Cards" );
JLabel lbl01 = new JLabel( "Welcome to Flashcards" );
JButton btn01 = new JButton( "Start" );

btn01.addActionListener( new ActionListener(){

@Override
public void actionPerformed( ActionEvent ae ) {
if( btn01.getText().equals( "Start" ) ){
lbl01.setText( fc.get( nums[q]).question );
btn01.setText( "Click for Answer" );
}
else if( btn01.getText().equals( "Click for Answer" ) ){
lbl01.setText( fc.get(nums[q]).answer );
q++;
btn01.setText( "Next Question" );
}
else if(btn01.getText().equals( "Next Question" )){
String input = "";
if(q>3){
input = JOptionPane.showInputDialog( "Want to start over?(Yes/No)" );
if(input.equalsIgnoreCase( "yes" )){
btn01.setText( "Start" );
lbl01.setText( "Welcome back" );
q = 0;
nums = shuffleArray( nums );
}
else System.exit( 0 );
}else{
lbl01.setText( fc.get(nums[q]).question );
btn01.setText( "Click for Answer" );
}
}
}
});

lbl01.setBounds( 50, 10, 500, 30 );
btn01.setBounds( 150, 200, 200, 30 );
f.getContentPane().setLayout( null );
f.getContentPane().add( lbl01 );
f.getContentPane().add( btn01 );
f.setVisible( true );
f.setSize( 500, 400 );
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

}


}

Explanation / Answer

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

class FlashCard{
String question;
String answer;

public FlashCard( String q, String a ){
question = q;
answer = a;
}
}

public class FlashCardProgram{

static int q = 0;
static int nums[] = { 0,1,2,3 };

static int[] shuffleArray( int[] shuffle ){

Random rnd = new Random();
for (int i = shuffle.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
int a = shuffle[index];
shuffle[index] = shuffle[i];
shuffle[i] = a;
}
return shuffle;
}

public static void main( String[] args ){
ArrayList< FlashCard > fc = new ArrayList<>();
fc.add( new FlashCard( "Question 1", "Answer 1" ) );
fc.add( new FlashCard( "Question 2", "Answer 2" ) );
fc.add( new FlashCard( "Question 3", "Answer 3" ) );
fc.add( new FlashCard( "Question 4", "Answer 4" ) );

nums = shuffleArray( nums );
JFrame f = new JFrame( "Flash Cards" );
JLabel lbl01 = new JLabel( "Welcome to Flashcards" );
JButton btn01 = new JButton( "Start" );

btn01.addActionListener( new ActionListener(){

@Override
public void actionPerformed( ActionEvent ae ) {
if( btn01.getText().equals( "Start" ) ){
lbl01.setText( fc.get( nums[q]).question );
btn01.setText( "Click for Answer" );
}
else if( btn01.getText().equals( "Click for Answer" ) ){
lbl01.setText( fc.get(nums[q]).answer );
q++;
btn01.setText( "Next Question" );
}
else if(btn01.getText().equals( "Next Question" )){
String input = "";
if(q>3){
input = JOptionPane.showInputDialog( "Want to start over?(Yes/No)" );
if(input.equalsIgnoreCase( "yes" )){
btn01.setText( "Start" );
lbl01.setText( "Welcome back" );
q = 0;
nums = shuffleArray( nums );
}
else System.exit( 0 );
}else{
lbl01.setText( fc.get(nums[q]).question );
btn01.setText( "Click for Answer" );
}
}
}
});

lbl01.setBounds( 50, 10, 500, 30 );
btn01.setBounds( 150, 200, 200, 30 );
f.getContentPane().setLayout( null );
f.getContentPane().add( lbl01 );
f.getContentPane().add( btn01 );
f.setVisible( true );
f.setSize( 500, 400 );
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

}


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote