Notes on Chapter Three

Similar documents
1B1b Classes in Java Part I

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Classes and Methods: Classes

Chapter 4 Defining Classes I

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Object Oriented Programming SCJ2153. Class and Object. Associate Prof. Dr. Norazah Yusof

ENCAPSULATION AND POLYMORPHISM

Principles of Object Oriented Programming. Lecture 4

Chapter 5: Classes and Objects in Depth. Information Hiding

Example: Fibonacci Numbers

Distributed Systems Recitation 1. Tamim Jabban

Programming II (CS300)

class objects instances Fields Constructors Methods static

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Programming II (CS300)

Chapter 4. Defining Classes I

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Overview of Eclipse Lectures. Module Road Map

A base class (superclass or parent class) defines some generic behavior. A derived class (subclass or child class) can extend the base class.

ECE 122. Engineering Problem Solving with Java

This Week. Fields and Variables. W05 Example 1: Variables & Fields. More on Java classes. Constructors. Modifiers

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Distributed Systems Recitation 1. Tamim Jabban

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Lecture 18 Tao Wang 1

Array. Prepared By - Rifat Shahriyar

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226

Introduction to Programming Using Java (98-388)

Programming Language Concepts: Lecture 2

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Basic Object-Oriented Concepts. 5-Oct-17

Review of Object-Oriented Concepts in JAVA

Handout 8 Classes and Objects Continued: Static Variables and Constants.

Objects as a programming concept

Getter and Setter Methods

Chapter 4: Writing Classes

Data Structures (INE2011)

Recitation 3 Class and Objects

Anatomy of a Class Encapsulation Anatomy of a Method

Chapter 15: Object Oriented Programming

Lecture 07: Object Encapsulation & References AITI Nigeria Summer 2012 University of Lagos.

A JavaBean is a class file that stores Java code for a JSP

CS100J Prelim I, 29 Sept. 2003

CS260 Intro to Java & Android 03.Java Language Basics

Class, Variable, Constructor, Object, Method Questions

Arrays Classes & Methods, Inheritance

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

C08: Inheritance and Polymorphism

COMP 250 Winter 2011 Reading: Java background January 5, 2011

CS 302 Week 9. Jim Williams

Chapter 10 Introduction to Classes

Chapter 2a Class Relationships

Object-Oriented Programming (Java)

Practice problem on defining and using Class types. Part 4.

9 Working with the Java Class Library

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Classes, Objects, and OOP in Java. June 16, 2017

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3

Recursion 1. Recursion is the process of defining something in terms of itself.

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

Java Identifiers, Data Types & Variables

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

CS 231 Data Structures and Algorithms, Fall 2016

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Chapter 9 Enumerations

CS105 C++ Lecture 7. More on Classes, Inheritance

And Even More and More C++ Fundamentals of Computer Science

2. [20] Suppose we start declaring a Rectangle class as follows:

Comments are almost like C++

Java Fundamentals (II)

Software and Programming 1

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

CS 1302 Chapter 9 (Review) Object & Classes

IS-A is a way of saying: This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance.

COMP 401 Spring 2014 Midterm 1

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Java Program Coding Standards Programming for Information Technology

University of Palestine. Mid Exam Total Grade: 100

Working with Objects. Overview. This chapter covers. ! Overview! Properties and Fields! Initialization! Constructors! Assignment

Java Object Oriented Design. CSC207 Fall 2014

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

C++ Important Questions with Answers

Java Foundations Certified Junior Associate

Methods and Data (Savitch, Chapter 5)

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

CS 116 Week 8 Page 1

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

(a) Write the signature (visibility, name, parameters, types) of the method(s) required

W3101: Programming Languages C++ Ramana Isukapalli

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Announcements for the Class

Abstraction in Software Development

CS1004: Intro to CS in Java, Spring 2005

Java Server Page (JSP)

Transcription:

Notes on Chapter Three Methods 1. A Method is a named block of code that can be executed by using the method name. When the code in the method has completed it will return to the place it was called in the program. 2. Methods are called by inserting the method name followed by a pair of () symbols. This indicates to Java that the name references a method. 3. Methods are created outside of main() and any other methods but inside the class block, which is defined by { } symbols. Methods can be placed before or after the location from which they are called.

4. Method have two parts, a title and a body. The body is enclosed in { } symbols. Methods that appear with main() must have the leading identifier static because main() is a static method. 5. Method names must follow the same rules as variable naming. That is, case must match and method names cannot use reserved words, have spaces, special characters, etc. 6. By convention method names start with a lower case letter and capitalize other letters that makeup words. 7. Once the code inside a method completes the program transfers control back to the place in the code from where the method was called. 8. Access specifiers are words placed before the method return type that indicate how the method will be handled within the class. The terms that define visibility within a class include: public can be executed from a class object type private can only be executed from within the class protected can only be executed from within the class or by any inherited classes The term that defines class level access is static which means that the method must be executed at the class level and does not require objects.

You will learn more about these modifiers later. 9. Modifiers are placed before the method return type, which is placed just before the method name. In this case, the method returns an integer which is the data type of the item being returned. 10. If a method does not return a value then the return type is void. 11. Method automatically jump back to where they were called but in order to return something from a method the items being returned must appear after the statement return x where x is either a variable or a value. 12. When a method is called it must be followed by a pair of () symbols. To pass information into the method you would list the variables or values inside these () characters and separate the list with commas:

The method named gettotal has three arguments or parameters. When the method is called (line 13) two variables and a number are passed into the method. 13. When a method that uses parameters is called, the data in the parameters is copied into the parameters defined for the method. In the previous sample the contents of val1 is copied into v1, the contents of val2 is copied into v2, and the number 500 is copied into v3. The number of parameters used when calling the method must match the number of variables defined for the method. 14. When writing a method the data type of the method parameters must be defined. This identifies to Java what kind of data to expect in the variables defined for the method (line 18).

15. The parameters, once defined for the method, can be used inside the method as regular variables. However, the scope of the variables defined inside the method are tied to the method body. That is, you cannot use variable declared inside a method outside the method. You can also not use variables defined in the calling method (in this case main() ) inside the method.

16. Methods can be called from other methods. 17. Methods can be overloaded which means that different methods can have the same name but must have different parameter types:

18. Overloaded methods must differ in parameters. Differing in return types is not enough. Classes and Object 1. Classes are software constructions that are used to organize and design programs. They are also called Abstract Data Types (ADT) because they are used like regular data type to define what look like variables but are called Objects. 2. Classes can include both methods and properties. Methods are functions that either the Class or the Objects defined from the class use. Properties are data objects that are defined from either fundamental data types or from other classes. 3. Properties and Methods in classes have an access type. The access types will either be public, private, or protected. 4. Classes can be defined with the class name followed by the block characters { }. The class name must follow the variable naming conventions.

5. Classes must be defined before they can be used. They usually are defined outside other classes, but you can create inner classes that are defined inside other classes. 6. Most classes will have both Properties and Methods. By convention Properties are defined to be private. Methods used to access the properties are defined to be public. 7. Most methods will have a getter and setter method. The getter method returns the private property and the setter method sets the private property.

8. The property and methods inside a class must be set with the access modifier public, private or protected. 9. Private properties and methods cannot be accessed from objects but only by other methods inside the class. Public properties can be accessed from objects. 10. Using classes normally requires that an instance of the class be defined. This is done by using the class as a data type and the defined instance is an object.

11. Once objects are defined they each hold separate instances of the properties and method defined in the class. Only public properties and methods can be accessed by using the object name, a period, and the method or property. 12. Before using an object it must be created by using the command new ClassName(). Once defined the public methods and properties can be accessed from the object.

13. The new command will create the object instance in memory. 14. Regular methods defined inside a class are called non-static methods because they can only be used once you have created an object from the class using the new method.

15. A Constructor method is a special method which is used when an object is created using the new method. These methods don t need a return type and will be run when a new object is defined with the new command:

16. Constructor methods can perform multiple tasks. 17. Just like regular methods, Class methods can be overloaded which means that different parameters will define different methods.

18. Classes can have object from other classes: package samplemethod; class EmpPay { private double payrate; private int hours; } public void setpayrate(double p) { payrate=p; } public void sethours(int h) { hours = h; } public double getpayrate() { return payrate; } public int gethours() { return hours; } public double calcpay() { return payrate * hours; } class Employee { private String name; private EmpPay emp; public Employee(String n) { emp = new EmpPay(); name=n; emp.sethours(40); emp.setpayrate(10.00); } public void setname(string n) { name=n; } public void setemppay( EmpPay e) { emp = e; } public EmpPay getemppay() { return emp; } public void setpayrate(double pr) { emp.setpayrate(pr); } public void setpayrate(string pr) { emp.setpayrate( Double.parseDouble(pr)); } public void sethours(int h) { emp.sethours(h) } public void sethours(string h) { emp.sethours(integer.parseint(h); } public String getname() { return name; } public double getpayrate() { return emp.getpayrate() } public int gethours() { return emp.gethours() } public double calcpay() { return emp.calcpay(); }

} public class samplemethod { public static void main(string[] args) { Employee e1, e2; e1 = new Employee("Joe Jonees"); e2 = new Employee("Sara Smith"); EmpPay emp1, emp2; emp1 = e1.getemppay(); emp2 = e1.getemppay(); System.out.println("Starting main"); } }