180 likes | 188 Views
This lecture covers the basics of classes in Java, including attributes (variables), behaviors (methods), and how to write and use methods. It also covers the concepts of scope and public/private access modifiers.
E N D
Today’s Topics • Classes • Attribute (variables) • Behaviors (methods) • Using methods • How to write methods • How to use methods • Scope • Private and Public
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…
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):. . .
public class ClassName { // attributes or variablesvariable1 variable2 ….. // methods or behaviors method1 { … } method2 { … } ….. }
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…
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 input output
Example of method Program starts from main method Create object of MyClass 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; } } 5 Go to the method Print as sum is 5 2 + 3
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
Example of method Efficient 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 ); } } public class Turtle { public void drawSquare(int len) { forward( len ); turnRight(); forward( len ); turnRight(); forward( len ); turnRight(); forward( len ); turnRight(); } }
Scope: Public & Private (1) public class Student { privateString name;privatechar grade; public char getGrade(int score) { if( score > 70 ) grade = ‘A’; else grade = ‘B’; return grade; } }
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
Class1 (e.g. Turtle) Class2 (e.g. Test) - color : Color + forward(int) : int Scope: Public & Private (3) • Normally, we declare variables to be private • Normally, we declare methods to be public public private We can use methods of class1in the class2 For example, t.forward() Accessible!!! because of own class We can create class1 object in class2 For example, we create Turtle object in Test class We cannot access variables in class1 from class2 public
? ? ? ? ? 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 ); t.name = “chonho”; t.penColor = Color.BLUE; t.setPenColor( Color.BLUE ); } 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; } }
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
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
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.
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