Professional Documents
Culture Documents
Covers information found in Chapter 6 and Chapter 7 An Object-Oriented Approach To Programming Logic and Design
Methods
Similar to math functions f(x) = x2 May take input May produce an output Object method
Implicitly passed to the current object Sent as a message to an object of a class Example: objectReference.methodName(); Defined by using the keyword static Sent as a message to a class Example: ClassName.methodName(); System.out.println();
Class method
Class methods are executed by using the class name followed by a period and then the method name
ClassName.methodName();
Like Character, System, or Math The Character class provides general character methods Characters in Java are specified between single quotes: A
In DrJavas interaction pane type the following and note the results
Class methods
objectReference.methodName();
Java has a String class that is used to represent the list of characters (like letters in a persons name)
Whenever the compiler sees string literals (characters enclosed with double quotes), objects of the String class are created Double quotes tell the compiler that this is an object of the String class and not a variable name Barbara or cat.jpg
In DrJavas interaction pane the following and note the results (okay to type on one line, rather than 2)
Object methods
String name = Fred Farmer; System.out.println(name); String lowerName = name.toLowerCase(); System.out.println(lowerName); String upperName = name.toUpperCase(); System.out.println(upperName); System.out.println(name); Even though we manipulated the appearance of name, why didnt the value of name change in the example above?
If you are sending data along with a message it goes inside the parentheses
Common Errors
Did you make any mistakes when you typed in the examples?
Use the up arrow key in DrJava to bring up the previous statement and fix it
Objects are created with space for their data Objects have a reference to the object that represents the class
Food : Class Name = Food Fields = Name, Price Methods = getName, setName, getPrice, setPrice, getCalories
Meaning that double isnt the same as Double Or string isnt the same as String double, float, int, So String and System are the names of classes
Which of these are primitive types, and which are the names of classes?
API Exercise
The Classes defined as part of the Java language are documented in the API (Application Programming Interfaces)
http://docs.oracle.com/javase/6/docs/api/ A shortcut to the above link is in the assignments folder Click on it now to open the webpage String and Math
Java Packages
Common Packages
java.io
Contains classes for input and output Contains basic user interface classes Contains more advanced user interface classes
java.awt
javax.swing
In the API documentation how can you tell which are class methods and which are object methods?
Look for the keyword static on the method If it has the keyword static then it is a class method If there is no keyword static then it is an object method
All other names start with lowercase letters but uppercase the first letter of each additional word
Assignment
The API will be the first place you need to look when you are not sure where to begin on programming assignments.
int length = name.length(); System.out.println(length); Deal with the error message so that you have some code that will work. In order to deal with the error, you will need to use the API to determine which class is being used and understand what is happening. HINT: the object name is descriptive!
Read Chapters 2 and 3 in the textbook Quiz will most likely be next Thursday or Friday