Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the...

46
Introducing Objects and stuff GABY and MATT

Transcript of Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the...

Page 1: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Introducing Objects and stuff

GABY and MATT

Page 2: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Definition:• Object: “a class that is the root of the hierarchy

tree for all classes in JAVA.”– An object is defined by a class or

• Primitive Data: Includes common values such as numbers and characters.

• Data Type: Defines a set of values and operations.• Operators: A symbol that represents an operation

in a programming language, such as the addition operator

Page 3: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Continued…

• Encapsulation: the characteristic of an object that limits access to its variables and methods. All interaction with the object occurs through an interface.

• Inheritance: The ability to create a new class from an existing one. Inherited variables and methods of the original class are available in the new class if they were declared locally.

Page 4: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Section 2.1: Using Objects

Kevin Curtis

Dennis Truong

Period 2

Page 5: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

The Basics

Objects

• The basic software part in an object-orientated program.

• Represents output device or file (i.e. monitor)

Page 6: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

The Basics

For example

System.out.println (“Whatever you are, be a good one.”);

System.out represents the output device or file

The object’s name is out and stored in the system

class

The println method will print a string of characters to

the screen.

The parameter is the piece of data sent to a method, or in this example, the string of characters printed to the screen.

Page 7: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Methods and Objects• Receives information from the parameter:

System.out.println (“test”);

• Determines how the object functions for example:

• print method: prints information and stays on the same line.

• println method: prints information and then skips to the next line.

Page 8: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Methods and Objects

main

Countdown System.out

println

Page 9: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Abstraction

• An abstraction means that the details of how it works does not matter to the user. An object is an example of an abstraction.

• An abstraction hides or ignores certain details. With a good abstraction hiding the right details at the right time to manage the complexity.

• Level of abstraction: the amount of abstraction that is used to hide the details

Page 10: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

2.6 Creating Objects

Page 11: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Introduction

• Variable can hold primitive value or reference to object • The new operator returns a reference to a newly created

object • Creating object with new operator is called instantiation ex. String name = new String (“Mr. Jacobson”); • An object is called instance of particular class • After the new operator creates the object, a constructor is a

string literal

Page 12: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Dot Operator

• When object is instantiated, we use the dot operator to get is methods

• Dot operator is added right after object reference and is followed by method being invoked

ex. Count = name.length()

Page 13: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

The String Class

• String in Java are object represented by the string class • the type shown in front of method name is called return

type of the method • A return type void means that the method doesn’t return a

value • Object immutable when value can’t lengthened or short

nor can any of its character change • Index is a character’s position in string • The first character is zero

Page 14: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Wrapper Class

• All primitive type in Java have wrapper class-- let you create objects representing primitive data

• Integer class represent int and double class represent a double

ex. Integer number = new Integer (45); • The Integer and Double objects are immutable

Page 15: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Chapter 4.0 Writing Classes

By Dina Deng April Ochoa

Page 16: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Objects Revisited• Example of an Object:

A ball has: diameter, color and elasticity.• The properties that describe an object are

called attributes - defined as the object’s state of being.

• Behaviors: What it does-ex. the ball can be thrown, bounced, or rolled.

Page 17: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

• [[Each object has a state and a set of behaviors. The values of an object’s variables define its state and the methods define its behaviors.]]

• The action of an object will remain the same, but it depends on the object’s state.

• The class of an object may contain a method to add a new course.

• Software objects can often represent physical things, but they don’t have to. For example: an error message can be an object, with its state being the text of the message and behaviors, including printing the error message. There’s no limit for possibilities to just be physical things.

Page 18: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Classes• An object is defined by a class• A class can be described as a model, patter, or

blueprint of the object that’s being created. It has no memory space for data. Each object has its own data space, thus its own state.

• Blueprint - defines the important characteristics. Ex. A house’s : walls, windows, doors, electrical outlets, and so on.

Page 19: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Chapter 4.1 Review

Anatomy of a Class

Darwin Arayata

Stephen Le

Jibram Martinez

Page 20: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

• Members of the class are classes containing the declarations if the data that will be stored in each instantiated object and the declarations of the methods that can be invoked using an object.

• Refer to the picture on pg. 193 listing 4.1

Page 21: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Instance Data

• In the coin class, the constants HEADS and TAILS, and the variable face are declared inside the class, but not inside any methods.

• Location at which variable declared defines its scope• The coin object, for example, has its own face variable

with its own data space. -Reference to Page 197 listing 4.3

Page 22: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Instance Data continued..

• Attributes such as the variable face are also called instance data because memory space is created for each instance of the class that is created.

• Java automatically initializes any variable declared at the class level.

Page 23: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Encapsulations and Visibility Modifiers

• We can think about an object in one of two ways, depending on what we are trying to do.

• First when we are designing and implementing objects• We have to define the variables that will be held in the

object and write the methods that make the object useful• Objects should be encapsulations lated. The rest of a

program should work with an object only through a well defined interface

Page 24: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Encapsulations and Visibility modifiers continued….

• When we are designing a solution we have to think about the objects in the program work with each other

• In java we create object encapsulations using Modifiers

• A modifier is a java reserved word that names special characteristics of a programming language

Page 25: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Continued

• Some Java Modifiers are called visibility modifiers

• They control whether client code can “see’ what’s “inside” an object

Page 26: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Anatomy Of A Method

By: Jei Mercado, David Chap, and Kevin Lai

Page 27: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Anatomy Of a Method

• A method declaration defines the code that is executed when the method is invoked.

• When it is done, control return to the location where the call was made and execution continue.

• Define the same class• Define Programs

Page 28: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Return Statement

• A return value must match the return type in the method header.

• Return type in the method header can be a primitive type class name Reserved word void

• Void is used as a return type• Return is double

Page 29: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Parameter

• Pass through when invoked• List in the header of this method

list the type of value that are passed their name

• Formal parameter• Actual value pass into actual

parameter

Page 30: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Constructor

• Cannot have any Return type even void

• Is the same name as the class• Each class have a different

structure

Page 31: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Local Data

• A variable declared in a method is local to that method and cannot be used outside of it

• Instruct data

Page 32: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Lesson 4.3

Method Overloading

Page 33: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Uses

• Performing similar operations on different types of data.

• Using the same method name with different parameter lists for several different methods.

Page 34: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

What is needed

• The compiler still needs to match up each invocation with a specific method declaration.

• It needs a “signature”, which is the methods name, number, type, and order of its parameters.

Page 35: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

What happens

• First, the value in the variable is converted to a string representation.

• Next, the two strings are concatenated into one longer string.

• Last, the definition of println that accepts a single string is called.

Page 36: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Public class SnakeEyes{ public static void main (String[] args) { final int ROLLS = 500; int snakeEyes = 0, num1, num2;

Die die1 = new Die(); Die die2 = new Die(20);

for (int roll = 1; roll <= ROLLS; roll++) { num1 = die1.roll(); num2 = die2.roll(); if (num1 == 1 && num2 == 1) snakeEyes++; } System.out.println (“Number of rolls: “ + ROLLS); System.out.println (“Number of snake eyes: “ + snakeEyes); System.out.println (“Ratio: “ + (double)snakeEyes/ROLLS); }}

Page 37: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Output

Number or rolls: 500

Number of snake eyes: 6

Ratio: 0.012QuickTime™ and a

TIFF (Uncompressed) decompressorare needed to see this picture.

Page 38: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Chapter 4 - Section 4our

Method Decomposition

By Miguel, Marvin, and Eric

Page 39: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

What is method decomposition????

-a complicated method can be broken into several simpler methods and helped by support methods.

-in an object-oriented design, breaking up methods must happen after objects have been broken.

Page 40: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

For example…

-beginsWithVowel, beginsWithBlend, startsWith are simpler methods that help the translate method do its job.

-PigLatinTranslator.java (pg. 216-218)

-the translate method is broken up into simpler methods and uses several support methods.

-translateWord is a support method.

Page 41: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

THE END

Or is it…?

Page 42: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Object Relationship

By

Dustin and Greg

Page 43: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Association• Use Relationship- when

two classes are aware of each other and may use each other.– Ex. An artist object draws

a picture object

Page 44: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Association of objectsof the same class

• A method invoked through one object may take as a parameter another object of the same class

Woman object taking parameters from Man

object

Page 45: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Aggregation

• An aggregate object is made up, in part, of other objects, forming a has-a relationship– Ex. A car has ahas a tire

Page 46: Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.

Great Job!

• I will send a copy to each computer

• This is a good summary of some of the object oriented programming ideas