1 / 14

Week 11

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

Télécharger la présentation

Week 11

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. Week 11

  2. An Object is An instance of some class that has state and behaviour

  3. The STATE of an object is the aggregate values of its attributes

  4. A constructor…. Places a newly created object into a well defined initial state

  5. 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)

  6. 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

  7. // 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

  8. // 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

  9. 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

  10. 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);

More Related