9_12%20Methods%2c%20Errors%2c%20Classes%2c%20Objects

9_12%20Methods%2c%20Errors%2c%20Classes%2c%20Objects - COP...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COP 3330 Topics Java Basics- How to write & call your own method- Math methods (static methods) - String methods (instance methods)- Errors Classes Objects Writing your own methods main() is a method youve all already used- public static void main(String args) To write your own methods, use the same syntax public static void myMethod() { // Do something here. } public static int myIntMethod() { // Do something here and return an int. } Writing your own methods Order of methods doesnt matter (Difference from C) Dont forget static keyword or youll receive error Cannot make a static reference to the non-static method Write code showing method calls Math Methods These are static methods. That means they are called on the Math class- abs() : Takes in an int and returns its absolute value- sin(), cos(), tan() : Take in doubles and perform trigonometry. These functions use radians, not degrees- PI : A static final variable that is closer to than any other double. Math Methods More Math methods- pow() : Takes in two doubles and returns the first raised to the power of the second- random() : Takes in no parameters and returns a random double in the range from 0.0 to 1.0, including 0, but excluding 1.- sqrt() : Takes in a double and returns its square root- Show example Math code String Methods Java Strings are immutable. That means that once they're created, they can't be altered Calling methods on Strings will return new Strings, but leave the original intact String Methods These are instance methods (that means they're called on String objects)- concat() : Takes in a String and returns a new String. Equivalent to +- replace() : Takes in two chars and returns a new String. The first char is the character to be replaced, and the second char is the character to replace it with String Methods...
View Full Document

This note was uploaded on 09/21/2011 for the course COP 3330 taught by Professor Staff during the Fall '08 term at University of Central Florida.

Page1 / 34

9_12%20Methods%2c%20Errors%2c%20Classes%2c%20Objects - COP...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online