Defining Classes and Methods

Similar documents
Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

Defining Classes and Methods

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

5. Defining Classes and Methods

5. Defining Classes and Methods

Defining Classes and Methods

Defining Classes and Methods

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

EECS168 Exam 3 Review

Methods and Data (Savitch, Chapter 5)

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Computational Expression

Key Differences Between Python and Java

Methods. Methods. Mysteries Revealed

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Introduction to Programming Using Java (98-388)

Full file at

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Section 2.2 Your First Program in Java: Printing a Line of Text

Anatomy of a Class Encapsulation Anatomy of a Method

Section 2.2 Your First Program in Java: Printing a Line of Text

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

CS-201 Introduction to Programming with Java

Chapter 4 Defining Classes I

Software and Programming 1

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Objectives. Order (sort) the elements of an array Search an array for a particular item Define, use multidimensional array

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Objects and Classes Lecture 2

COMP 111. Introduction to Computer Science and Object-Oriented Programming. Week 2

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Programming with Java

Basic Computation. Chapter 2

( &% class MyClass { }

Objects and Classes -- Introduction

Full file at

ECE 122. Engineering Problem Solving with Java

Programming with Java

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

Java Primer 1: Types, Classes and Operators

Array Basics: Outline

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Review. these are the instance variables. these are parameters to the methods

CPS122 Lecture: Defining a Class

Chapter 6 Introduction to Defining Classes

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Fundamental Concepts and Definitions

Section 2: Introduction to Java. Historical note

Pace University. Fundamental Concepts of CS121 1

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

4. Java language basics: Function. Minhaeng Lee

Class object initialization block destructor Class object

Course Outline. Introduction to java

9 Working with the Java Class Library

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

Introduction to Computers and Java. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Introduction to Computers and Java

VARIABLES AND TYPES CITS1001

9/11/08 (c) 2008 Matthew J. Rutherford Class (c) 2008 Matthew J. Rutherford Class

Introduction to Computers and Java

CS1150 Principles of Computer Science Methods

CHAPTER 7 OBJECTS AND CLASSES

Arrays. Chapter 7. Walter Savitch Frank M. Carrano

Introduction to Computers and Java. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Supplementary Test 1

Software and Programming 1

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Lab5. Wooseok Kim

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

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

AP Computer Science Unit 1. Programs

PIC 20A The Basics of Java

Using Java Classes Fall 2018 Margaret Reid-Miller

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

More About Objects and Methods

More About Objects and Methods. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

St. Edmund Preparatory High School Brooklyn, NY

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

7. C++ Class and Object

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

4. If the following Java statements are executed, what will be displayed?

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

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

Basic Computation. Chapter 2

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

Recitation 02/02/07 Defining Classes and Methods. Chapter 4

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

1/29/2018. Starting a Program Executes its main Function. ECE 220: Computer Systems & Programming. The Function main Divides into Two Parts

Lecture Set 2: Starting Java

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e

Chapter 4: Writing Classes

A+ Computer Science -

CHAPTER 7 OBJECTS AND CLASSES

Transcription:

Walter Savitch Frank M. Carrano Defining Classes and Methods Chapter 5

Class and Method Definitions: Outline Class Files and Separate Compilation Instance Variables Methods The Keyword this Local Variables Blocks Parameters of a Primitive Type

Class and Method Definitions Java program consists of objects which interact with each other Objects of class types (String, Scanner) Objects have both data and methods Program objects can represent Objects in real world Abstractions

Class and Method Definitions A class definition is a template or blueprint for creating objects A class definition is like a cookie cutter A cookie cutter is not a cookie, but it can be used to create cookies Each cookie created by a particular cookie cutter will have the same attributes (thickness, decoration), but different values for those attributes (3mm, #1 Luke )

Class and Method Definitions An instance of a class is an object of that class type

Class and Method Definitions Figure 5.1 A class as a blueprint

Class and Method Definitions Figure 5.1 ctd. Objects that are instantiations of the class Automobile

Class and Method Definitions Figure 5.2 A class outline as a UML class diagram

Class Files and Separate Compilation Each Java class definition usually in a file by itself File begins with name of the class Ends with.java Class can be compiled separately Helpful to keep all class files used by a program in the same directory

Instance Variable Download SpeciesFirstTry and SpeciesFirstTryDemo Note SpeciesFirstTry has Three pieces of data (instance variables) Three behaviors (methods) Each instance of this type has its own copies of the data items Use of public No restrictions on how variables used

Methods When you use a method you "invoke" or "call" it Two kinds of Java methods Return a single item Perform some other action a void method The method main is a void method Invoked by the system Not by the application program

Methods Calling a method that returns a quantity Use anywhere a value can be used if (keyboard.nextint() > 0)... Calling a void method Write the invocation followed by a semicolon Resulting statement performs the action defined by the method System.out.println( hello );

Defining void Methods Consider method writeoutput from SpeciesFirstTry Method definitions appear inside class definition Can be used only with objects of that class

Defining void Methods Most method definitions we will see as public Method does not return a value Specified as a void method Heading includes parameters Body enclosed in braces { } Think of method as defining an action to be taken

Methods That Return a Value Consider method getpopulationin10( )... Heading declares type of value to be returned Last statement executed is return

Naming Methods Use a verb to name a void method writeoutput Use a noun to name a method that returns a value nextint All method names should start with a lowercase letter

The Keyword this Referring to instance variables outside the class must use Name of an object of the class Followed by a dot Name of instance variable Inside the class, Use name of variable alone The object (unnamed) is understood to be there

The Keyword this Inside the class the unnamed object can be referred to with the name this Example this.name = keyboard.nextline(); The keyword this stands for the receiving object can usually be omitted We will see some situations later that require the this

Local Variables Note beginning of class in listing 5.1 Variables declared inside the class are considered local variables May be used only inside this class Variable with same name inside a different class is considered a different variable All variables declared in method main are local to main

Local Variables Download BankAccount and LocalVariablesDemoProgram Note two different variables newamount Note different values output Sample screen output

Blocks Recall compound statements Enclosed in braces { } When you declare a variable within a compound statement The compound statement is called a block The scope of the variable is from its declaration to the end of the block Variable declared outside the block usable both outside and inside the block

Parameters of Primitive Type Recall method declaration in SpeciesFirstTry Note it only works for 10 years We can make it more versatile by giving the method a parameter to specify how many years Download SpeciesSecondTry and SpeciesSecondTryDemo

Parameters of Primitive Type Note the declaration public int predictpopulation(int years) The formal parameter is years Calling the method int futurepopulation = speciesofthemonth.predictpopulation(10); The actual parameter is the integer 10

Parameters of Primitive Type Parameter names are local to the method When method invoked Each parameter initialized to value in corresponding actual parameter Primitive actual parameter cannot be altered by invocation of the method Automatic type conversion performed byte -> short -> int -> long -> float -> double