|
|
||
| You are here: | ||
|
Methods and parameters So far there does not seem to be any great advantage!
Remember the exercises on the String methods substring and length - they will help you with the formatting. (You would have used these when printing star patterns - if you did that exercise) see also this page
To work out whether one number is wholly divisible by another number, use the modulo operator (%), which gives the remainder after integer division: int remainder = x % y; if remainder = 0 then y divides wholly into x. This is the most difficult exercise. For those who really like a challenge, how should we deal with vulgar fractions like 23/5 - that should look like 4 3/5. (You may need another data member in the Fraction Class ) |
Here and elsewhere we have mentioned methods and parameters . This page attempts to show how they can be used as building blocks of larger programs. We will use this technique again and again. A helper method is a way of "tidying" a logical group of statements together. As the first example we will look at getting int values from a TextField - this is a good candidate because it is something we have done a lot: int x = Integer.parseInt(number1.getText()); We could write a method that does this as follows: private int getInt(TextField theField) We could then use this code in the Applet that adds two numbers together: int answer = getInt(number1) + getInt(number2); where number1 and number 2 are declared as TextField's. Often we want to get only numbers that are within a given range (this is called validation ), for example a year between 1950 and 2004: private int getInt(TextField theField, int min, int max) This example uses the special value -1 to indicate that the user did not type in a value in the expected range. We can use this in a program, for example: int year = getInt(yearField, 1950, 2004); We could extend this example with a do while loop: int year; Exercise - How Old re-visited Using the above helper method(s) Construct an Applet that accepts a date of birth between 1950 and 1999. Given that the year is 2004 (or amend as appropriate), calculate and display the person's age. Do not allow them to enter an age outside the range. Why use Helper Methods? Helper methods are used because, once written and tested:
Put these ideas into practice in the next set of exercises : Light Plane A light airplane has to be loaded with a maximum of 6 passengers. The combined weight of the passengers cannot be more than 400 kg. design an Applet that allows up to 6 passenger weights to be input until, either 6 have been input or the 400 kg limit has been exceeded, whichever comes first. Your Applet should output the number of passengers and their total weight. Times Tables Write an Apple that displays a given times table. Allow the user to select the table they want, the start value and end value for the table. display the result, formatted nicely, in a TextArea. Thus if the user selected 7x table, starting value 7 and ending value 10, we would see something like this: 7 x 7 = 7 Prime numbers An Applet can work out if a number, x, is prime by dividing it succesively by (n-1, n-2, n-3) etc and seeing if there is any remainder. If the number is divisible by any other number than 1 and x, it is prime. Design an Applet that allows the user to enter two numbers and decide whether the sum of the two is prime or not.
Fraction - improved The Fraction class did not reduce fractions to the lowest possible denominator, eg if our calculator was given the sum 1/4 + 1/4 it would give the answer 2/4 instead of 1/2. Similar to the prime operation, starting with the numerator, you could test if it divides into the denominator exactly. If it does not then reduce the numerator and try again. Continue until 1 is reached. For example, consider the fraction 12/32. Does 12 divide into 32, no, then try 11, 10, 9 etc. We find that 4 divides into both, do the reduction to 3/8. Add a method to the Fraction Class to implement this and test it works with the Applet TestFraction . Related: [ Java Core | previous repetition | next:Finally] |
Jargon: This code of Lily's may help you to understand some of the terms.
|
|
|
|||
|
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 - 2007 Richard Jones, PO BOX 246, Cambridge, New Zealand; This page was last modified: July 29, 200823, 2008 |