Write a program using applets to accept a floating value as shown in the following output, theprogram should compute the sum of the two values and display the results on the screen. [3 marks]
Question
Write a program using applets to accept a floating value as shown in the following output, theprogram should compute the sum of the two values and display the results on the screen. [3 marks]
Solution
Here is a simple Java Applet program that accepts two floating point numbers, calculates their sum and displays the result:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class SumApplet extends Applet implements ActionListener {
TextField text1, text2;
public void init() {
text1 = new TextField(10);
text2 = new TextField(10);
add(text1);
add(text2);
text1.addActionListener(this);
text2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
repaint();
}
public void paint(Graphics g) {
String s1, s2, sum;
float a=0, b=0, result=0;
g.drawString("Input a float number in each box ", 0, 50);
try {
s1 = text1.getText();
a = Float.parseFloat(s1);
s2 = text2.getText();
b = Float.parseFloat(s2);
result = a + b;
} catch(NumberFormatException e) {
g.drawString("Invalid Format", 0, 80);
return;
}
g.drawString("The sum is: " + String.valueOf(result), 0, 100);
}
}
This program creates two text fields for input. When you enter a number in each field and press Enter, the actionPerformed method is called, which triggers a repaint. The paint method is then called, which retrieves the text from the text fields, converts them to floats, adds them, and displays the result. If the text cannot be converted to a float, it catches the NumberFormatException and displays an error message.
Similar Questions
Your task is to take two numbers of int data type, two numbers of float data type as input and output their sum:Declare variables: two of type int and two of type float.Read lines of input from stdin (according to the sequence given in the 'Input Format' section below) and initialize your variables.Use the and operator to perform the following operations:Print the sum and difference of two int variable on a new line.Print the sum and difference of two float variable rounded to one decimal place on a new line.
Add two numbersYou have been provided with some code. The code should take in two numbers from the user, add them together, and then display the result to the user. However, the code has bugs!Debug this code!Here are some examples of how the program should work:Example 1Enter a number: 32Enter another number: 64The sum of your two numbers is: 96.00Example 2Enter a number: 1234.5678Enter another number: 9999.99The sum of your two numbers is: 11234.56Example 3Enter a number: 23Enter another number: -532.824The sum of your two numbers is: -509.82
elect the correct answerWhat is the output of the following program?public class Score{ public static void main(String[] args) { double data = 222.423; int sum = 3; float value = 2.1f; System.out.println(data + sum + value); }}Options227.52299222222.423222.5
Daniel is working on a program for mathematical operations. Create a program that takes two floating-point numbers, multiplies them, converts the result to an integer, and based on the user's choice:If the choice is 1, add 5 to the converted integer.If the choice is 2, subtract 5 from the converted integer.If the choice is 3, divide the converted integer by 2.Display the original multiplication result as a float with two decimal places, and then show the modified integer after the chosen operation. Input format :The first line of input consists of two float values: num1 and num2 separated by a space.The second line of input consists of an integer value n, representing Daniels's choice:If the choice is 1, add 5 to the converted integer.If the choice is 2, subtract 5 from the converted integer.If the choice is 3, divide the converted integer by 2.Output format :The first line displays "Multiplication Result (as float): " followed by the result of multiplying num1 and num2 as a float value with two decimal values.The second line displays "Converted Integer: " followed by the result as an integer value after performing the required operation on the converted result based on the choice.
Problem Statement:Accept a floating point value and precision value and print the floating point value according to the precision given.Input Format:Accept one floating point and one integer valueOutput Format:print the floating point value according to the given precisionConstraints:3.4E-4932 to 1.1E+4932Sample Input :15.87 6Sample Output :15.870000
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.