Question
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,
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); }
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