Week 7 Power Point Slides

Download Report

Transcript Week 7 Power Point Slides

Blue Box Diagram
Definition: A method is a block of instructions in a class or object that is called by name
Instance Variables
(side effects)
We pass (give)
The method
information
(expressions)
as arguments
to use
Method
contains
a
block
of
instructions
Out can come
a value that is
returned
Processing is done in the blue box
Parameters and
x = triple(10);
Arguments
• Parameter:
name a function gives an
expression used to call a method
First Call
public int triple(int x) 30
{ return x*3; }
Second Call
x = triple(20); public int triple(int x) 60
• Argument:
{ return x*3; }
expression used to call a method
• Why the difference?
– We might want to use the
method more than once.
– We might want to call a
function using literals
int z = 20;
x = triple(z);
x = triple(triple(10));
Third Call
public int triple(int x)
{ return x*3; }
60
Fourth Call
public int triple(int x) 90
{ return x*3; }
General Class Syntax
<public, private, protected> class <className>
{
… Your instance variables here …
<public, private, protected> <mods><type><methodName>(<parameters>)
{
…. Instructions here …
}
∙
∙
∙
<public, private, protected> <mods><type><methodName>(<parameters>)
{
…. Instructions here …
}
}
Example that we did in labs
public class Grader
{ public final static int LABMAX = 90;
public static void main(String[] args)
{ … instructions …}
}
Note: protected is something that can be inherited by a child class
(This is a CS 257 topic – memorize the definition for now.
Methods
Definition: Block of instructions called (executed) by name
•
Static method
–
–
•
Non static method
–
–
–
•
It is using the class name
Call round() method in Math class :
System.out.println(Math.round(4.5) ;
It is called using an instantiated object
String str = “abc”;
Call the length() method in str, an instantiated object of type String
System.out.println(str.length();
Java has a huge class library .
–
–
We can use this library to access methods that do all sorts of useful
things.
See: http://java.sun.com/j2se/1.5.0/docs/api/
Dot notation: The syntax to access methods inside classes and objects
More on static and non-static
public class MyClass
{ public static void main(String[] args)
{
MyClass data = new MyClass(); // Make instance of MyClass().
String s1 = data.name();
// The name method executes.
String s2 = name();
// Error – name() is not static .
String s3 = name2();
// OK – name2() is a static method.
String s4 = MyClass.name2(); // OK – name2() is a static method.
System.out.println(s1);
}
public String name() { return “Dan ” + doIt(); }
public String doIt() { return “Harvey”; }
public static String name2()
{ return “Static Dan Harvey”; }
}
Note: when we call methods in the same class, dot notation is not needed.
A method returning a value
public class ReturnValue
{ private int y = 10;
public static void main(String[] args)
{
ReturnValue myObject = new ReturnValue();
int x = myObject.doReturn();
System.out.println(x);
}
private int doReturn()
{
int x = 33 + y;
return x;
What prints?
Where is x visible? Where is y visible?
}
Is x in doReturn the same variable as in main?
}
Which are the instance variables?
A method with parameters
public class LotsOfParams
{ protected static int x = 3;
public static void main(String[] args)
{
LotsOfParams params = new LotsOfParams();
int y = x + 4;
int x = params.doSomething
(1,2,x + y -3,4,params.doSomething(x, y, 3, 4, 5));
}
private int doSomething(int a, int b, int c, int d, int e)
{
System.out.println(“” + c + b + d + a + e );
return a + b + c + d + e;
}
What prints? Why private? Which are the arguments?
}
Which are the parameters (formal arguments)?
Which are instance variables? What are the methods?
Pass by value
• A copy of arguments gets passed to a method,
not the variable itself
• Example
1. Call: int x = 5; tryToChange(x);
2. Method:
private void tryToChange(int x)
{ x = 3; }
3. Result: x in the call does not change! The x in the
method only lives between the method’s braces.
Question: Why doesn’t x change?
Method to search an array
public class FindMe
{ String[] list = {“hello”, “this”, “is”, “a”, “good”, “day”};
public static void main(String[] args)
{
FindMe find = new FindMe();
System.out.println(find.isItThere(“is”));
System.out.println(find.isItThere(“cat”));
}
private int isItThere(String which)
{
for (int i=0; i<list.length; i++)
{ if (list[i].equals(which)) return i; }
return -1;
}
}
Question: Why do we need find in find.isItThere(“is”));
Method to remove and add
public class RemoveMe
{ String[] list = {“hello”, “this”, “is”, “a”, “good”, “day”};
int howMany = list.length;
public static void main(String[] args)
{
removeIt(3);
addIt(“greetings”); }
private static void removeIt(int which)
{
for (int i=which; i<howMany-1; i++)
{ list[i] = list[i+1]; }
howMany--; }
public static boolean addIt(String newOne)
{
if (howMany == list.length) return false;
list[howMany++] = newOne;
} }
Review
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
What is a method?
What is an instance variable?
What is the difference between an argument an a parameter?
What does it mean to limit scope?
What is an argument?
What is a side effect?
What does private and public mean?
What is the definition of protected?
How do you return a value from a method?
What does pass by value mean?
What is the difference between an object variable and primitive
variable?