Defining Classes and Methods

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 4 Defining Classes I Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Road Map Introduction to object oriented programming. Classes
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 4 Defining Classes I Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Classes, Objects, and Methods
Writing Classes (Chapter 4)
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Class and Method Definitions. UML Class Diagram Automobile - fuel: double - speed: double - license: String + increaseSpeed(double howHardPress): void.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Chapter 4 Defining Classes I Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 3 Implementing Classes
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Defining Classes and Methods
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Java Programming: Guided Learning with Early Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Chapter 5 Classes.
Chapter 4: Writing Classes
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Classes, Objects, and Methods
Inheritance, Polymorphism, and Interfaces. Oh My
Object-Oriented Programming
Chapter 4 Writing Classes.
Classes and Objects.
Object-Oriented Programming
Defining Classes and Methods
More About Objects and Methods
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Object-Oriented Programming
Chapter 4 Defining Classes I
Object Oriented Programming in java
Defining Classes and Methods
Defining Classes and Methods
Classes, Objects and Methods
OO Programming Concepts
Presentation transcript:

Defining Classes and Methods Chapter 5 Modified by JJ

Objectives Describe concepts of class, class object Create class objects Define a Java class, its methods Describe use of parameters in a method Use modifiers public, private Define accessor, mutator class methods Describe information hiding, encapsulation Write method pre- and postconditions

Objectives Describe purpose of javadoc Draw simple UML diagrams Describe references, variables, parameters of a class type Define boolean-valued methods such as equals

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

Let’s Take a Step Back

int numberOfCats; THE BIG IDEA To declare variables you provide its TYPE and NAME int numberOfCats; Type Name

Classes

Class and Method Definitions Java program consists of objects Objects of class types Objects that interact with one another Program objects can represent Objects in real world Abstractions ***CLASSES ARE PROGRAMMER CREATED TYPES***

Class and Method Definitions A class as a blueprint Attributes and Methods

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

Methods! NumberGuessingGame – system randomly selects a number and the user must keep guessing it until they got it

Creating Methods! NumberGuessingGame – system randomly selects a number and the user must keep guessing it until they got it

Methods Go inside the class 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 Calling a void method Use anywhere a value can be used Calling a void method Write the invocation followed by a semicolon Resulting statement performs the action defined by the method

Method General Form General form of a method Return Type Name Parameters Scope Body

Defining void Methods Consider method writeOutput from Listing 5.1 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 getAgeInHumanYears( ) Heading declares type of value to be returned Last statement executed is return

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 We will seem some situations later that require the this

Local Variables Variables declared inside a method are called local variables May be used only inside the method All variables declared in method main are local to main Local variables having the same name and declared in different methods are different variables

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 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

Using Methods!

Using Methods To access a method in an object, one must use the dot (“.”) operator The method must have the scope public to be accessed outside of the class Before a method can be used the object must be instantiated Otherwise you get a NullReferenceException run-time error You have used this before keyboard.nextInt() Assumed to be type Scanner str.charAt(0) Assummed to be type String

A Way to Visualize Using Methods

A Way to Visualize Using Methods

A Way to Visualize Using Methods

A Way to Visualize Using Methods MAGIC

A Way to Visualize Using Methods MAGIC

A Way to Visualize Using Methods MAGIC

A Way to Visualize Using Methods MAGIC

A Way to Visualize Using Methods MAGIC

Making Classes!

In 7 Easy Steps!

The Steps! Define the Class Create the Instance Variables (attributes) Make their scope private Create the Constructors Default Parameterized for each attribute Create the Accessors for every attribute Create the Mutators for every attribute Create the other Methods Equals() and toString() are both good ones to have Profit!

Example! Cat Example

Let’s Define a Cat Attributes Methods Name Weight Number of Legs Eat Sleep Destroy Your Life