New to Java, help with an Applet


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: New to Java, help with an Applet

  1. #1
    Join Date
    Apr 2009
    Posts
    1

    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

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Lots of ideas.

    What have you done so far?

  3. #3
    Join Date
    May 2009
    Posts
    2

    Smile 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*/

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    How is this working for you?

Similar Threads

  1. Java book
    By Lou in forum Java
    Replies: 9
    Last Post: 09-19-2007, 06:58 AM
  2. Replies: 1
    Last Post: 03-28-2007, 01:02 AM
  3. Page-Loading Java Applet
    By SvenLittkowski in forum Java
    Replies: 3
    Last Post: 09-22-2006, 03:19 PM
  4. Java Applet won't work via browsers
    By Graham in forum Java
    Replies: 1
    Last Post: 02-20-2001, 02: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center