Presentation is loading. Please wait.

Presentation is loading. Please wait.

LimitedCounter - extension & inheritance BasicCounter LimitedCounter Every LimitedCounter instance inherits every resource (attributes & methods) which.

Similar presentations


Presentation on theme: "LimitedCounter - extension & inheritance BasicCounter LimitedCounter Every LimitedCounter instance inherits every resource (attributes & methods) which."— Presentation transcript:

1 LimitedCounter - extension & inheritance BasicCounter LimitedCounter Every LimitedCounter instance inherits every resource (attributes & methods) which BasicCounter provides. … so everything a BasicCounter can do, can also be done by a LimitedCounter. … everywhere a BasicCounter is needed (e.g. data flows) a LimitedCounter can be used. A LimitedCounter is a BasicCounter !

2 LimitedCounter - class diagram BasicCounter LimitedCounter package counters DEFAULT MINIMUM DEFAULT MAXIMUM minimum Count maximum Count LimitedCounter isAtMinimum isAtMaximum minimumIs maximumIs maxToCount minToCount boolean minimum maximum

3 LimitedCounter - inheritance and overriding LimitedCounter overrides: 2 constructors. LimitedCounter supplies: 2 class-wide data attributes 2 instance data attributes 3 new constructor 2 boolean inquiry methods 2 integer inquiry methods LimitedCounter inherits: 2 instance data attributes 3 counting methods 1 integer inquiry method 1 state setting method

4 LimitedCounter - header 0001 // Filename counters/BasicCounter.java. 0002 // First extension of the Counters hierarchy providing 0003 // minimum & maximum values. 0004 // 0005 // This version written for SDO lecture 1. 0006 // Fintan Culwin, v0.1, February 1998. 0007 0008 package counters; 0009 0010 public class LimitedCounter extends BasicCounter {

5 LimitedCounter - attributes DEFAULT MINIMUM DEFAULT MAXIMUM Minimum Count Maximum Count 0012 static private final int DEFAULT_MINIMUM = 0; 0013 static private final int DEFAULT_MAXIMUM = 999; 0014 0015 private int miniumumCount = DEFAULT_MINIMUM ; 0016 private int maximumCount = DEFAULT_MAXIMUM ;

6 LimitedCounter - constructors LimitedCounter maxToCount minToCount 0019 public LimitedCounter(){ 0020 this( DEFAULT_MINIMUM, DEFAULT_MAXIMUM); 0021 } // End Default Constructor. 0022 0023 0024 public LimitedCounter( int maxToCount){ 0025 this(DEFAULT_MINIMUM, maxToCount); 0026 } // End alternative Constructor. 0027 0028 0029 public LimitedCounter( int minToCount, int maxToCount){ 0030 super( minToCount); 0031 miniumumCount = minToCount; 0032 maximumCount = maxToCount; 0033 } // End principal Constructor.

7 LimitedCounter - boolean inquiry methods isAtMinimum isAtMaximum boolean 0036 public boolean isAtMinimum() { 0037 return this.numberCountedIs() == miniumumCount; 0038 } // End isAtMinimum. 0039 0040 public boolean isAtMaximum() { 0041 return this.numberCountedIs() == maximumCount; 0042 } // End isAtMaximum.

8 LimitedCounter - int eger inquiry methods minimumIs maximumIs minimum maximum 0045 public int minimumIs() { 0046 return miniumumCount; 0047 } // End minimumIs. 0048 0049 public int maximumIs() { 0050 return maximumCount; 0051 } // End maximumIs. 0052 0053 } End LimitedCounter.

9 LimitedCounterDemo - fragment... 0018 System.out.print( "\n\nConstructing an instance with the "); 0019 System.out.println( "range 10 to 12."); 0020 aCounter = new LimitedCounter( 10, 12); 0021 System.out.print( "Instance created..."); 0023 System.out.print( "\nDemonstrating minimumIs(), "); 0024 System.out.print( "it should be 10... "); 0025 System.out.println( aCounter.minimumIs()); 0027 System.out.print( "\n\nDemonstrating numberCountedIs(), "); 0028 System.out.print( "it should be 10... "); 0029 System.out.println( aCounter.numberCountedIs()); 0030 0031 System.out.print( "\n\nDemonstrating isAtMinimum(), "); 0032 System.out.print( "it should be true... "); 0033 System.out.println( aCounter.isAtMinimum());

10 LimitedCounterDemo - fragment output... Limited Counter Demonstration Constructing an instance with the range 10 to 12. Instance created … Demonstrating minimumIs(), it should be 10... 10 Demonstrating numberCountedIs(), it should be 10... 10 Demonstrating isAtMinimum(), it should be true... true


Download ppt "LimitedCounter - extension & inheritance BasicCounter LimitedCounter Every LimitedCounter instance inherits every resource (attributes & methods) which."

Similar presentations


Ads by Google