Question

Write an applet program that would enter two variables in a text field and perform the...

Write an applet program that would enter two variables in a text field and perform the following operations when calculate button is pressed. Design a friendly interface. Have buttons for all operations including exit.

Addition

Subtraction

Multiplication

Division

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

Code:

====

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Q2 extends Applet implements ActionListener
{
    TextField num1 = new TextField(10);
    TextField num2 = new TextField(10);
    TextField res = new TextField(10);

    Label fir = new Label("FIRST NO:");
    Label sec = new Label("SECOND NO:");
    Label answer = new Label("Answer:");
    Button a = new Button("Addition");
    Button s = new Button("Subtraction");
    Button m = new Button("Multiplication");
    Button d = new Button("Division");

    public void init()
    {
        num1.setForeground(Color.RED);
        num2.setForeground(Color.RED);
        add(fir);
        add(num1);
        add(sec);
        add(num2);
        add(res);
        add(answer);
//        add(sum);
//        add(diff);
//        add(mul);
//        add(mul);

        add(a);add(s);add(m);add(d);
        a.addActionListener(this);
        s.addActionListener(this);
        m.addActionListener(this);
        d.addActionListener(this);
    }
    public void paint(Graphics g) {
        this.fir.setLocation(50,50);
        this.num1.setLocation(150,50);
        this.sec.setLocation(50,100);
        this.num2.setLocation(150,100);

        this.a.setLocation(20,150);
        this.s.setLocation(100,150);
        this.m.setLocation(200,150);
        this.d.setLocation(300,150);

        this.answer.setLocation(50, 200);
        this.res.setLocation(150, 200);


    }
    public void actionPerformed(ActionEvent e)
    {
        
        if (e.getSource() == a)
        {
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());
            res.setText(" " + (n1 + n2));
        }
        else if (e.getSource() == s)
        {
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());
            res.setText(" " + (n1 - n2));
        }
        else if (e.getSource() == m)
        {
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());
            res.setText(" " + (n1 * n2));
        }
        else if(e.getSource() == d)
        {
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());
            res.setText(" " + (n1 / n2));
        }
    }
}

Output:

======

Applet Viewer: Q2 Applet DL FIRST NO: SECOND NO: 2 Addition! Subtraction」 Mu ation] Division Answer: 12 Applet started.

Please comment if you need any help.

Add a comment
Know the answer?
Add Answer to:
Write an applet program that would enter two variables in a text field and perform the...
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
  • Write a calculator program using JavaScript in HTML in the same HTML file. (There will only...

    Write a calculator program using JavaScript in HTML in the same HTML file. (There will only be 1 HTML file containing everything that you use for this program.) *************JavaScript Functions should be written in the HTML <head> .............. </head> tag, **************** (Please be mindful of the formatting of the text of your program. Meaning that do not copy paste everything in a single line. Add clear comments for a better understanding of your program) as follows Assignment: create a form...

  • Question 20: Write a python program that will perform the operations of simple calculator. The operations...

    Question 20: Write a python program that will perform the operations of simple calculator. The operations are : Addition, subtraction, Multiplication and division. Sample output : Select operation. 1.Add 2.Subtract 3.Multiply 4.Divide Enter choice(1/2/3/4): 3 Enter first number: 15 Enter second number: 14 15 * 14 = 210

  • Write a program that declares an initializes two variables a & b with the values your...

    Write a program that declares an initializes two variables a & b with the values your choice, use pointer to those variables and perform, addition, subtraction and multiplication: Print the results.

  • Calculator = 0.01 Calculate . Add O subtract O Multiply ODvide uttons on the right determine the operation that is perf...

    Calculator = 0.01 Calculate . Add O subtract O Multiply ODvide uttons on the right determine the operation that is performed on the numbers. For example, the following image shows values 12 and 3 are entered in the ion in selected. When the Calculate button is pressed, the result of the addition operation is shown after the equal sign ( In this example, the result is 1 Calculator 12 .15.01 Calculate | ⓔAdd subtract。Multiply O Divide g images show the...

  • This week we looked at an example math program that would display the answer to a...

    This week we looked at an example math program that would display the answer to a random math problem. Enhance this assignment to prompt the user to enter the solution to the displayed problem. After the user has entered their answer, the program should display a message indicating if the answer is correct or incorrect. If the answer is incorrect, it should display the correct answer. The division section should use Real data types instead of Integer data types. Keep...

  • RAPTOR PROGRAMMING Write a program which will receive two integer values as input. Then prompt the...

    RAPTOR PROGRAMMING Write a program which will receive two integer values as input. Then prompt the user to choose one of the following operations and produce an output with the selected operation. 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Modulo Division Example output with response: Please enter an input: 2 Please enter another input: 3 Please choose from the follow: 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Modulo Division Output: 2 % 3 is 2.

  • Write a Python Program that displays repeatedly a menu as shown in the sample run below....

    Write a Python Program that displays repeatedly a menu as shown in the sample run below. The user will enter 1, 2, 3, 4 or 5 for choosing addition, substation, multiplication, division or exit respectively. Then the user will be asked to enter the two numbers and the result for the operation selected will be displayed. After an operation is finished and output is displayed the menu is redisplayed, you may choose another operation or enter 5 to exit the...

  • Write an assembler program that asks the user (as shown below) for two integers and a...

    Write an assembler program that asks the user (as shown below) for two integers and a single character representing the arithmetic operations: addition, subtraction, multiplication and integer division (displays both quotient and remainder). Perform the requested operation on the operands and display the result as specified below. Assume SIGNED NUMBERS. The program should not divide by 0! If the user attempts to divide by 0, display the error message: "Cannot divide by zero." If this error occurs, the program should...

  • Prepare a MATLAB program (script file) that allows a user to enter two 4-element vectors and...

    Prepare a MATLAB program (script file) that allows a user to enter two 4-element vectors and scalar values to perform different mathematical (exponentiation, multiplication, and subtraction) operations on the elements within the originally entered vectors Moreover, the program also performs addition, dot product and element-wise multiplication of the two vectors.

  • In Python Calculator Write a program that allows the user to enter two numbers and then...

    In Python Calculator Write a program that allows the user to enter two numbers and then adds, subtracts, divides or multiplies them when the user clicks on the appropriate button. Make sure you have a Quit button. Modification and requirements”: _____Window title should be  your name Calculate self.main_window.title("?????") _____Labels - change the attributes of the color, font, size and bold the answerExample self.label = tkinter.Label(self.main_window, \ text='?????????!', font=(" should Arial", 12, "bold"), fg="blue", bg="pink")Tip: choose a color from the...

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