L2-Fundamentalconcepts(In Java)

L2-Fundamentalconcepts(In Java)

CSC7322: Object Oriented Development J Paul Gibson, A207 [email protected] http://www-public.telecom-sudparis.eu/~gibson/Teaching/CSC7322/ Fundamental Concepts (In Java) …/~gibson/Teaching/CSC7322/L2-FundamentalConcepts.pdf 2013: J Paul Gibson TSP: Object Oriented Development CSC7322/ FundamentalConcepts .1 Moving from procedures to objects/classes Questions: •What is good about the procedural approach? •What is bad about the procedural approach? •What could we force the user to do to make the procedural approach better? •Could we build these constraints into the language? •What can we help the user to do to avoid repetition? •Could we build these features as semantic extensions to the language? •For example, is this why C moved to C++? Think about these as we learn more about OO (Java) 2013: J Paul Gibson TSP: OOD CSC7322/ FundamentalConcepts .2 From Structures to Objects In C and other procedural programming languages: •programming is action oriented •unit of programming is the function (or procedure) •data exists to support the actions that the functions perform •dynamically, functions are called In Java and C++ (and other OO languages): •programming is object oriented •unit of programming is the class (a user-defined type) •data and functions/methods are contained within a class •dynamically, objects are created (and used through their methods) 2013: J Paul Gibson TSP: OOD CSC7322/ FundamentalConcepts .3 OO is a structuring mechanism OO is more than just a means of structuring data and functions: But it does help in structuring our models 2013: J Paul Gibson TSP: Object Oriented Development CSC7322/ FundamentalConcepts .4 Before OO: the function-data problem Functional dependencies provided a structural view Partitioning the dependency graph (coloured above) provides a means of grouping functions into modules/packages However, the data structure is not so evident (it is moving around the functions and it is stored in modules. 2013: J Paul Gibson TSP: Object Oriented Development CSC7322/ FundamentalConcepts .5 Before OO: the function-data problem A typical data structure diagram does not help in structuring understanding of the functionality of the system 2013: J Paul Gibson TSP: Object Oriented CSC7322/ FundamentalConcepts .6 Development Before OO: the function-data problem The real problem is integrating dynamic and static views/models/properties/requirements in a coherent fashion (and facilitating different levels of abstraction) A good example is a DFD But, this is a compromise which is focussed on what a system does rather than how it does it. Question: how well does it integrate the dynamic and static views? 2013: J Paul Gibson TSP: Object Oriented CSC7322/ FundamentalConcepts .7 Development Before OO: the function-data problem The real problem is integrating dynamic and static views/models/properties/requirements in a coherent fashion (and facilitating different levels of abstraction) Another good example is entity relationship diagrams Question: would you say these focus on dynamic behaviour or static structure? 2013: J Paul Gibson TSP: Object Oriented Development CSC7322/ FundamentalConcepts .8 After OO: the function-data problem Data Data Data Functions Functions Functions Data Data Functions Functions But, there is a new kind of problem: we have lots of new different types of relations between classes (objects) 2013: J Paul Gibson TSP: Object Oriented Development CSC7322/ FundamentalConcepts .9 Constructing Software Systems Given a system which is structured in terms of functions and data, one must ask how to construct the system. Further, one must ask whether the decomposition mechanism supports re-use. Also , does it help in system evolution /maintenance Finally, one should ask about support for independent development/compilation QUESTION: Do you know what the major issues were before OO and whether OO has helped/hindered/changed the situation? 2013: J Paul Gibson TSP: Object Oriented Development CSC7322/ FundamentalConcepts .10 Constructors : make an object of a class ... make a system Constructors are fundamental to all OO programming: •A special method/function used to initialise data members in the object being created •Called automatically when an object of the class is created •Common to have several constructors for a class (overloading) •Things get complicated when: •member objects of a class are themselves objects! •we inherit behaviour … •we have recursive data structures in classes In some OO languages, there are also destructors . Question: why are these not so fundamental in a language like Java? 2013: J Paul Gibson TSP: OOD CSC7322/ FundamentalConcepts .11 Constructors: make an object of a class ... make a system public class Dice extends DiceAbstraction implements DiceSpecification{ public final int NUMBEROFSIDES ; protected Random rng = new Random(); protected static int numberOfDie = 0; protected int numberOfRolls = 0; protected int lastRoll = 0; public boolean invariant(){ return super .invariant() && rng != null ; } Note: the name of the class, public, final protected, int, protected, boolean, null, 2013: J Paul Gibson TSP: OOD CSC7322/ FundamentalConcepts .12 Constructors: make an object of a class ... make a system /** * Dice constructor by default will make a dice with <code> DEFAULT_numberOfSides * </code> sides. <br> * Increments the <code> numberOfDie </code> counter <br><br> * * Tested by {@link JUnit_DiceAbstractionTest#testDefaultConstructor()} , which * guarantees that the Dice is * constructed in a safe state as specified by {@link Dice#invariant}. */ public Dice () throws InvariantBroken { NUMBEROFSIDES = DEFAULT_numberOfSides ; numberOfDie ++; numberOfRolls =0; // if (!invariant()) throw (new InvariantBroken("Dice was not constructed in a // safe state")); } Note: the name of the constructor, the comments, the use of a default, the (user defined) exception, the initialisation of variables of primitive type, the constants 2013: J Paul Gibson TSP: OOD CSC7322/ FundamentalConcepts .13 Constructors: make an object of a class ... make a system /** * Dice constructor with the specified number of sides <br> * Increments the <code> numberOfDie </code><br> * If the specified number <code> sides </code> is bigger than * <code> MAXIMUM_numberOfSides </code> * or less than <code> MINIMUM_numberOfSides </code> then the default of <code> * DEFAULT_numberOfSides </code> is used. <br><br> * * Tested by {@link JUnit_DiceAbstractionTest#testNonDefaultConstructor()} , * which guarantees that the Dice is constructed in a safe state as specified by *{@link Dice#invariant}. * * @param sides defines the number of sides for the new dice being constructed. */ public Dice ( int sides) throws InvariantBroken{ if (sides<MINIMUM_numberOfSides || sides > MAXIMIM_numberOfSides ) sides = DEFAULT_numberOfSides ; NUMBEROFSIDES =sides; numberOfDie ++; numberOfRolls =0; // if (!invariant()) throw (new InvariantBroken("Dice was not constructed in a // safe state")); } NOTE: the overloading 2013: J Paul Gibson TSP: OOD CSC7322/ FundamentalConcepts .14 Constructors: make an object of a class ... make a system /** * Dice copy constructor - which copies the <code> numberOfSides </code> and the <code> * lastRoll </code> values, * it does not copy the <code> numberOfRolls </code> value as this is intialised to 1 in the * new Dice. <br> * Increments the <code> numberOfDie </code> .<br><br> * * Tested by {@link JUnit_DiceAbstractionTest#testCopyConstructor()} , which guarantees that * the Dice is constructed in a safe state as specified by {@link Dice#invariant}. * * @param diceToCopy provides the values to be copied in the new Dice. * @throws IllegalArgumentException if argument <code> diceToCopy </code> is <code> null </code> */ public Dice (Dice diceToCopy ) throws IllegalArgumentException , InvariantBroken { if (diceToCopy == null ) throw (new IllegalArgumentException("Can't copy a null dice" )); NUMBEROFSIDES = diceToCopy. numberOfSides ; if (diceToCopy.numberOfRolls()==0) lastRoll = 0; else lastRoll = diceToCopy.lastRoll(); numberOfDie ++; numberOfRolls =1; // if (!invariant()) throw (new InvariantBroken("Dice was not constructed in a safe state")); } NOTE: the new exception (from the libraries) 2013: J Paul Gibson TSP: OOD CSC7322/ FundamentalConcepts .15 Constructors: make an object of a subclass public class DiceWithStatistics extends Dice implements DiceSpecification /** * Create a default dice and reset the roll frequencies for each side to be 0 */ public DiceWithStatistics (){ super (); rollsFrequency = new int [numberOfSides ]; resetFrequencies(); numberOfDieWithStatistics ++; } /** * Create a dice and reset the roll frequencies for each side to be 0 * @param sides can specify the number of sides (within range of 3..36) */ public DiceWithStatistics (int sides){ super (sides); rollsFrequency = new int [numberOfSides ]; resetFrequencies(); if (lastRoll !=0) rollsFrequency [lastRoll()-1] = 1; numberOfDieWithStatistics ++; } NOTE: super, extends, implements, the new attributes, an array 2013: J Paul Gibson TSP: OOD CSC7322/ FundamentalConcepts .16 Constructors: make an object of a subclass /** * Create a new DiceWithStatistics as a copy of a Dice <br><br> * Reset all frequencies to 0 except frequency of last roll which * should be set to 1. */ public DiceWithStatistics (Dice diceToCopy){ super (diceToCopy); rollsFrequency = new int [numberOfSides ]; resetFrequencies(); if (lastRoll !=0) rollsFrequency [lastRoll()-1] = 1; numberOfDieWithStatistics ++; } NOTE: extends, implements, the new attributes,

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    33 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us