Object Oriented Programming (OOP) LAB # 5

Slides:



Advertisements
Similar presentations
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Advertisements

Copyright © Texas Education Agency, Computer Programming Class Basics.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
© Vinny Cahill 1 Classes in Java. © Vinny Cahill 2 Writing a Java class Recall the program to calculate the area and perimeter of a rectangle of given.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
30-Jun-15 Static Methods. About methods A method is a named group of declarations and statements You execute those declarations and statements by calling.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Introduction to Methods
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
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.
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
Chapter 4 Procedural Methods. Learning Java through Alice © Daly and Wrigley Objectives Identify classes, objects, and methods. Identify the difference.
Introduction to Computers and Programming Lecture 14: User defined methods (cont) Professor: Evan Korth New York University.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CS 106 Introduction to Computer Science I 10 / 29 / 2007 Instructor: Michael Eckmann.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.
Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Written by: Dr. JJ Shepherd
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Methods.
Topics Instance variables, set and get methods Encapsulation
OOP Basics Classes & Methods (c) IDMS/SQL News
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Object Oriented Programming (OOP) LAB # 3 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Functions + Overloading + Scope
Objects as a programming concept
Understand the Fundamentals of Classes
Computer Programming Fundamentals
Chapter 5 Classes.
Programming Language Concepts (CIS 635)
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
Chapter 4 Procedural Methods.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Static Methods 14-Nov-18.
Object Oriented Programming (OOP) LAB # 8
A simple example of program design
Bases and Representations, Memory, Pointers, Arrays, For-Loops
Coding Concepts (Basics)
Classes Lecture 7 from Chapter /1/11.
Object Oriented Programming (OOP) LAB # 9
Chapter 7 Procedural Methods.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
COP 3330 Object-oriented Programming in C++
Java Programming Language
Object Oriented Programming
Final and Abstract Classes
Methods/Functions.
CPS125.
ITE “A” GROUP 2 ENCAPSULATION.
CSG2H3 Object Oriented Programming
Presentation transcript:

Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Methods

Method A Java method is a collection of statements that are grouped together to perform an operation. In java, Methods must be defined within a class. When you use methods: Programs will be easier to understand Programs will be easier to change, if needed Programs will be easier to write, test, and debug Programmers don’t need to repeat the same instructions many times.

Defining a Method in java (access modifier) When you declare a method, the first thing you typically provide is called the access modifier. You can use one of three terms, public, private, or protected. Public means that the method is available to the entire application or at least to any part of the application that can see the class itself. So if the class is public and the method is public, then it can be called from anywhere. Private is the opposite. The method is only available to code within this class. Protected has to do with inheritance. A protected method is available to the current class and to any of its sub classes.

Defining a Method in java (static method or instance method) If you want the method to be static, you can put the word static after the access modifier you choose . And if you don't want to it be static , don’t put any thing. When do you want a method to be static? the term static means that the method is also called a class method, as opposed to an instance method. A class method is called directly from the class definition, whereas an instance method is called from an instance of the class or an object. For example: (next slide)

Defining a Method in java (static method or instance method)- example

Defining a Method in java (void or other variable type) After choosing the method to be static or not, you need to declare if it returns a value or not. If the method doesn’t return any value, it should be defined as void function. If it returns a value, it will take the same type of the variable it returns.

Defining a Method in java (function name) After declaring the method as void or not, you should write name for your function. it's important to follow the right conventions when you name your methods and variables. The initial character must be lowercase or other java developers will think you don't know what you are doing, and it must be an alphabetical character, not numeric. You can also use underscores, but that's fairly in frequent.

Defining a Method in java (arguments) The method can take arguments or not. For example, if you want to write a method that adds two numbers, the arguments passed into the function should be the two integer variables. But if you want to write a function that prints a statement, it can take no argument.

Defining a Method in java (statements) At the end of the method name with its arguments you always add opening and closing parenthesis. You can write the instructions inside your method which you want the function to perform.

Exercise #1 Write a method that used to print even numbers from 1 to 10.

Define the main class as usual Define the main function. Static method. So, we can call it easily from the main function. Call the function in the main method. the method’s name is printeven. This function doesn’t have arguments. Private method. So, it is only seen inside Printing class which is the class that it is defined inside. Void function. It returns no value. Set of statements to print even numbers in the range (1-10).

So, when we called it in the main function, we defined an object within the same class and then call the function. The method here is instance method.

Testing#1

Exercise#2 Write a java method which returns a rectangle area. Hint: Rectangle Area= width*height

Define the main class as usual Define the main function. Static method. So, we can call it easily from the main function. int means that it will return integer number. Call the function in the main method. Private method. So, it is only seen inside Area class which is the class that it is defined inside. the method’s name is rectanglearea. This function has two arguments (height and width). Compute rectangle area and return it.

Testing #2

The End !!