DOC PREVIEW
Penn CIT 591 - Using Objects

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Using ObjectsOverviewClasses and objectsDeclarationsAssignment statementsCombining declaration and assignmentMethodsData in classes and objectsSending messages to objectsMessages and methodsMessages to a GraphicsMessages to a ColorStringString methodsString concatenationData in classesPrinting out results, part 1Printing out results, part 2New vocabularyThe EndUsing ObjectsOverview•In this presentation we will discuss:–Classes and objects–Methods for objects–Printing resultsClasses 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 moreDeclarations•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;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";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 are complicated and left for laterMethods•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 onesData in classes and objects•A class is the type of an object•A class describes:–How to make a new object of that class•Example: new Color(175, 175, 30);–What kind of data is in an object•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 containsSending 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( );Messages and methods•Messages can be used to:–Tell an object some information–Tell an object to do something–Ask an object for information (usually about itself)–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 methodsMessages 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);Messages to a Color•Once you make a Color, you cannot change it; you can only ask it for information // Make a new purplish colorColor myColor = new Color(100, 0, 255); // Ask how much blue is in itint amountOfBlue = myColor.getBlue(); // Ask the color for a brighter version of itselfColor brightColor = myColor.brighter();•The last method doesn’t change the color; it makes a new colorString•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"•(Almost) no other objects have literals–There is an operation, concatenation, on Strings:•"Dave" + "Matuszek" gives "DaveMatuszek"•In other respects, Strings are just objectsString 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();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 methodsData 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.PIPrinting 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 itPrinting 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);New vocabulary•class: the type, or description, of an object•object: an instance, or member, of a


View Full Document

Penn CIT 591 - Using Objects

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Download Using Objects
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Using Objects and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Using Objects 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?