Method Practice
We learned about methods in grade 11, but not as well as we should have, so here's some review and practice.
Methods Info
- Method names are normally verb-like and are always in lower-camel-case. e.g.
isWinner()
or drawGraphics()
or attackEnemyOnlyAtNight()
- Read over the notes in unit3 from the grade 11 notes
- Have a look at codingbat.com/java if you need more practice writing methods.
- Methods can take zero, one, or many parameters but can only return one thing (or zero).
Practice
Make a class called MethodPractice (or something) and then from public static void main()
run (or call) the following methods.
Write methods that do the following:
- take an int and return its reciprocal as a double
- take a string and return the length of the string as an int
- take nothing, return nothing, just print "YOLO"
- take nothing, return a random number between 1 and 99
- take a boolean and an int and return a string
- returns a double with the value of NaN or infinity.
Use the output of some of these in your main() method.