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 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
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.
Section 2.1: Using Objects
Kevin Curtis
Dennis Truong
Period 2
The Basics
Objects
• The basic software part in an object-orientated program.
• Represents output device or file (i.e. monitor)
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.
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.
Methods and Objects
main
Countdown System.out
println
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
2.6 Creating Objects
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
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()
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
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
Chapter 4.0 Writing Classes
By Dina Deng April Ochoa
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.
• [[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.
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.
Chapter 4.1 Review
Anatomy of a Class
Darwin Arayata
Stephen Le
Jibram Martinez
• 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
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
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.
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
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
Continued
• Some Java Modifiers are called visibility modifiers
• They control whether client code can “see’ what’s “inside” an object
Anatomy Of A Method
By: Jei Mercado, David Chap, and Kevin Lai
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
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
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
Constructor
• Cannot have any Return type even void
• Is the same name as the class• Each class have a different
structure
Local Data
• A variable declared in a method is local to that method and cannot be used outside of it
• Instruct data
Lesson 4.3
Method Overloading
Uses
• Performing similar operations on different types of data.
• Using the same method name with different parameter lists for several different methods.
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.
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.
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); }}
Output
Number or rolls: 500
Number of snake eyes: 6
Ratio: 0.012QuickTime™ and a
TIFF (Uncompressed) decompressorare needed to see this picture.
Chapter 4 - Section 4our
Method Decomposition
By Miguel, Marvin, and Eric
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.
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.
THE END
Or is it…?
Object Relationship
By
Dustin and Greg
Association• Use Relationship- when
two classes are aware of each other and may use each other.– Ex. An artist object draws
a picture 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
Aggregation
• An aggregate object is made up, in part, of other objects, forming a has-a relationship– Ex. A car has ahas a tire
Great Job!
• I will send a copy to each computer
• This is a good summary of some of the object oriented programming ideas