Question
So I am having probelms with figuring out how to do this program the part in bold is the part I dont know how to
So I am having probelms with figuring out how to do this program the part in bold is the part I dont know how to do. This is in JAVA.
What I have so far is shown below the question, this is right upto what I have to do next.
Choose Your Operation
Write a program that uses a WidgetViewer 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 WidgetViewer 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.
Note: Put a copy of WidgetViewer.java from the WidgetViewer primer in the folder along with your program.
Grading Elements:
- Program displays a WidgetViewer window
- Program generates two random numbers in the appropriate range
- Program displays the random number using JLabel
- Program displays a JLabel instructing the user to enter an operation number
- Program displays a JTextField for the user's input
- Program displays a JButton for the user to indicate that processing should continue
- Program evaluates user input using the specified tests in the prescribed order
- Program executes the correct operation
- Program executes exactly one operation
- Program displays the appropriate result using a JLabel
- Program does not use System.out.print or Scanner for user interaction
import java.util.Random; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JTextField;
public class ChooseYourOperation {
public static void main(String[] args) {
//Create objects of widgetView and Random class
WidgetViewer wv = new WidgetViewer();
Random rand = new Random();
int x, y;
//rand.ints() take 3 arguments total numbers, min value, max value.
int[] nums = rand.ints(2, 1, 9).toArray();
//Store random numbers in num1 and num2
x = nums[0]; y = nums[1]; //Add a new label to widgetView. JLabel jl = new JLabel("Enter an operation number."); //Add a empty text filed to widgetView.
JTextField store = new JTextField(5); //Displays X and Y values JLabel jlx = new JLabel("x :" + x); JLabel jly = new JLabel("y :" + y); //Add the initial widgets
wv.add(jl, 10, 30, 300, 20); wv.add(jlx, 225, 30, 50, 20); wv.add(jly, 250, 30, 50, 20); wv.add(store, 170, 30, 50, 20); //Add a button to widgetView.
JButton jb = new JButton("Press here when you've entered your operation"); wv.addAndWait(jb); //Store user result in output variable. int output = Integer.parseInt(store.getText());
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started