You are on page 1of 16

Java Basics

Covers information found in Chapter 6 and Chapter 7 An Object-Oriented Approach To Programming Logic and Design

Georgia Institute of Technology

Methods

Classes in Java define methods


Similar to math functions f(x) = x2 May take input May produce an output Object method

Two Types of methods

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

Invoking Class Methods

Class methods are executed by using the class name followed by a period and then the method name

ClassName.methodName();

By convention, class names in Java begin with an uppercase letter


Like Character, System, or Math The Character class provides general character methods Characters in Java are specified between single quotes: A

Class Method Exercise

In DrJavas interaction pane type the following and note the results

Class methods

Math.abs(13) Math.abs(-13) Math.min(3,4) Character.getNumericValue('A')

Invoking Object Methods

Object methods must be executed on an object using:

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

Object Method Exercise

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?

Message Always Have Parenthesis

You can tell that out.println() is sending a message

Because of the () Even if there are no parameters (arguments)

Messages always have ()

If you are sending data along with a message it goes inside the parentheses

Separated by commas Math.min(3,4);

Common Errors

Did you make any mistakes when you typed in the examples?

If you use the wrong case it wont work


> math.abs(-3) Error: Undefined class 'math

If you misspell something it wont work


> Mat.abs(-3) Error: Undefined class 'Mat > Math.ab(-3) Error: No 'ab' method in 'java.lang.Math'

Use the up arrow key in DrJava to bring up the previous statement and fix it

What do Objects Look Like?

Objects are created with space for their data Objects have a reference to the object that represents the class

Fries: Food Name = Fries Price = 1.99

Waffles: Food Name =Waffles Price = 2.99

Food : Class Name = Food Fields = Name, Price Methods = getName, setName, getPrice, setPrice, getCalories

Object of the class Class

Java is Case Sensitive

Some programming languages are case sensitive

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

In Java primitive types are all lowercase

Class names start with an uppercase letter

Identifying Classes Exercise

Which of these are primitive types, and which are the names of classes?

int Picture char Double Math double Integer String

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

Find the documentation for the following classes:

Java Packages

Java groups related classes into packages

View in frames option java.lang

Common Packages

Contains basic classes for the language

System, Math, Object,

java.io

Contains classes for input and output Contains basic user interface classes Contains more advanced user interface classes

java.awt

javax.swing

Class Methods versus Object Methods

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

Java Naming Conventions

In Java only Class names start with an uppercase letter

System, BufferedImage, Picture

All other names start with lowercase letters but uppercase the first letter of each additional word

picture, fileName, thisIsALongName

Assignment

Using the API documentation online:

Find documentation for the methods in Java classes

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!

Try the following in DrJavas interaction pane:

Complete the reading assignment for next weeks quiz


Read Chapters 2 and 3 in the textbook Quiz will most likely be next Thursday or Friday

You might also like