Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 13. 2 Today’s Topics Classes –Attribute (variables) –Behaviors (methods) Using methods –How to write methods –How to use methods Scope –Private.

Similar presentations


Presentation on theme: "1 Lecture 13. 2 Today’s Topics Classes –Attribute (variables) –Behaviors (methods) Using methods –How to write methods –How to use methods Scope –Private."— Presentation transcript:

1 1 Lecture 13

2 2 Today’s Topics Classes –Attribute (variables) –Behaviors (methods) Using methods –How to write methods –How to use methods Scope –Private and Public

3 3 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…

4 4 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 Turtle - color : Color - name : String... + forward (int): + setPenColor (Color):... Class Name List of its Variables List of its Methods

5 5 public class ClassName { // attributes or variables variable1 variable2 ….. // methods or behaviors method1 { … } method2 { … } ….. }

6 6 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…

7 7 How to write method Syntax (rule) [scope] [return type] [method name] ( [arguments] ) { [method body] } Scope: public, private, protected, … Return type: type of output values as a result of method body (if there is no output values, we put void) Method name: Arguments: 0 or more input variables to the method Method body: if return type is specified, return a value at the end of method body by using reserved word return Method requires input and output Arguments and Return value So, you can write a method like function in mathematics F(x) = 2x + 1; For example, F(3) = 7, F(-2) = -3 Method inputoutput

8 8 Example of method public class MyClass { private int total; public static void main(String[] args) { MyClass myClass = new MyClass(); int sum = myClass.addTwoNum( 2, 3 ); System.out.println( “sum is “ + sum ); } public int addTwoNum( int a, int b ); { int result = a + b; return result; } Print as sum is 5 Program starts from main method Go to the method 5 Create object of MyClass 2 + 3

9 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); 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(); } inefficient

10 Example of method public class Turtle { public void drawSquare(int len) { forward( len ); turnRight(); forward( len ); turnRight(); forward( len ); turnRight(); forward( len ); turnRight(); } public class MyClass { public static void main(String[] args) { World w = new World(); Turtle t1 = new Turtle(w); Turtle t2 = new Turtle(w); t1.drawSquare( 50 ); t2.drawSquare( 100 ); } Efficient

11 11 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; }

12 12 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

13 13 Scope: Public & Private (3) Normally, we declare variables to be private Normally, we declare methods to be public Class1 (e.g. Turtle) - color : Color + forward(int) : int Class2 (e.g. Test) public private public We can create class1 object in class2 For example, we create Turtle object in Test class We cannot access variables in class1 from class2 We can use methods of class1 in the class2 For example, t.forward() Accessible!!! because of own class

14 Example: Public & Private (4) public class Turtle { private String name; private Color penColor; public void drawLine( int len ) { forward( len ); setPenColor(Color.GREEN); } private void setPenColor(Color c) { penColor = c; } public class MyClass { public static void main(String[] args) { World w = new World(); Turtle t = new Turtle(w); t.drawLine( 100 ); t.name = “chonho”; t.penColor = Color.BLUE; t.setPenColor( Color.BLUE ); } ? ? ? ? ?

15 15 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

16 16 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

17 17 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.

18 18 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


Download ppt "1 Lecture 13. 2 Today’s Topics Classes –Attribute (variables) –Behaviors (methods) Using methods –How to write methods –How to use methods Scope –Private."

Similar presentations


Ads by Google