-
New to Java, help with an Applet
Hello. I'm taking an intro to computer science class, and I'm hung up on a homework assignment.
Here's the assignment:
Write an applet that has the user enter two numbers, calculates the sum of the numbers, and displays the sum as an output.
Any ideas?
Thanks,
medicmatt
-
Lots of ideas.
What have you done so far?
-
Find the sum of numbers in applet
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="sum.class" height=200 width=200>
</applet>
*/
public class sum extends Applet implements ActionListener
{
int no1,no2,sum=0;
TextField text1,text2,text3;
Label label1,label2,label3;
public void init()
{
label1=new Label("Enter First Number:");
text1=new TextField(20);
add(label1);
add(text1);
label2=new Label("Enter Second Number:");
text2=new TextField(20);
add(label2);
add(text2);
label3=new Label("The Sum is:");
text3=new TextField(20);
add(label3);
add(text3);
text2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
no1=Integer.parseInt(text1.getText());
no2=Integer.parseInt(text2.getText());
sum=no1+no2;
text3.setText(Integer.valueOf(sum).toString());
}
}
/* note:hit enter after entering the second number*/
-
How is this working for you?
Similar Threads
-
Replies: 9
Last Post: 09-19-2007, 05:58 AM
-
Replies: 1
Last Post: 03-28-2007, 12:02 AM
-
By SvenLittkowski in forum Java
Replies: 3
Last Post: 09-22-2006, 02:19 PM
-
Replies: 1
Last Post: 02-20-2001, 01:47 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks