Program :1 FlashCard This program implements a flash card to teach multiplication. Write a program that uses a WidgetView object (the WidgetView class is available
Program :1
FlashCard
This program implements a flash card to teach multiplication.
Write a program that uses a WidgetView object (the WidgetView class is available elsewhere in this Lesson).
your program should use a Random object to generate two random numbers between 0 and 9 (inclusive).
To explain the operation of this program, we'll assume that our random number generator generated 6 and 3.
display a JLabel with the text "What is 6 times 3?"
create an empty JTextField to hold the user's answer
create a JButton that has the text "click after answering"
The user should put his or her guess in the JTextField and click the JButton.
When the button is clicked, the program should get the text from the JTextField, convert it from String to int, and create a JLabel that says either
That's right. Good Job, or
Sorry, the correct answer is 18
depending on whether the user input the correct number (18 in this case).
Program 2:
Write a program that uses a WidgetView object to do the following:
Generate two random integers between 1 and 9 (inclusive). Name one of them x , the other y . Display them to the user using JLabel objects.
Create a JLabel object displaying the text "Enter an operation number."
Create a JTextField for the user's input.
Create a JButton displaying the text "Press here when you've entered your operation." Use addAndWait to add it to the WidgetView object.
When the user clicks the JButton , evaluate operation in the following order to determine the one and only mathematical operation to perform on x and y . Use a JLabel to display the result.
If operation is between 1 and 10 inclusive, add x and y.
If operation is evenly divisible by 4, subtract y from x.
If operation is evenly divisible by 5, use integer division to divide y into x.
If operation is an even number, use floating point division to divide y into x.
If none of the other tests on operation apply, multiply x and y.
Note: Operation can be negative or zero.
import java.awt.event.ActionListener;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* A really simple class to display Swing widgets in a FlowLayout GUI.
*
*
* @author parks
*/
public class WidgetView {
public static final int DEFAULT_X_SIZE = 600;
public static final int DEFAULT_Y_SIZE = 400;
private JFrame jframe;
private JPanel anchor;
private boolean userClicked = false;
private Lock lock;
private Condition waitingForUser;
private JComponent userInputComponent = null;
private ActionListener eventHandler;
/**
* Default constructor will display an empty JFrame that has a Flow layout
* JPanel as its content pane and initialize the frame to a default size.
*/
public WidgetView() {
this(DEFAULT_X_SIZE, DEFAULT_Y_SIZE);
}
/**
* Constructor will display an empty JFrame that has a Flow layout JPanel as its
* content pane and initialize the frame to a given size.
*/
public WidgetView(int pixelSizeInX, int pixelSizeInY) {
lock = new ReentrantLock();
waitingForUser = lock.newCondition();
// lambda expression requires Java 8
eventHandler = e -> {
if (e.getSource() != userInputComponent) {
return;
}
lock.lock();
try {
while (userClicked) {
waitingForUser.await();
}
} catch (InterruptedException e1) {
System.err.println("WidgetView reports that something really bad just happened");
e1.printStackTrace();
System.exit(0);
} finally {
userClicked = true;
waitingForUser.signalAll();
lock.unlock();
}
};
/*
* java 7 solution eventHandler = new ActionListener() {
*
* @Override public void actionPerformed(ActionEvent e) { if (e.getSource() !=
* userInputComponent) { return; } lock.lock(); userClicked = true;
* waitingForUser.signalAll(); lock.unlock(); } };
*/
jframe = new JFrame();
anchor = new JPanel();
jframe.setContentPane(anchor);
jframe.setSize(pixelSizeInX, pixelSizeInY);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
}
/**
* Add a Swing widget to the GUI.
*
* @param jcomp
* Swing widget (subclasses of JComponent--like JLabel and
* JTextField) to be added to the JFrame
*/
public void add(JComponent jcomp) {
anchor.add(jcomp);
jframe.setContentPane(anchor);
}
/**
* Add an Abstract Button (like a JButton) to the JFrame. Create an action
* listener to wait (suspend the caller) until it is clicked.
*
* @param absButton
* Button (like a JButton) to add to the JFrame
*/
public void addAndWait(AbstractButton absButton) {
userInputComponent = absButton;
absButton.addActionListener(eventHandler);
addWait(absButton);
}
/**
* Add a JTextField to the JFrame, and wait for the user to put the cursor in
* the field and click Enter. The caller is suspended until enter is clicked.
*
* @param jTextField
* Field to add to the JFrame
*/
public void addAndWait(JTextField jTextField) {
userInputComponent = jTextField;
jTextField.addActionListener(eventHandler);
addWait(jTextField);
}
private void addWait(JComponent jcomp) {
add(jcomp);
lock.lock();
try {
while (!userClicked) {
waitingForUser.await();
}
} catch (InterruptedException e1) {
System.err.println("WidgetView reports that something really bad just happened");
System.err.println(e1);
e1.printStackTrace();
System.exit(0);
} finally {
userClicked = false;
waitingForUser.signalAll();
lock.unlock();
}
}
}
Step by Step Solution
3.50 Rating (163 Votes )
There are 3 Steps involved in it
Step: 1
Program 1 import javaawteventActionEvent import javaawteventActionListener import javautilRandom import javaxswingJButton import javaxswingJLabel impo...See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Document Format ( 2 attachments)
6090d401b0897_21958.pdf
180 KBs PDF File
6090d401b0897_21958.docx
120 KBs Word File
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started