|
|
||
| You are here: | ||
|
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; /** 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. 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.
|
|
|
|||
|
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. 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 |