Transcript Lecture 13

Lecture 13
1
Today’s Topics
• Classes
– Attribute (variables)
– Behaviors (methods)
• Using methods
– How to write methods
– How to use methods
• Scope
– Private and Public
2
Introduction to Classes (review)
• A class contains
– Attributes (fields)
• Attributes are variables declared in the class
– name, size, ID, etc…
– Methods (behavior)
• Methods are behaviors of the class
– turn, forward, drawLine, etc…
3
Example of a Class Definition
• Before writing code, we can draw a
diagram of a class to outline its features
such as its name, attributes, and methods
Class Name
Turtle
List of its
Variables
- color : Color
- name : String
...
List of its
Methods
+ forward (int):
+ setPenColor (Color):
...
4
public class ClassName
{
// attributes or variables
variable1
variable2
…..
// methods or behaviors
method1
{
…
}
method2
{
…
}
…..
}
5
Method
• Write your own methods for classes!!!
– So far, we use only methods of classes which
are already provided in Java standard library
• sqrt(double), pow(double, double) from Math class
• forward(), turnRight() from Turtle class
– We can add (write) our own methods in a
class
For example
• drawSquare(), drawRobot(), etc…
6
How to write method
• Syntax (rule)
[scope] [return type] [method name] ( [arguments] )
{
[method body]
}
Scope: public,Method
private,requires
protected,
… and output
input
Return type: typeArguments
of output values
as a result
and Return
value of method body
(if
nowrite
output
values, like
we put
void)in mathematics
So,there
you is
can
a method
function
Method name:
F(x) = 2x + 1;
Formore
example,
F(3) = 7, F(-2)
-3
Arguments: 0 or
input variables
to the=method
Method body: if return type is specified, return a value at the
end of method
body by using
reserved output
word return
Method
input
7
Example of method
public class MyClass
{
private int total;
Program
Create
starts
object
from
of main
MyClass
method
public static void main(String[] args)
{
MyClass myClass = new MyClass();
int sum = myClass.addTwoNum( 2, 3 );
System.out.println( “sum is “ + sum );
}
Print
Go to the method
public int addTwoNum( int a, int b );
{
int result = a + b;
5
return result;
as
sum is 5
2+3
}
}
8
Example of method
public class MyClass
{
public static void main(String[] args)
{
World w = new World();
Turtle t1 = new Turtle(w);
Turtle t2 = new Turtle(w);
inefficient
t1.forward(50);
t1.turnRight();
t1.forward(50);
t1.turnRight();
t1.forward(50);
t1.turnRight();
t1.forward(50);
t1.turnRight();
t2.forward(100);
t2.turnRight();
t2.forward(100);
t2.turnRight();
t2.forward(100);
t2.turnRight();
t2.forward(100);
t2.turnRight();
}
}
Example of method
public class MyClass
{
Efficient
public class Turtle
{
public static void main(String[] args)
{
public void drawSquare(int len)
{
forward( len );
turnRight();
forward( len );
turnRight();
forward( len );
turnRight();
forward( len );
turnRight();
}
World w = new World();
Turtle t1 = new Turtle(w);
Turtle t2 = new Turtle(w);
t1.drawSquare( 50 );
t2.drawSquare( 100 );
}
}
}
Scope: Public & Private (1)
public class Student
{
private String name;
private char grade;
public char getGrade (int score)
{
if( score > 70 )
grade = ‘A’;
else
grade = ‘B’;
return grade;
}
}
11
Scope: Public & Private (2)
• We use the Java reserved word private to
prevent access to a variable or method from
the other classes
• We use the Java reserved word public to
allow access to a variable or method from
the other classes
12
Scope: Public & Private (3)
• Normally, we declare variables to be private
• Normally, we declare methods to be public
public
Class1 (e.g. Turtle)
Class2 (e.g. Test)
private
We can use methods of class1
- color : Color
in the class2
Accessible!!!
because
of own
We can
For
create
We
example,
cannot
class1
access
t.forward()
object
variables
inclass
class2in
class1 from class2
For example, we create Turtle object in
+ forward(int) : Test
int class
public
13
Example: Public & Private (4)
public class MyClass
{
public static void main(String[] args)
{
World w = new World();
Turtle t = new Turtle(w); ?
t.drawLine( 100 );
public class Turtle
{
private String name;
private Color penColor;
public void drawLine( int len )
{
forward( len );
setPenColor(Color.GREEN);?
}
?
t.name = “chonho”;
t.penColor = Color.BLUE; ?
t.setPenColor( Color.BLUE );
private void setPenColor(Color c)
{
penColor = c;
}
?
}
}
Exercise
1. ExerciseMethod1.java
- write a method to return the sum of two numbers
- write a method to return the difference of two numbers
2. ExerciseMethod2.java
- now, you have doCalculation method
- in the main method,
create an object of ExerciseMethod2
and use the method
15
Exercise
3. ExerciseMethod3.java and MyCalculator3.java
- Now, all methods are in ExerciseMethod3
- and MyCalculator3 contains a main method
- in the main method,
create an object of ExerciseMethod3
and call doCalculation method
16
Announcement
In homework 3
- I have found some students whose answers are
completely same…
- Same font, same program, same output, even same
wrong answers, etc …
- I don’t want to say “they cheated” because I
recommend you help your classmates each other.
- Also, I know you spend much time for doing project,
and your Java programming skill is improving.
- But, you cannot learn anything from just copying.
17
Announcement
For the rest of course
- Let’s finish Project 1 and make our Gallery
- Please try to do homework 3 again.
- We have homework 4 and 5
- Additional homework (for extra credit) if you want
- Project 2
- You can use Dr.Java to check if your answer is correct
18