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 EndJan 14, 2019Using Objects2OverviewIn this presentation we will discuss:Classes and objectsMethods for objectsPrinting results3Classes and objectsA class is the type of an objectJust as a variable classSize may have type int, Color.red has type ColorJust as 5 is a literal of type int, "Hello" is a literal of type StringThere are exactly eight primitive typesThere are thousands of classes, and you can create more4DeclarationsYou declare variables to hold primitive values like this: int classSize; double area;You declare variables to hold objects like this: Color uglyBrown; String myName;5Assignment statementsAn assignment statement has the form: variable = expression ;Examples: classSize = 57; area = pi * radius * radius; uglyBrown = new Color(175, 175, 30); myName = "David Matuszek";6Combining declaration and assignmentDeclaration 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 placesThis rule is “true enough” for nowExceptions to this rule are complicated and left for later7MethodsPrimitives have operations, classes have methodsYou cannot define new primitives, but you can define new classesYou cannot define new operations, but you can define new methodsHere we will talk about using methods supplied by Java, not defining new ones8Data in classes and objectsA class is the type of an objectA class describes:How to make a new object of that classExample: new Color(175, 175, 30);What kind of data is in an objectExample: a Color object contains three numbers representing the amount of red, green, and blueThe 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();9Sending messages to objectsWe don’t perform operations on objects, we “talk” to themThis is called sending a message to the objectA 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 jobExamples: g.setColor(Color.pink); amountOfRed = Color.pink.getRed( );10Messages and methodsMessages can be used to:Tell an object some informationAsk an object for information (usually about itself)Tell an object to do somethingAny and all combinations of the aboveA method is something inside the object that responds to your messagesA message contains commands to do somethingJava contains thousands of classes, each typically containing dozens of methodsWhen you program you use these classes and methods, and also define your own classes and methods11Messages to a GraphicsIf 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);12Messages 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 color13StringA String is an object, but......because Strings are used so much, Java gives them some special syntaxThere are String literals: "This is a String"(Almost) no other objects have literalsThere is an operation, concatenation, on Strings:"Dave" + "Matuszek" gives "DaveMatuszek"In other respects, Strings are just objects14String methodsA String, like a Color, is immutableOnce you create it, there are no methods to change itBut 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 awayIf s is the name of the string "Hello", thens.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();15String concatenation+ usually means “add,” but if either operand (thing involved in the operation) is a String, then + means concatenationIf you concatenate anything with a String, that thing is first turned into a StringFor 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 methods16Data in classesA class describes objects. It describes:How to construct an object of that class,the kind of data in an object, andthe messages that the object can understandA class can also contain its own data, which is the same for any object of that classConstants are often provided this wayExamples:class Color contains the constant Color.REDclass Math contains the constant Math.PI17Printing out results, part 1In Java, “print” really means “display on the screen”Actually printing on paper is much harder!System is one of Java’s built-in classesSystem.out is a data object in the System class that knows how to “print” to your screenWe can “talk to” (send messages to) this mysterious object without knowing very much about it18Printing out results, part 2System.out is a object with useful methods that will let you print anything:print(x) turns x into a String and displays itprintln(x) (pronounced “print line”) turns x into a String and displays it, then goes to the next lineExamples: System.out.print("The sum of x and y is ");System.out.println(x + y);19New vocabularyclass: the type,
View Full Document