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

The TestFraction Applet

 

The Applet will give the result of adding two fractions together - but still lacks a great deal of meaningful functionality.

 

 

 

 

 

 

 

Nothing different here, except there are a lot more objects on the screen.

 

 

 

 

 

Set up a panel with GridLayout to hold the labels and boxes in two rows and three columns.

 

 

 

 

 

 

 

 

Call the Fraction constructor with two ints to create new Fraction objects a and b.

 

Add them together.

 

An Applet to demonstrate the working of the Fraction Class:

Source code: TestFraction.java

import java.applet.Applet;
 import java.awt.*;
 import java.awt.event.*;
 /**
 * Tests the operation of the Fraction Class
 *
 * @author Mr J
 * @version 20040212
 */

 public class TestFraction extends Applet implements ActionListener
 {
   // input boxes
   Label topLabel = new Label("Numerators:");
   Label botLabel = new Label("Demoninators:");
   TextField n1 = new TextField("n1");
   TextField n2 = new TextField("n2");
   TextField d1 = new TextField("d1");
   TextField d2 = new TextField("d2");

   private Button addButton = new Button("Add");
   private Label greeting = new Label("Applet that adds two fractions");

  /**
   * Add objects to the Applet
   */

   public void init()
   {
     Panel inputPanel = new Panel();
     inputPanel.setLayout(new GridLayout(2, 3));
     inputPanel.add(topLabel);
     inputPanel.add(n1);
     inputPanel.add(n2);
     inputPanel.add(botLabel);
     inputPanel.add(d1);
     inputPanel.add(d2);
     add(inputPanel);
     add(addButton);
     add(greeting);
     // Causes button presses to be detected
     addButton.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)
   {
     // Get the data from the boxes and make the fraction objects
     Fraction a = new Fraction( Integer.parseInt(n1.getText()),
                                Integer.parseInt(d1.getText()) );
     Fraction b = new Fraction( Integer.parseInt(n2.getText()),
                                Integer.parseInt(d2.getText()) );
     // add them together
     Fraction c = Fraction.add( a, b );
     greeting.setText("" + c.toString());
   }
}

Of course, our Fraction Class doesn't work too well, it doesn't have a lot of functionality and it doesn't reduce fractions (eg 10/25 is not reduced to 2/5 but we can always fix that later). However, hopefully it illustrates a few useful points about Classes and methods.

back to top

On to selection - how to make choices in your code.

Related: [ Java Home | previous methods & classes | next:selection ]

To use, put the fractions in the box, the result of adding them appears in the label.

Students who complete this activity quickly might like to try to write a method that reduces a fraction to its lowest common denominator.

 


 
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