140 likes | 209 Views
Week 11. An Object is. An instance of some class that has state and behaviour. The STATE of an object is. the aggregate values of its attributes. A constructor…. Places a newly created object into a well defined initial state. Object. GameCounter. - position : integer
E N D
An Object is An instance of some class that has state and behaviour
The STATE of an object is the aggregate values of its attributes
A constructor…. Places a newly created object into a well defined initial state
Object GameCounter - position : integer - MAXSQUARE : integer + GameCounter() + moveIt( distance:integer) + integer getPosition() + boolean hasFinished() # setPosition( position : integer) BouncingCounter JumpBackCounter + BouncingCounter() + moveIt( distance:integer) + JumpBackCounter() + moveIt( distance:integer)
public class GameCounter extends Object { private int position; privatefinal static int MAXSQUARE = 100; public GameCounter(){ super(); position = 1; } publicvoid moveIt( int distance){ int oldPosition = position; position += distance; if ( position > MAXSQUARE){ position = oldPosition; } // End if } // End Moveit() public int getPosition(){ return position; } // End getPosition() protectedvoid setPosition( int position){ this.position = position; } // End setPosition public boolean isFinished(){ if( position == MAXSQUARE){ return true; }else { return false; } // End if } // End hasFinished() } // End class GameCounter
// BouncingCounter.java publicclass BouncingCounter extends GameCounter{ public BouncingCounter(){ super(); } // End constructor publicvoid moveIt( int distance){ int newPosition = getPosition() + distance; if( newPosition > maxSquare){ setPosition(( 2 * maxSquare) - newPosition ); } else { setPosition( newPosition); } // End If } // End moveIt() } // End class BouncingCounter
// JumpBackCounter.java publicclass JumpBackCounter extends GameCounter{ public JumpBackCounter(){ super(); } // End Constructor publicvoid moveIt( int distance){ int newPosition = getPosition() + distance; if( newPosition > maxSquare){ setPosition(90); } else { setPosition(getPosition() + distance); } // End if } // End JumpBackCounter() } // End JumpBackCounter
write the first line of the class declare the attributes Write the constructor The addTo() method accepts a Bloop as an argument adds the size of the other Bloop to the size of this bloop and increments the count The combine() method accepts another Bloop as an argument and returns a Bloop. The new Bloop gets the sum of the sizes in its size and the sum of the counts in its count the toString() method returns a string something like "size = 6 and count = 2" to reflect the state of the Bloop publicclass Bloop extends Object { private int size; private int count; public Bloop ( int size){ this.size = size; count = 0; } // End Constructor publicvoid addTo( Bloop anotherBloop){ count ++; size += anotherBloop.size; } // End AddTo public Bloop combine( Bloop anotherBloop){ Bloop toReturn = new Bloop(size); toReturn.size += anotherBloop.size; toReturn.count = this.count + anotherBloop.count; return toReturn; } // End combine public String toString(){ return "Size = " + size + " and count = " + count; } // End toString } // End class Bloop
publicclass BloopDemonstration extends Object { publicstaticvoid main (String [] args){ create 2 instances of Bloops referred to as thisBloop (with a start value of 5) and anotherBloop ( with a start value of 4) add anotherBloop to thisBloop display the Bloops create a Bloop reference variable called total and set it to null combine()thisBloop with anotherBloop and put the result in result display the result } // End main() } // End BloopDemonstration Bloop thisBloop = new Bloop(5); Bloop anotherBloop = new Bloop(4); thisBloop.addTo(anotherBloop); System.out.println( "thisBloop = " + thisBloop); System.out.println( "anotherBloop = " + anotherBloop); Bloop result = null; result = thisBloop.combine( anotherBloop) System.out.println("The Result is " + result);