Using objects

Download Report

Transcript Using objects

Using Objects
26-Jul-16
Overview

In this presentation we will discuss:



Classes and objects
Methods for objects
Printing results
2
Classes and objects





A class is the type of an object
Just as a variable classSize may have type int,
Color.red has type Color
Just as 5 is a literal of type int, "Hello" is a literal of
type String
There are exactly eight primitive types
There are thousands of classes, and you can create more
3
Declarations

You declare variables to hold primitive values like this:
int classSize;
double area;

You declare variables to hold objects like this:
Color uglyBrown;
String myName;
4
Assignment statements

An assignment statement has the form:
variable = expression ;

Examples:
classSize = 57;
area = pi * radius * radius;
uglyBrown = new Color(175, 175, 30);
myName = "David Matuszek";
5
Combining declaration and assignment

Declaration and assignment can be combined into a
single statement:
int classSize = 57;
String myName = "David Matuszek";
Color uglyBrown = new Color(175, 175, 30);

You can only declare a variable once, but you can
assign to it many times in many places


This rule is “true enough” for now
Exceptions to this rule are complicated and left for later
6
Methods




Primitives have operations, classes have methods
You cannot define new primitives, but you can define
new classes
You cannot define new operations, but you can
define new methods
Here we will talk about using methods supplied by
Java, not defining new ones
7
Data in classes and objects


A class is the type of an object
A class describes:

How to make a new object of that class


What kind of data is in an object


Example: new Color(175, 175, 30);
Example: a Color object contains three numbers representing the
amount of red, green, and blue
The methods of an object (the actions it can perform)


Example: a Color object can tell you how much red it contains:
int amount = myColor.getRed();
8
Sending messages to objects

We don’t perform operations on objects, we “talk” to them


This is called sending a message to the object
A message looks like this:
object.method(extra information)
•
•
•

The object is the thing we are talking to
The method is a name of the action we want the object to take
The extra information is anything required by the method in order
to do its job
Examples:
g.setColor(Color.pink);
amountOfRed = Color.pink.getRed( );
9
Messages and methods

Messages can be used to:





Tell an object some information
Ask an object for information (usually about itself)
Tell an object to do something
Any and all combinations of the above
A method is something inside the object that
responds to your messages



A message contains commands to do something
Java contains thousands of classes, each typically
containing dozens of methods
When you program you use these classes and methods,
and also define your own classes and methods
10
Messages to a Graphics

If you have a Graphics, and its name is g, here are some
things you can do with g:



Tell it to use a particular color:
g.setColor(Color.orange);
Ask it what color it is using:
Color currentColor = g.getColor();
Tell it to draw a line:
g.drawLine(14, 23, 87, 5);
11
Messages to a Color
•
Once you make a Color, you cannot change it; you
can only ask it for information
// Make a new purplish color
Color myColor = new Color(100, 0, 255);
// Ask how much blue is in it
int amountOfBlue = myColor.getBlue();
// Ask the color for a brighter version of itself
Color brightColor = myColor.brighter();
•
The last method doesn’t change the color; it makes a new
color
12
String


A String is an object, but...
...because Strings are used so much, Java gives
them some special syntax

There are String literals: "This is a String"


There is an operation, concatenation, on Strings:


(Almost) no other objects have literals
"Dave" + "Matuszek" gives "DaveMatuszek"
In other respects, Strings are just objects
13
String methods

A String, like a Color, is immutable


Once you create it, there are no methods to change it
But you can easily make new Strings:
myName = "Dave";
myName = "Dr. " + myName;


This is kind of a subtle point; it will be important
later, but you don’t need to understand it right away
If s is the name of the string "Hello", then


s.length() tells you the number of characters in
String s (returns 5)
s.toUpperCase() returns the new String "HELLO"
(s itself is not changed)

But you can say s = s.toUpperCase();
14
String concatenation



+ usually means “add,” but if either operand (thing
involved in the operation) is a String, then + means
concatenation
If you concatenate anything with a String, that thing is
first turned into a String
For example, you can concatenate a String and a
number:
System.out.println("The price is $" + price);
 + as the concatenation operator is an exception to the
rule: Primitives have operations, Objects have methods
15
Data in classes

A class describes objects. It describes:




How to construct an object of that class,
the kind of data in an object, and
the messages that the object can understand
A class can also contain its own data, which is the same
for any object of that class


Constants are often provided this way
Examples:


class Color contains the constant Color.RED
class Math contains the constant Math.PI
16
Printing out results, part 1

In Java, “print” really means “display on the
screen”




Actually printing on paper is much harder!
System is one of Java’s built-in classes
System.out is a data object in the System class
that knows how to “print” to your screen
We can “talk to” (send messages to) this
mysterious object without knowing very much
about it
17
Printing out results, part 2

System.out is a object with useful methods that will
let you print anything:



print(x) turns x into a String and displays it
println(x) (pronounced “print line”) turns x into a String
and displays it, then goes to the next line
Examples:
System.out.print("The sum of x and y is ");
System.out.println(x + y);
18
New vocabulary





class: the type, or description, of an object
object: an instance, or member, of a class
message: something that you “say” to a class, either
telling it something or asking it for information
immutable: cannot be changed after it is created
operand: one of the inputs to an operation
19
The End
“If you give someone a program, you will
frustrate them for a day; if you teach them how to
program, you will frustrate them for a lifetime.”
--Anon.
20