{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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
COP 3330
Background image of page 1

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

View Full Document Right Arrow Icon
Topics Java Basics - How to write & call your own method - Math methods (static methods) - String methods (instance methods) - Errors Classes Objects
Background image of page 2
Writing your own methods main() is a method you’ve 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. }
Background image of page 3

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

View Full Document Right Arrow Icon
Writing your own methods Order of methods doesn’t matter (Difference from C) Don’t forget static keyword or you’ll receive error “ Cannot make a static reference to the non-static method Write code showing method calls
Background image of page 4
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.
Background image of page 5

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

View Full Document Right Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full Document Right Arrow Icon
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
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}