Question

So I am having probelms with figuring out how to do this program the part in...

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());

      
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:-

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class WidgetDriver {
  

   public static void main(String[] args) {
       WidgetView widgetView = new WidgetView(200,250);
      
       //Random Numbers
       Random rand = new Random();
       Integer x=rand.nextInt(9)+1;
       Integer y=rand.nextInt(9)+1;
      
       //Random numbers as labels
       JLabel xLabel = new JLabel("Value Of X : "+x.toString());
       JLabel yLabel = new JLabel("Value Of Y : "+y.toString());
      
       //Added to widget
       widgetView.add(xLabel);
       widgetView.add(yLabel);
      
       //The prompt and text field
       JLabel prompt = new JLabel( "Enter an operation number.");
       widgetView.add(prompt);
      
       JTextField input = new JTextField(5);
       widgetView.add(input);
      
       //Html formatting had to be added to enable multiline, as the message was too long
       String msg="Press here when you've \n entered your operation.";
       JButton btn = new JButton ("<html>" + msg.replaceAll("\\n", "<br>") + "</html>");
       btn.setPreferredSize(new Dimension(170,30));
       widgetView.add(btn);
      
       ///The result
       JLabel result = new JLabel("Result is 0");
       widgetView.add(result);
      
       //What happens when button is pressed
       btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String inp=input.getText();
                   //Operation number in int
                   int op = Integer.parseInt(inp);
                   int res=0;
                   String solution="";
                  
                  
                   if(op >=1 && op <= 10){
                       res=x+y;  
                   }else if(op%4==0){
                       res=x-y;
                   }else if(op%5==0){
                       res=x/y;
                   }else if(op%2==0){
                       float resF = (float)x/y;
                       solution+=resF;
                   }else{
                       res=x*y;
                   }
                  
                   //Means no float divison happened
                   if(solution.length() == 0){
                       solution+=res;
                   }
                  
                   result.setText("Result is "+solution);
              
            }
       });
      
   }

}

If you find any difficulty with the code, please let know know I will try for any modification in the code. Hope this answer will helps you. If you have even any small doubt, please let me know by comments. I am there to help you. Please give Thumbs Up,Thank You!! All the best

Add a comment
Know the answer?
Add Answer to:
So I am having probelms with figuring out how to do this program the part in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help me the JAVA program - Choose Your Operation Write a program that uses a...

    Please help me the JAVA program - Choose Your Operation 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...

  • Program #2 Write a program to simulate showing an NBA Team on a Basketball court Here...

    Program #2 Write a program to simulate showing an NBA Team on a Basketball court Here is an example of the screenshot when running the program: Tony Parker T. Splitter T. Duncan M. Gineb Player Kame: M Ginobil Player Age 2 Add A Player Ce An example of the JFrame subclass with main method is as follows import java.awt. import java.awt.event." import javax.swing. public class NBA Playoff extends JFrame private JTextField txtName: private JTextField txtAge: private NBATeam spurs private NBAcourtPanel...

  • I am having trouble figuring out what should go in the place of "number" to make...

    I am having trouble figuring out what should go in the place of "number" to make the loop stop as soon as they enter the value they put in for the "count" input. I am also having trouble nesting a do while loop into the original while loop (if that is even what I am supposed to do to get the program to keep going if the user wants to enter more numbers???) I have inserted the question below, as...

  • Can someone modify my code so that I do not get this error: Exception in thread...

    Can someone modify my code so that I do not get this error: Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:1043) at java.awt.Container.add(Container.java:363) at RatePanel.<init>(RatePanel.java:64) at CurrencyConverter.main(CurrencyConverter.java:16) This code should get the amount of money in US dollars from user and then let them select which currency they are trying to convert to and then in the textfield print the amount //********************************************************************* //File name: RatePanel.java //Name: Brittany Hines //Purpose: Panel for a program that convers different currencyNamerencies to use dollars //*********************************************************************...

  • JAVA Hello I am trying to add a menu to my Java code if someone can...

    JAVA Hello I am trying to add a menu to my Java code if someone can help me I would really appreacite it thank you. I found a java menu code but I dont know how to incorporate it to my code this is the java menu code that i found. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; public class MenuExp extends JFrame { public MenuExp() { setTitle("Menu Example");...

  • Write a program that will define a runnable frame to have a ball move across the...

    Write a program that will define a runnable frame to have a ball move across the frame left to right. While that is happening, have another frame that will let the user do something like click a button or input into a textfield, but keep the ball moving in the other frame. When the ball reaches the right edge of the frame or when the user takes action - enters in textfield or clicks the button, end the thread. The...

  • Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button...

    Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button just hit enter on keyboard to try number -Remove Button Quit import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; // Main Class public class GuessGame extends JFrame { // Declare class variables private static final long serialVersionUID = 1L; public static Object prompt1; private JTextField userInput; private JLabel comment = new JLabel(" "); private JLabel comment2 = new JLabel(" "); private int...

  • Help with StringAnalysis Write a WidgetViewer application. Create a class StringAnalysis. This class has an event...

    Help with StringAnalysis Write a WidgetViewer application. Create a class StringAnalysis. This class has an event handler inner that extends WidgetViewerActionEvent it has instance variables StringSet sSet JTextField inputStr JLabel numStr JLabel numChar In the constructor, create a WidgetViewer object create sSet create a local variable JLabel prompt initialized to "Enter a String" create inputStr with some number of columns create a local variable JButton pushMe initialized to "Push to include String" create numStr initialized to "Number of Strings: 0"...

  • In java language here is my code so far! i only need help with the extra...

    In java language here is my code so far! i only need help with the extra credit part For this project, you will create a Rock, Paper, Scissors game. Write a GUI program that allows a user to play Rock, Paper, Scissors against the computer. If you’re not familiar with Rock, Paper, Scissors, check out the Wikipedia page: http://en.wikipedia.org/wiki/Rock-paper-scissors This is how the program works: The user clicks a button to make their move (rock, paper, or scissors). The program...

  • Java question so right now this code is work perfectly the problem is I also want...

    Java question so right now this code is work perfectly the problem is I also want to know if possible to get the X and Y value outside the JFrame? import java.awt.FlowLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JFrame; import javax.swing.JLabel; public class MouseEventDemo extends JFrame implements MouseListener { //It is a class which generate a JFrame and implements MouseListener interface //These all are text that show x , y coordinates on mouse click event JLabel lclick; JLabel lpressed; JLabel lreleased;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT