Question

Create a simple calculator. Your interface should contain two input boxes in which the user can...

Create a simple calculator. Your interface should contain two input boxes in which the user can put two numbers. There should be four buttons: add, subtract, multiply, and divide. When the user inputs two number and clicks on an operation button, the result should be displayed in the label.

Can some please help my code that i came up with just displays a pop box with no words or nothing! I'm not the best at coding. Any tips or advice would be greatly appreciated.

My source code:

package java applicationcalculator;


/**
*
* @author vancarlosjackson
*
*
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Calculator extends Applet implements ActionListener{
  


Label Num1 = new Label("Enter first number:");
Label Num2 = new Label("Enter second number:");
TextField First = new TextField("",0);
TextField Second = new TextField("",0);
TextField Answer = new TextField("",30);
Font MyFont = new Font("TimesRoman", Font.BOLD,12);
Button ButtAdd = new Button("Add");
Button ButtMinus = new Button("Subtract");
Button ButtMultiple = new Button("Multiply");
Button ButtDivide = new Button("Divide");

public void init(){
  
add(Num1);
add(First);
add(Second);
add(Answer);
ButtAdd.addActionListener(this);
ButtMinus.addActionListener(this);
ButtMultiple.addActionListener(this);
ButtDivide.addActionListener(this);
First.requestFocus();
  
}
public void actionPerformed(ActionEvent e){
String s = e.getActionCommand();

if ("ButAdd".equals(s))
{
Integer Result = Integer.parseInt(First.getText()) + Integer.
parseInt(Second.getText()); Answer.setLocation(10,100);
Answer.setText("The result for added two numbers is: +"
+ "Answer");

}
else if ("ButtMinus".equals(s)){
Integer Result = Integer.parseInt(First.getText())-Integer.parseInt
(Second.getText()); Answer.setLocation(10,100);
Answer.setText("The result for multiplied two number is:"
+ Answer);
}
else if ("ButtMultiple".equals(s)){
Integer Result = Integer.parseInt(First.getText()) * Integer.parseInt
(Second.getText()); Answer.setLocation(10,100);
Answer.setText("The result for multiplied two numbers is:" +
Answer);

}
else if("ButtDivide".equals(s)){
Integer Results = Integer.parseInt(First.getText())/Integer.
parseInt(Second.getText());
Answer.setLocation(10,100);
Answer.setText("The result for divided two number is"+ Answer);

}

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

/**

* Modified Calculator.java program

* The java program prompts user to enter two integer values

* in two text boxes and then allows to click any buttons of user

* choice for add,subtract, divide and multiplicaiton.

* Then find the result and display,

* */

//Calculator.java

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

public class Calculator extends Applet implements ActionListener

{

private Label Num1 = new Label("Enter first number:");

private Label Num2 = new Label("Enter second number:");

private TextField First = new TextField("",0);

private TextField Second = new TextField("",0);

private TextField Answer = new TextField("",50);

private Font MyFont = new Font("TimesRoman", Font.BOLD,12);

private Button ButtAdd = new Button("Add");

private Button ButtMinus = new Button("Subtract");

private Button ButtMultiple = new Button("Multiply");

private Button ButtDivide = new Button("Divide");

public void init()

{

add(Num1);

add(First);

add(Num2);

add(Second);

add(Answer);

//Add add,subtract ,multiplication and division buttons

add(ButtAdd);

add(ButtMinus);

add(ButtMultiple);

add(ButtDivide);

add(Answer);

ButtAdd.addActionListener(this);

ButtMinus.addActionListener(this);

ButtMultiple.addActionListener(this);

ButtDivide.addActionListener(this);

First.requestFocus();

}

/*Override the actionPerformed method*/

public void actionPerformed(ActionEvent e){

//call getSource method to get the source of user button click

Object s = e.getSource();

//Check if object,s is add button

if (s.equals(ButtAdd))

{

Integer Result = Integer.parseInt(First.getText()) + Integer.parseInt(Second.getText());

Answer.setLocation(10,100);

Answer.setText("The result for added two numbers is: "+ Result);

}

//Check if object,s is minus button

else if (s.equals(ButtMinus)){

Integer Result = Integer.parseInt(First.getText())-Integer.parseInt

(Second.getText()); Answer.setLocation(10,100);

Answer.setText("The result for multiplied two number is:"

+ Result);

}

//Check if object,s is multiple button

else if (s.equals(ButtMultiple)){

Integer Result = Integer.parseInt(First.getText()) * Integer.parseInt

(Second.getText()); Answer.setLocation(10,100);

Answer.setText("The result for multiplied two numbers is:" +

Result);

}

//Check if object,s is divide button

else if(s.equals(ButtDivide)){

Integer Result = Integer.parseInt(First.getText())/Integer.

parseInt(Second.getText());

Answer.setLocation(10,100);

Answer.setText("The result for divided two number is"+ Result);

}

} //end of actionPerformed

}//end of class,Calculator

Sample Output:

Add a comment
Know the answer?
Add Answer to:
Create a simple calculator. Your interface should contain two input boxes in which the user can...
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
  • I have been messing around with java lately and I have made this calculator. Is there...

    I have been messing around with java lately and I have made this calculator. Is there any way that I would be able to get a different sound to play on each different button 1 - 9 like a nokia phone? Thank you. *SOURCE CODE* import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class Calculator extends JFrame { private final Font BIGGER_FONT = new Font("monspaced",Font.PLAIN, 20); private JTextField textfield; private boolean number = true; private String equalOp = "="; private...

  • Java: In this assignment, you will create an accumulator accumulator-calculator that displays a sad face “...

    Java: In this assignment, you will create an accumulator accumulator-calculator that displays a sad face “ :-( “ whenever the number displayed by the calculator is negative and a happy face “ :-) ” whenever the number displayed is positive. The calculator responds to the following commands: num + , num - , num * , num / and C ( Clear ). After initial run, if the user clicks 8 and then presses the “+” button for example, the...

  • please help me debug this Create a Future Value Calculator that displays error messages in labels.GUISpecificationsStart...

    please help me debug this Create a Future Value Calculator that displays error messages in labels.GUISpecificationsStart with the JavaFX version of the Future Value application presented in chapter 17. Create error message labels for each text field that accepts user input. Use the Validation class from chapter 17 to validate user input.Format the application so that the controls don’t change position when error messages are displayed. package murach.business; public class Calculation {        public static final int MONTHS_IN_YEAR =...

  • Create a 'simple' calculator. The calculator should be able to add, subtract, multiply, and divide two...

    Create a 'simple' calculator. The calculator should be able to add, subtract, multiply, and divide two numbers. You can use whatever format you want to receive the numbers (such as textboxes). You also can use whatever method (such as a button for each operation +, -, *, /) to determine what operation the user wants to accomplish. The output should be displayed in a similar format to "1 + 2 = 3" or "1 - 2 = -1". Make sure...

  • JAVA QUESTION Design a simple calculator for two numbers

    JAVA QUESTIONDesign a simple calculator for two numbers.Important note. Avoid using IDE's facilities for automatically GUI code generation.Simple Calculator Second Number Multiply 」L Divide First Number Result Add Subtract Power Remainder -

  • IN PYTHON! Create a calculator GUI. The GUI should let the user add, subtract, multiply, and...

    IN PYTHON! Create a calculator GUI. The GUI should let the user add, subtract, multiply, and divide two numbers they input. All user input and the result displayed to the user should be seen in the GUI itself (i.e. the terminal should not show anything). Thank you very much! PLEASE PASTE IN HERE THE RIGHT INDENTS

  • C# Windows Form Application (CALCULATOR): (This is the instruction below): Windows Calculator that will going to...

    C# Windows Form Application (CALCULATOR): (This is the instruction below): Windows Calculator that will going to create a Windows Form Application that will mimics a calculator. This is the screenshot how it shouldl look like. Calculator It should have a textbox and the following buttons: + Addition - Subtraction * Multiplication / Division = Equals (Will perform the final calculation) C Clear (Will clear the text box) There will be no maximize or minimize buttons. The rules are these: 2...

  • The main method of your calculator program has started to get a little messy. In this...

    The main method of your calculator program has started to get a little messy. In this assignment, you will clean it up some by moving some of your code into new methods. Methods allow you to organize your code, avoid repetition, and make aspects of your code easier to modify. While the calculator program is very simple, this assignment attempts to show you how larger, real world programs are structured. As a new programmer in a job, you will likely...

  • Write a calculator that will give the user this menu options: 1)Add 2)Subtract 3)Multiply 4)Divide 5)Exit...

    Write a calculator that will give the user this menu options: 1)Add 2)Subtract 3)Multiply 4)Divide 5)Exit . Your program should continue asking until the user chooses 5. If user chooses to exit give a goodbye message. After the user selections options 1 - 4, prompt the user for two numbers. Perform the requested mathematical operation on those two numbers. Round the result to 1 decimal place. Please answer in python and make it simple. Please implement proper indentation, not just...

  • This is my code. I need the UML for this code My code: public class Main...

    This is my code. I need the UML for this code My code: public class Main extends FlightManager {    Stage window;    Scene scene;    Button button;    HBox layout = new HBox(20);    Label dateTxt, airlineTxt, originTxt, destTxt, clssTxt, scopeTxt, adultsTxt, childTxt;    @Override public void start(Stage stage) { // create the UI and show it here    window = stage; window.setTitle("Reservations 2020"); button = new Button("New Flight");    VBox group3 = new VBox(10); dateTxt = Text("", group3);...

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