Google
 
Site navigation: [ Home | Theory | Java | Moodle courses | Resource wiki | About ]

AddSub - an Applet that can Add or Subtract

source: AddSub.java

 

The Class has the same types of awt object as before.

Two TextFields hold the user input. Two Buttons for the choices; an answer Label.

 

 

 

 

Does the same as init in previous examples.

 

Since we have two buttons, both get an actionListener.

 

 

 

 

Local identifiers (sometimes called variables) hold temporary values needed by this method .

The if statement has a condition (must evaluate either to true or false). If true the first part is done, otherwise the else part is done.

The Label text is set to the answer, the + converts the int to a String automatically.

 

Related pages: [ selection | quizzes ]


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/**
* A two button Applet, can add or subtract two numbers
*
* @author Mr J
* @version 241103
*/

public class AddSub extends Applet implements ActionListener
{
   // two text fields for the numbers
   private TextField number1 = new TextField(20);
   private TextField number2 = new TextField(20);
   // two buttons for the add/subtract options
   private Button add = new Button("+");
   private Button sub = new Button("-");
   // Label for the answer
   private Label answer = new Label("The answer will appear here");

  /**
   * Add the objects to the Applet
   */

   public void init()
   {
     add(number1);
     add(number2);
     add(add);
     add(sub);
     add(answer);
     // Causes button presses to be detected
     add.addActionListener(this);
     sub.addActionListener(this);
   }
  /**
   * When an event occurs on an object with an ActionListener attached, this
   * method is carried out.
   *
   * @param e carries details about the event that occurred
   */

   public void actionPerformed(ActionEvent e)
   {
     int total = 0;
     int n1, n2;
     // convert the Strings from the TextFields to ints
     n1 = Integer.parseInt(number1.getText());
     n2 = Integer.parseInt(number2.getText());
     // check which button was pressed
     if (e.getSource()== add)
     {
       total = n1 + n2;
     }
     else // must have been sub
     {
       total = n1 - n2;
     }
     // put answer
     answer.setText("" + total);
   }
}

Pre-exercise exercises

Calculator
Can you extend this Applet to do division and multiplication? You will need to use a structure like this:

    // check which button was pressed
     if (e.getSource()== add)
     {
       total = n1 + n2;
     }
     else if (e.getSource()==sub)
     {
       total = n1 - n2;
     } 
     else if (e.getSource()==mult)
     {

etc. The final else block in this chain is done when no other options are true. You can use the howToImproveLayout techniques to add the buttons to a panel to make them look nicer.

PressMe Re-visited
Play a trick on your friend; change the PressMe Applet so that instead of saying Hello when your friend enters their name, it gives another message. NB when you make an if statement with Strings, use this form:

  if (name.equals("Ginger"))

and not

  if (name=="Ginger")

Related: [ Java Core | Selection Quizzes | previous Selection | next:Repetition ]

Teaching notes

After looking at this example students should be able to attempt most of the exercises.


 
The site is partly financed by advertising revenue, partly by online teaching activities and partly by donations. If you or your organisation feel these resouces have been useful to you, please consider a donation, $9.95 is suggested. Please report any issues with the site, such as broken links, via the feedback page, thanks.

Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy.

Creative Commons License


This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand;
This page was last modified: May 31, 2009