Java – Software Solutions….Objects and Primitive DataIntroductory Notions Introduction to ObjectsIntroduction to ObjectsObjects and ClassesInheritanceUsing ObjectsThe print MethodAbstractionAbstraction (continued)Character StringsString ConcatenationSlide 14Overloading OperatorsEscape SequencesEscape sequences (continued)Slide 18Public Directory for Roses.javaVariablesSlide 21AssignmentGeometry example…ConstantsPrimitive DataNumeric Primitive DataSlide 27CharactersSlide 29Boolean2.5 Arithmetic Expressions (p. 80)Division and RemainderOperator PrecedenceSlide 34Assignment RevisitedSlide 36Data ConversionsSlide 38Slide 392.6 Creating Objects (p. 87)Creating Objects - ConstructorsCreating ObjectsString MethodsSlide 442.7 Class Libraries (p.91)PackagesThe import DeclarationSlide 48Random Class from java.util package2.8 Class Methods (p. 98)The Keyboard ClassKeyboard InputReading Keyboard InputBufferedReader Class and its methods p. 287-288Wrapper ClassesSlide 56Continuing…2.9 Formatting Output (p. 103)More…Stated a little differently…Slide 60More on Formatting OutputSlide 62SummaryJava – Software Solutions…. Chapter 2: Objects and Primitive Data2Objects and Primitive DataNow we can explore some more fundamental programming conceptsChapter 2 focuses on:predefined objectsprimitive datathe declaration and use of variablesexpressions and operator precedencecreating and using objectsclass libraries3Introductory NotionsNeed ability to create and use objects.Provide ‘services’Objects are fundamental to writing programs in an OOP language.Objects ‘do’ all the things we want:Manipulate stringsMake computationsDo input/outputDraw shapes…4 Introduction to ObjectsAn object represents something with which we can interact in a programAn object provides a collection of services that we can tell it to perform for usThe services are defined by methods in a class that defines the objectA class represents a concept, and an object represents the embodiment of a classA class can be used to create multiple objects5Introduction to ObjectsObjects also manage dataMaybe primitive; maybe complex (like integers, floats…)Most complex data consists if primitive data…A data type defines a set of values and operations that can be performed on those values.Think: integers; +, -, *, /, no division by zero… Objects is defined by a class – an abstraction; a generalization. Objects are ‘instances’ of a class.Operations (methods) are defined by methods in the class.Methods – a collection of programming statements with a given name that perform some kind of operation6Objects and ClassesBank AccountA class(the concept)Class = BankAccountJohn’s Bank AccountBalance: $5,257An object(a realization)(an ‘instantiation’)Bill’s Bank AccountBalance: $1,245,069Mary’s Bank AccountBalance: $16,833Multiple objectsfrom the same class. Has attributes (data). Has methods (operations)Classes ‘encapsulate’ attributes and methods.7InheritanceOne class can be used to derive another via inheritanceClasses can be organized into inheritance hierarchiesBank AccountAccountCharge AccountSavings AccountChecking AccountThink: Gender Men Women Mothers non-Mothers8Using ObjectsThe System.out object represents a destination to which we can send outputIn the Lincoln program, we invoked the println method of the System.out object:System.out.println ("Whatever you are, be a good one.");objectmethodinformation provided to the method(parameters)Notice the ‘notation’ for referencing the method: object.methodWe are SENDING A MESSAGE to the object, System.out.We are requesting that the object perform a ‘service’ for us by invoking.the services of its method, println.9The print MethodThe System.out object provides another service as wellThe print method is similar to the println method, except that it does not advance to the next lineTherefore anything printed after a print statement will appear on the same lineSee Countdown.java (page 65)Sending a message (fig. 2.2)mainCountdown System.outprintln//println//lprint// others…10AbstractionAn abstraction hides (or suppresses) the right details at the right time. We ‘abstract out’ common characteristics. An object is abstract in that we don't have to think about its internal details in order to use it. encapsulate attributes and methods and provide services (have ‘responsibilities’) to other objects through the sending of messages. For example, we don't have to know how the println method inside the object System.out actually works in order to invoke it. We know when we send a message to the object System.out (we send messages by invoking its ‘methods’) with the parameters (“ “) the object will print out the contents enclosed in quotes.11Abstraction (continued)Of course, we have ‘levels’ of abstraction – germane to the problem at hand.CarFordMustangRed Mustang that belongs to …..Are all levels of abstraction! Each is more and more specific, but all have the ‘is-a’ characteristics. More later…..Classes and their objects help us write complex software12Character StringsEvery character string is an object in Java, defined by the String classEvery string literal, delimited by double quotation marks, represents a String objectTwo fundamental string operations:1) The string concatenation operator (+) is used to append one string to the end of anotherIt can also be used to append a number to a stringA string literal cannot be broken across two lines in a program13String ConcatenationThe plus operator (+) is also used for arithmetic additionThe function that the + operator performs depends on the type of the information on which it operatesIf both operands are strings, or if one is a string and one is a number, it performs string concatenationIf both operands are numeric, it adds themThe + operator is evaluated left to right (associativity)Parentheses can be used to force the operation orderSee Addition.java (page 70); Let’s look at some code:14//********************************************************************// Facts.java Author: Lewis/Loftus//// Demonstrates the use of the string concatenation operator and the// automatic conversion of an integer to a
View Full Document