Presentation is loading. Please wait.

Presentation is loading. Please wait.

HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.

Similar presentations


Presentation on theme: "HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1."— Presentation transcript:

1 HKCT Java OOP 2017-18 Brian Lai Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1

2 HKCT Java OOP 2017-18 Brian Lai Unit 02 Methods In other computer languages functional modules are called functions or subroutines. In Java, we call them Methods. Sending a message to a method in object oriented programming means calling a function in structural computer languages, such as, C. The “main()” method is the entry point to all Java programs. We can add methods in a class. For example, we add playGame() method in GuessANumber class. Both “main()” and “playGame()” methods are static and belong to a class. 2

3 HKCT Java OOP 2017-18 Brian Lai Unit 02 Static Method Example class SquareMethod { public static void main (String[] args) { int squareOfNine; squareOfNine = squareANumber(9); // static method call and return here. System.out.println("Square of nine is: "+squareOfNine); } public static int squareANumber(int number) { int result; result = number*number; return result; // multiplication answer returns to the caller } 3 Parameter Listt

4 HKCT Java OOP 2017-18 Brian Lai Unit 02 Method Return type is int for the squareANumber method. Return type could be void if nothing to return. Parameter list will be followed after the method name. The parameter list should not be more than five parameters. Methods should perform one task that matches the name. Methods are used to decompose more complex problems into simpler and easier problems. Again, a method should not be long. One page long is a good guideline. 4

5 HKCT Java OOP 2017-18 Brian Lai Unit 02 Void Return Type 5 class SquareMethod { public static void main (String[] args) { int squareOfNine; int inputValue = 9; squareOfNine = squareANumber(inputValue); outputMessage(); System.out.println("Square of nine is: "+squareOfNine); } public static void outputMessage() { System.out.println(“My Square Number program”); } public static int squareANumber(int number) { int result; result = number*number; return result; } void return typet integer return typet

6 HKCT Java OOP 2017-18 Brian Lai Unit 02 Classes and Objects A class is a user defined data type. Sometimes, people call it Abstract Data Type (DAT). An object is an instance of the class. An object is created based on the definition of a class. There is only one class definition but many similar objects. Opposite to user defined data type, we have Java pre-defined (or build-in) data types, such as, int, and double. From the definition of int and double, we can create many int and double variables. Classes and objects are the direct mapping of the real world. Therefore, object oriented design and programming makes our life easier. 6

7 HKCT Java OOP 2017-18 Brian Lai Unit 02 Your First Class: MyDate class MyFirstClass{ public static void main(String[] args) { MyDate birthDay = new MyDate(); MyDate myBirthDay = new MyDate(); birthDay.setMonth(11); System.out.println(birthDay.getMonth()); MyBirthDay.setMonth(12); System.out.println(MyBirthDay.getMonth()); } // main } // MyFirstClass 7 class MyDate { // private variables used inside the class only private int month, day, year; // getter method (or accessor method) public int getMonth() { return month; } // getter // setter method (or mutator method) public void setMonth(int m) { month = m; } // setter } // MyDate This class should be created in a separate file.

8 HKCT Java OOP 2017-18 Brian Lai Unit 02 Your First Class: MyDate Getters and setters encapsulate and protect data in a class. How do we protect the class from invalid month input? Also, there are day and year getters and setters are missing. As an exercise, please add these methods in the MyDate class. In addition, the declaration of birthDay: MyDate birthDay = new MyDate(); calls MyDate() Java default method which is constructor. However, we can override the default constructor by our own constructor. 8

9 HKCT Java OOP 2017-18 Brian Lai Unit 02 User defined Constructor class MyDate { private int month, day, year; // private variables used inside the class only public MyDate(int m, int d, int y) { // user defined constructor month = m; day = d; year = y; } public int getMonth() { // getter method (or accessor method) return month; } public void setMonth(int m) { // setter method (or mutator method) month = m; } } // Date 9

10 HKCT Java OOP 2017-18 Brian Lai Unit 02 User defined Constructor class MyFirstClass{ public static void main(String[] args) { MyDate birthDay = new MyDate(11,30, 2000); MyDate myBirthDay = new MyDate(); // will this work? birthDay.setMonth(10); System.out.println(birthDay.getMonth()); } } // MyFirstClass 10

11 HKCT Java OOP 2017-18 Brian Lai Unit 02 Constructor When we execute the new object statement: MyDate birthDay = new MyDate(); The default constructor will allocate memory for the object, birthDay, and initialize the private variables, month, day, and year to zero. However, when the user defined constructor is called: MyDate birthDay = new MyDate(11,30,2000); The user defined constructor will allocate memory for the object, birthDay, and initialize the private variables, month, day, and year according to the parameters passing into the constructor. Please also write an output method to print the birthDay object. 11

12 HKCT Java OOP 2017-18 Brian Lai Unit 02 Lab 2 Based on the MyDate class, please add setters and getters for day and year. For the setter, setMonth(), please verify that input month is in the range of 1 to 12. Similarly, for the setter, setDay(), please verify that input day in the range of 1 to 31, 30, or 28 for the corresponding months. For the setter, setYear(), also limit the year from 1900 to 2014. Also, when the user defined constructor exists: public MyDate(int m, int d, int y) will the default constructor MyDate() still work? 12

13 HKCT Java OOP 2017-18 Brian Lai Unit 02 End of Unit 02 13


Download ppt "HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1."

Similar presentations


Ads by Google