Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please add a button View Longest Word that takes in the string of answer inputs by the user and list them from longest to shortest.

Please add a button "View Longest Word" that takes in the string of answer inputs by the user and list them from longest to shortest.

QuestionAnswerPlatform.java

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JLabel;

import java.awt.BorderLayout;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

public class QuestionAnswerPlatform {

private JFrame frame;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

QuestionAnswerPlatform window = new QuestionAnswerPlatform();

window.frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the application.

*/

public QuestionAnswerPlatform() {

initialize();

}

/**

* Initialize the contents of the frame.

*/

private void initialize() {

frame = new JFrame();

frame.setBounds(100, 100, 450, 300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(null);

JLabel lblNewLabel = new JLabel("Welcome to the FUZA Therapy Session. Start session to begin.");

lblNewLabel.setBounds(53, 11, 318, 50);

frame.getContentPane().add(lblNewLabel);

JButton btnNewButton = new JButton("Start Session");

btnNewButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

frame.dispose();

HandleQuiz hq=new HandleQuiz();

hq.main(null);

}

});

btnNewButton.setBounds(152, 137, 116, 23);

frame.getContentPane().add(btnNewButton);

}

}

QuestionBank.java

import java.util.HashSet;

import java.util.Random;

import java.util.Set;

public class QuestionBank {

String [] questions;

Set set;

int currQuestionIndex;

public QuestionBank(){

set=new HashSet();

questions = new String [10]; //increase array size if you will add more questions

questions[0]= "Which three words describe you best?";

questions[1]= "Which is your best feature?";

questions[2]= "Which common saying or phrase describes you?";

questions[3]= "Whats the best thing thats happened to you this week?";

questions[4]= "Who was your role model when you were a child?";

questions[5]= "Who was your favorite teacher and why?";

questions[6]= "What was your favorite subject at school?";

questions[7]= "What did you want to be when you grew up?";

questions[8]= "If you could have one wish come true what would it be?";

questions[9]= "Which would you prefer three wishes over five years or one wish right now?";

//add more questions

}

public boolean isLastQuestion()

{

if(set.size()==questions.length-1)

return true;

return false;

}

public String getNextQuestion() {

//do stuff in here to get the next question.. Randomly from the array...

if(set.size()==questions.length)

{

return null;

}

Random random=new Random();

currQuestionIndex=random.nextInt(questions.length);

while(set.contains(currQuestionIndex))

currQuestionIndex=random.nextInt(questions.length);

set.add(currQuestionIndex);

return questions[currQuestionIndex];

}

}

HandleQuiz.java

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JTextField;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.awt.event.ActionEvent;

public class HandleQuiz {

private String question;

private String answer;

private static int qIndexCount;

private QuestionBank qb;

private static int sessCount;

private JFrame frame;

private JTextField textField;

private HandleSessionChange hsc;

private static PrintWriter pw;

private static File file;

static {

file=new File("quizQA.txt");

try {

pw=new PrintWriter(file);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

/**

* Launch the application.

*/

public static void main(String[] args) {

sessCount++;

qIndexCount=1;

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

HandleQuiz window = new HandleQuiz();

window.frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

public int getSessionNumber()

{

return sessCount;

}

/**

* Create the application.

*/

public HandleQuiz() {

qb=new QuestionBank();

initialize();

}

/**

* Initialize the contents of the frame.

*/

private void initialize() {

frame = new JFrame();

frame.setBounds(100, 100, 537, 385);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(null);

JLabel label = new JLabel("label");

label.setBounds(21, 26, 462, 50);

frame.getContentPane().add(label);

String qs=qb.getNextQuestion();

label.setText("Session #"+sessCount+" "+qs);

JLabel lblAnswer = new JLabel("Answer: ");

lblAnswer.setBounds(21, 111, 85, 14);

frame.getContentPane().add(lblAnswer);

textField = new JTextField();

textField.setBounds(116, 108, 367, 20);

frame.getContentPane().add(textField);

textField.setColumns(10);

JButton button = new JButton("Next Question");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

if(qIndexCount==1)

{

pw.println("Session #"+sessCount+" Q"+qIndexCount+" "+qs);

pw.println("\tA"+qIndexCount+" "+textField.getText());

pw.flush();

}

else {

pw.println("\tA"+qIndexCount+" "+textField.getText());

pw.flush();

}

qIndexCount++;

if(button.getText().equalsIgnoreCase("finish session"))

{

frame.dispose();

hsc=new HandleSessionChange(sessCount+1);

hsc.main(null);

pw.flush();

pw.close();

return;

}

boolean isLast=qb.isLastQuestion();

String ques=qb.getNextQuestion();

pw.println("Session #"+sessCount+" Q"+qIndexCount+" "+ques);

pw.flush();

if(isLast)

{

button.setText("Finish Session");

}

label.setText("Session #"+sessCount+" "+ques);

textField.setText("");

String ans=textField.getText();

}

});

button.setBounds(176, 167, 130, 23);

frame.getContentPane().add(button);

}

}

HandleSession.java

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

import java.awt.event.ActionEvent;

import javax.swing.JTextArea;

import javax.swing.DropMode;

import javax.swing.JScrollPane;

public class HandleSessionChange {

private JFrame frame;

private static int sessCount;

private HandleQuiz hq;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

HandleSessionChange window = new HandleSessionChange();

window.frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the application.

*/

public HandleSessionChange(int count)

{

this.sessCount=count;

}

public HandleSessionChange()

{

initialize();

}

/**

* Initialize the contents of the frame.

*/

private void initialize() {

frame = new JFrame();

frame.setBounds(100, 100, 450, 430);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(null);

JLabel lblNewLabel = new JLabel("click start session to start session #"+sessCount);

lblNewLabel.setBounds(39, 11, 354, 54);

frame.getContentPane().add(lblNewLabel);

JButton btnNewButton = new JButton("start session");

btnNewButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

frame.dispose();

hq.main(null);

}

});

btnNewButton.setBounds(39, 124, 133, 23);

frame.getContentPane().add(btnNewButton);

JScrollPane scrollPane_1 = new JScrollPane();

scrollPane_1.setBounds(39, 197, 354, 194);

frame.getContentPane().add(scrollPane_1);

JTextArea textArea = new JTextArea();

scrollPane_1.setViewportView(textArea);

JButton viewall = new JButton("View all Q and A");

viewall.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

File file=new File("quizQA.txt");

Scanner obj=null;

try {

obj = new Scanner(file);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

return;

}

while(obj.hasNextLine())

{

textArea.append(obj.nextLine());

textArea.append(" ");

}

}

});

viewall.setBounds(219, 124, 116, 23);

frame.getContentPane().add(viewall);

JScrollPane scrollPane = new JScrollPane();

scrollPane.setBounds(122, 229, 109, 31);

frame.getContentPane().add(scrollPane);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions