Building Java ProgramsChapter outlineClasses, types, and objectsObjects and "OOP"AbstractionFactory/blueprint analogyRecall: Point objectsRecall: Point data/methodsA Point classObject state: fieldsPoint class, version 1FieldsAccessing fieldsClient codeClient code questionObject behavior: instance methodsClient code redundancyEliminating redundancy, v1Classes with behaviorInstance methodsPoint object diagramsThe implicit parameterTracing instance method callsPoint class, version 2Instance method questionsAccessors and mutatorsClient code, version 2Slide 28Object initialization: constructorsInitializing objectsConstructorsPoint class, version 3Tracing constructor callsClient code, version 3Slide 35EncapsulationSlide 37Implementing encapsulationEncapsulation and accessorsBenefits of encapsulationPoint class, version 4Preconditions, postconditions, and invariantsPre/postconditionsClass invariantsViolated preconditionsDealing with violationsThrowing exceptions exampleEncapsulation and invariantsSpecial instance methods: toString, equalsProblem: object printabilityThe toString methodtoString method syntaxRecall: comparing objectsThe equals methodInitial flawed equals methodequals and the Object classAnother flawed versionType-casting objectsCasting objects diagramComparing different typesThe instanceof keywordFinal version of equals methodThe keyword thisUsing the keyword thisVariable shadowingAvoiding shadowing with thisMultiple constructorsMultiple constructors w/ thisMore class problemsObject practice problemSlide 71Slide 72Calculator client codeCopyright 2006 by Pearson Education1Building Java Building Java ProgramsProgramsChapter 8: ClassesCopyright 2006 by Pearson Education2Chapter outlineobjects, classes, and object-oriented programmingrelationship between classes and objectsabstractionanatomy of a classfieldsinstance methodsconstructorsencapsulationadvanced classespreconditions, postconditions, and invariantsspecial methods: toString and equalsthe keyword thisCopyright 2006 by Pearson Education3Classes, types, and objectsclass:1. A module that can be run as a program.2. A template for a type of objects.We can write Java classes that are not programs in themselves, but instead define new types of objects.We can use these objects in our programs if we so desire.Why would we want to do this?Copyright 2006 by Pearson Education4Objects and "OOP"object: An encapsulation of data and behavior.object-oriented programming (OOP): Writing programs that perform most of their useful behavior through interactions with objects.So far, we have interacted with objects such as:StringPointScannerDrawingPanelGraphicsColorRandomFilePrintStreamCopyright 2006 by Pearson Education5Abstractionabstraction: A distancing between ideas and details.The objects in Java provide a level of abstraction, because we can use them without knowing how they work.You use abstraction every day when interacting with technological objects such as a portable music player.You understand its external behavior (buttons, screen, etc.)You DON'T understand its inner workings, nor do you need to.Copyright 2006 by Pearson Education6Factory/blueprint analogyIn real life, a factory can create many similar objects.This is also like following a blueprint.Music player factorystate: # of players madebehavior: directions on how to build a music playerMusic player #1state: station/song, volume, battery lifebehavior: power on/of change station/song change volume choose random songMusic player #2state: station/song, volume, battery lifebehavior: power on/of change station/song change volume choose random songMusic player #3state: station/song, volume, battery lifebehavior: power on/of change station/song change volume choose random songcreatesCopyright 2006 by Pearson Education7Recall: Point objectsJava has a class of objects named Point.To use Point, you must write: import java.awt.*;Constructing a Point object, general syntax:Point <name> = new Point(<x>, <y>);Point <name> = new Point(); // the origin, (0, 0)Examples:Point p1 = new Point(5, -2);Point p2 = new Point();Point objects are useful for several reasons:They store two values, an (x, y) pair, in a single variable.They have useful methods we can call in our programs.Copyright 2006 by Pearson Education8Recall: Point data/methodsData stored in each Point object:Useful methods of each Point object:Point objects can also be printed using println statements:Point p = new Point(5, -2);System.out.println(p); // java.awt.Point[x=5,y=-2]Method name Descriptiondistance(p) how far away the point is from point psetLocation(x, y) sets the point's x and y to the given valuestranslate(dx, dy) adjusts the point's x and y by the given amountsField name Descriptionxthe point's x-coordinateythe point's y-coordinateCopyright 2006 by Pearson Education9A Point classA Point class might look something like this:Each object contains its own data and methods.The class has the instructions for how to construct individual objects.Point classpublic Point()public Point(int x, int y)Point object #1state: int x, ybehavior: distance(Point p) equals(Point p) setLocation(int x, int y) toString() translate(int dx, int dy)Point object #2state: int x, ybehavior: distance(Point p) equals(Point p) setLocation(int x, int y) toString() translate(int dx, int dy)Point object #3state: int x, ybehavior: distance(Point p) equals(Point p) setLocation(int x, int y) toString() translate(int dx, int dy)allowsconstruction ofCopyright 2006 by Pearson Education10Object state:Object state:fieldsfieldsreading: 8.2Copyright 2006 by Pearson Education11Point class, version 1The following code creates a new class named Point.public class Point { int x; int y;}We'd save this code into a file named Point.java.Each object contains two pieces of data:an int named x,an int named y.Point objects (so far) do not contain any behavior.Copyright 2006 by Pearson Education12Fieldsfield: A variable inside an object that represents part of the internal state of the object.Each object will have its own copy of the data fields we declare.Declaring a field, general syntax:<type> <name> ;Examples:public class Student { String name; // each student object has a double gpa; // name and gpa data field}Copyright 2006 by Pearson
View Full Document