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

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
CSCI 160 Midterm Review Rasanjalee DM.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Road Map Introduction to object oriented programming. Classes
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Introduction to Methods
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Agenda Object Oriented Programming Reading: Chapter 14.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Applications Development
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Classes - Intermediate
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Topics Instance variables, set and get methods Encapsulation
OOP Basics Classes & Methods (c) IDMS/SQL News
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Class Definitions and Writing Methods Chapter 3 10/12/15 & 10/13/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Structures and Classes
Classes and Objects.
Class Definitions and Writing Methods
Chapter 3: Using Methods, Classes, and Objects
Creating Your OwnClasses
Classes and Objects 2nd Lecture
Classes and Objects: Encapsulation
Classes and Objects Encapsulation
Lesson A4 – Object Behavior
Building Java Programs
Object Oriented Programming (OOP) LAB # 5
Classes, Encapsulation, Methods and Constructors (Continued)
Object Oriented Programming (OOP) Lecture No. 13
CMSC 202 Classes and Objects.
Introduction of Programming
© A+ Computer Science - Classes And Objects © A+ Computer Science -
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Object-Oriented Programming
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Dr. R Z Khan Handout-3 Classes
Chapter 9 Introduction To Classes
Building Java Programs
CMSC 202 Encapsulation Version 9/10.
Classes and Objects Object Creation
ITE “A” GROUP 2 ENCAPSULATION.
CSG2H3 Object Oriented Programming
CMSC 202 Constructors Version 9/10.
Presentation transcript:

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

HKCT Java OOP 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

HKCT Java OOP 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

HKCT Java OOP 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

HKCT Java OOP 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

HKCT Java OOP 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

HKCT Java OOP 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.

HKCT Java OOP 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

HKCT Java OOP 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

HKCT Java OOP 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

HKCT Java OOP 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

HKCT Java OOP 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 Also, when the user defined constructor exists: public MyDate(int m, int d, int y) will the default constructor MyDate() still work? 12

HKCT Java OOP Brian Lai Unit 02 End of Unit 02 13