Lecture Notes, class 7 for csc211.
Prepared by: Anthony Larrain
These notes serve as an outline to the lecture, they are not intended to be complete. You should be reading the assigned sections. During class I may include or omit certain topics.
Classes
Recall all Java programs are built from classes.In Java a class is used in several way's.
- To store a main method to launch an application.
- To create a data type from which objects are created.
- To store some static methods to be used by other classes.
- Some combination of the above.
- Servlets and Applets
Stack and the Heap
- The Stack - Where local variables live.
- The Heap - Where Objects live.
Writing Class or Static Methods
- A method is a named group of statements that perform a task.
- A method is always a member of a class.
- Two types of methods.
- Class or Static methods.
- Instance methods.
- In this section we will focus on writing class or static methods.
- Suppose we wanted to write a method to compute the average of two integers.
- Here is what the method would look like.
public static double averageOfTwo(int first, int second){ double result = (first + second) / 2.0; return result; }
- Notice the variable result is only used once and really not needed, so the body of the method could be written as ..
- All static methods will have this form.
access_modifier static returnType methodName( parameterList ) { // method body }
- A parameter list would be local variable declarations used to pass data into a method.
- Access_ modifiers -
public
andprivate
- To use this method
- We can put this method
averageOfTwo
in one of two classes.- After or before the class that contains the
main
method. - In its own class.(Write another class with a
main
method that uses the above class).Then use the name of the class, together with the dot '.' operator to call the method.
For now we will focus on the class that contains - After or before the class that contains the
- Some methods do not require a parameter list.
- Suppose we wanted to write a method that returned "Hello World"
public static String greeting(){ return "Hello World"; }
public static double averageOfTwo(int first, int second){ return (first + second) / 2.0; }
main
class Average{ public static void main(String [] args){ double result = averageOfTwo(2,3); System.out.println("The average of 2 and 3 is " + result); } public static double averageOfTwo(int first, int second){ return (first + second) / 2.0; } }
Exercises
For the following exercises I will place all the method definitions in a class called ClassMethods. This is strictly for convenience. Typically methods should be grouped together under class names when they are related in some way. Like the Math functions.
- Write a method that will print to the console 50 times, the phrase..
warning warning danger danger
- Write a method called
isAnInt
that accepts a String parameter and returns true if the characters of the String are all digits, false otherwise.
Arrays In Java
Consider the problem, suppose we wanted to find the average of a collection of scores and print out a table showing each score and its distance from the mean.
- A data structure used to group a collection of values of the same type using a single name.
- Each member can be accessed by an index(Starts at 0).
- In Java an Array is an Object (Special).
- Allocated Dynamically.
- Arrays have a public instance variable called length which specifies the capacity.
- The Capacity of an array object cannot change after it is created.
- Runtime Bounds Checking.
- Examples: