1 / 18

Introduction to Classes: Attributes, Behaviors, and Methods

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.

taing
Download Presentation

Introduction to Classes: Attributes, Behaviors, and Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 13

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

  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. 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):. . .

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

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

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

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

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

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

  14. ? ? ? ? ? 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; } }

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

More Related