1 / 21

Reasoning with Objects

Reasoning with Objects. Object references and self-referential class declarations. Data Types.

nolcha
Download Presentation

Reasoning with Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Reasoning with Objects Object references and self-referential class declarations

  2. Data Types A data type may be described in terms of a set of values and a set of operations involving those values. e.g. For integer, the values are {0, 1, -1, 2, -2, 3, -3, …} and the operations are plus, minus, times, divide,… In Java we have two distinct kinds of data type: primitive types and object types. They differ in the way which they are defined, the form of operations, the form of data values.

  3. Two Kinds Of Data Type Primitive Types e.g. int, double, boolean. • Predefined part of the core language. • Considered fundamental concepts (building blocks). Object Types e.g. String, Book, Student, Circle,… • User (i.e. programmer) defined through class declaration. • Class declarations are the means by which new concepts are added to the vocabulary.

  4. Two Kinds of Operation Primitive Types • Operations on primitive data values are predefined. e.g. +, -, *, / for int. • Primitive types don’t have methods. Object Types • Operations on objects are user defined through method declaration. • A method declaration describes how to represent a certain operation on the type being defined. • A method call performs the desired operation on the target object.

  5. Two Kinds of Operation (II) Practical distinctions in the syntax (or form) of expressions: e.g. performing addition on integers, int z = x + 3; e.g. performing a substring operation on a String, String b = a.substring(1, 3); General form of method call: <object reference>.<method name>(<actual parameters>)

  6. Complications String is a class but the concept is partially defined in the core language. Not such a neat divide. e.g. String concatenation, String s = “fred”+” jim”;

  7. Two Kinds of Data Value Another name for object type is reference type because the values are references. e.g. String name = “Stephan”; int i = 3; Representation of storage in computer memory: :String ‘S’, ‘t’, ‘e’, ‘p’, ‘h’, ‘a’, ‘n’ name i 3

  8. Two Kinds of Storage (II) Final example, String name1 = “Stephan”; String name2 = name1; Representation of storage in computer memory: :String ‘S’, ‘t’, ‘e’, ‘p’, ‘h’, ‘a’, ‘n’ name1 name2

  9. Exercises: Understanding Object References Consider Classes A, B, C in appendices. Points • Declaring a variable to be of an object type does not automatically create an object. • The execution of a method may involve the creation and use of other objects. • References to objects may be passed as parameters, stored in variables, returned as method results.

  10. Exercise One Draw an object diagram that depicts the outcome of executing the following fragment: //… B vB = new B(); C vC = new C(); A vA = new A(); vA.setOne(vB); vA.setTwo(vC); //…

  11. Exercise Two Draw an object diagram that depicts the outcome of executing the following fragment: //… B vB = new B(); A vA = new A(); vA.setOne(vB); vB.setTwo(new C()); //…

  12. Exercise Three Draw an object diagram that depicts the outcome of executing the following fragment: //… C vC = new C(); A vA = new A(); B vB = new B(null, vC); vC.setTwo(vB); //…

  13. Exercise Four Draw an object diagram that depicts the outcome of executing the following fragment: //… A vA = new A(); B vB = new B(vA, vC); C vC = new C(new B()); vA.setTwo(vB.getTwo()); //…

  14. Challenge: Family Tree Let’s say we want to represent a family tree, we choose to design a Person class, but here’s the data we want to store: • Name • Date of birth • Date of death • Father • Mother How can we do this? Many possibilities….

  15. Exercises: Self-referential Classes Consider classes D and E in the appendices. Points • An object can contain within its fields references to other objects of the same type. • An operation on one object may involve using and manipulating other objects of the same type.

  16. Exercise One Draw an object diagram that depicts the outcome of executing the following fragment: //… D dOne = new D(); D dTwo = new D(); D dThree = new D(); D dFour = new D(); dTwo.setOther(dOne); dThree.setOther(dOne); dFour.setOther(dOne); //…

  17. Exercise Two Draw an object diagram that depicts the outcome of executing the following fragment: //… D dOne = new D(1); D dTwo = new D(2); dTwo.setOther(dOne); D dThree = new D(dTwo, 3); dOne.setOther(dThree, 5); //…

  18. Exercise Three Draw an object diagram that depicts the outcome of executing the following fragment: //… E eOne = new E(1); E eTwo = new E(5); E eThree = new E(3); E eFour = new E(7); eOne.setFields(null, eTwo, eThree, null); eTwo.setFields(eOne, null, eFour, null); eThree.setFields(null,eFour, null, eOne); eFour.setFields(eThree, null, null, eTwo); //…

  19. Exercise Four Draw an object diagram that depicts the outcome of executing the following fragment: //… D dOne = new D(1); D dTwo = new D(dTwo, 5); D dThree = new D(dTwo, 3); D dFour = new D(dThree, 7); dFour.printValue(); //…

  20. Challenge: CyberPet Consider the CyberPet class in the appendicies It’s possible for a pair of CyberPets to produce offspring. A CyberPet created in this way has energy and health levels inherited from its parents. It gains the health level of the less healthy parent and the energy level of the more energetic parent. Write a “mate” instance method that accepts a CyberPet reference as a parameter and returns a new CyberPet with the required characteristics.

  21. Summary There are two kinds of data type in java: primitive types and object types. The latter is complicated by the inclusion of the concept of reference. The data value that is stored in a variable defined to be an object type is in fact a reference to an object. Through reference it is possible to build object structures that reflect real world structures. It is possible and desirable for an object of a certain type to refer to other objects of the same type.

More Related