03 objects

790 views
748 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
790
On SlideShare
0
From Embeds
0
Number of Embeds
79
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Demo what we have learned so far: conditional statements; loops; variables; objects; inputs; web
  • 03 objects

    1. 1. How Objects Can Change Your Life. LIS4930 © PIC <ul><ul><li>The Setup: Two programmers, Larry and Brad, are given a challenge by their project manager to build a program that meets the following specifications. First to finish wins an extra weeks vacation. </li></ul></ul>SPEC There will be shapes on a GUI, a square, a circle, and a triangle. When the user clicks on a shape, the shape will rotate clockwise 360° and play an AIF sound file specific to that particular shape.
    2. 2. LIS4930 © PIC Larry’s Cube (procedural approach) Brad’s Café (OOP approach) rotate (shapeNum) { // make the shape rotate // 360° } playSound (shapeNum) { //use shapeNum to lookup // which AIF sound to play, //and play it } Square rotate ( ) { // code to rotate a //square } playSound ( ) { // code to play the // AIF file for a // square } rotate ( ) { // code to // rotate a triangle } playSound ( ) { // code to play the // AIF file for a triangle } Triangle rotate ( ) { // code to rotate a //circle } playSound ( ) { // code to play the // AIF file for a // circle } Circle Which do you think is better?
    3. 3. Hold the Press! There’s a spec change. LIS4930 © PIC SPEC Change There will be an amoeba shape on the screen, with the others. When the user clicks on the amoeba, it will rotate like the others, and play a .hif sound file.
    4. 4. LIS4930 © PIC Larry’s Cube (procedural approach) Brad’s Café (OOP approach) rotate (shapeNum) { // make the shape rotate // 360° } playSound (shapeNum) { //if the shape is not an //amoeba, use shapeNum //to lookup which AIF //sound to play, and play it //else //play amoeba .hif sound } Amoeba Square rotate ( ) { // code to rotate a //square } playSound ( ) { // code to play the // AIF file for a // square } rotate ( ) { // code to // rotate a triangle } playSound ( ) { // code to play the // AIF file for a triangle } Triangle rotate ( ) { // code to rotate a //circle } playSound ( ) { // code to play the // AIF file for a // circle } Circle rotate ( ) { // code to rotate a //circle } playSound ( ) { // code to play the // new .hif file for an // amoeba } Why is it not a good idea to edit previously tested code?
    5. 5. What the Spec Forgot to Mention… LIS4930 © PIC Amoeba rotation point in Larry and Brad’s version. Where the amoeba rotation point should be. SPEC Change
    6. 6. LIS4930 © PIC Larry’s Cube (procedural approach) Brad’s Café (OOP approach) rotate (shapeNum, xPt, yPt) { //if the shape is not an amoeba, //calculate the center point //based on a rectangle, then //rotate. //else //use the xPt and yPt as the //rotation point offset and //then rotate. } playSound (shapeNum) { //if the shape is not an //amoeba, use shapeNum //to lookup which AIF //sound to play, and play it //else //play amoeba .hif sound } int xPoint; int yPoint; rotate ( ) { //code to rotate an amoeba //using amoeba’s x and y } playSound ( ) { // code to play the // new .hif file for an // amoeba } Amoeba Which approach is more efficient? Changing one class or editing the entire file?
    7. 7. SO did Brad win the vacation? LIS4930 © PIC Well Larry didn’t go down without a fight and pointed out that Brad’s code had a lot of duplicated code in it…??? Ahaa but did Larry see Brad’s final design? This is what Brad did… Square rotate ( ) playSound ( ) rotate ( ) playSound ( ) Triangle rotate ( ) playSound ( ) Circle rotate ( ) playSound ( ) Amoeba Larry looked at what all four classes had in common. 1 2 They’re all shapes, and they all rotate and playSound. So Larry abstracted out the common features and put them into a new class called Shape. Shape rotate ( ) playSound ( )
    8. 8. LIS4930 © PIC Then Larry linked the other four classes to the new Shape class, in a relationship called inheritance . Shape rotate ( ) playSound ( ) superclass You can read this as, “Square inherits from Shape”, “Circle inherits from Shape”, and so on. rotate() and playSound() have been taken out of all other shapes and replaced by one copy in a superclass called Shape. The other four are the subclasses of Shape. The subclasses inherit the methods of the superclass. Square Triangle Circle Amoeba 3 subclasses
    9. 9. What about the Amoeba rotate()? LIS4930 © PIC Wasn’t that the whole problem here – that the amoeba shape had a completely different rotate and playSound procedure? How can Amoeba do something different if it “inherits” its functionality from the Shape class??? Shape rotate ( ) playSound ( ) Square Triangle Circle rotate ( ) { // code to rotate a //circle } playSound ( ) { // code to play the // new .hif file for an // amoeba } Amoeba 4 The Amoeba class overrides the methods of the Shape class. Then at runtime, the JVM knows exactly which rotate() method to run when someone tells the Amoeba to rotate. Overriding just means that a subclass redefines one of its inherited methods when it needs to change or extend the behavior of that method. Overriding methods
    10. 10. Back to Classes LIS4930 © PIC <ul><li>When you design a class you need to think about the objects that will be created from that class and the: </li></ul><ul><ul><li>things the object KNOWS </li></ul></ul><ul><ul><li>things the object DOES </li></ul></ul>Shopping Cart cartContents addToCart() removeFromCart() checkCart() knows does Button label color setColor() setLabel() pressOn() pressOff() knows does Alarm alarmTime alarmMode getAlarmTime() setAlarmTime() isAlarmSet() snooze() knows does Remember a class is the blueprint for an object!
    11. 11. LIS4930 © PIC <ul><li>Things an objects knows about itself are called </li></ul><ul><ul><li>instance variables </li></ul></ul><ul><li>Things an object can do are called </li></ul><ul><ul><li>methods </li></ul></ul>instance variables (state) methods (behavior) Song knows does What could go here? What could go here? Why are they called “instance” variables? What does “state” mean?
    12. 12. Making Your First Object LIS4930 © PIC Write your class 1 class Dog { int size; String breed; String name; void bark() { System.out.println(“Woof! Woof!”); } } Dog size breed name bark() instance variables methods Write a tester (TestDrive) class 2 class DogTestDrive { public static void main (String [ ] args) { //Dog test code goes here } }
    13. 13. Making Your First Object LIS4930 © PIC In your test, make an object and access the object’s variables and methods 3 class DogTestDrive { public static void main (String [ ] args) { Dog rufus = new Dog(); rufus.size = 40; rufus.breed = “cockapoo”; rufus.name = “Rufus”; rufus.bark(); } } rufus size = 40 breed = cockapoo name = Rufus bark() Make a new Dog object named “rufus” Use the dot operator (.) to set the size, breed, and name of rufus. Use the dot operator (.) to call the rufus’ bark() method. If you already have some OOP experience, you might notice we are not using encapsulation. We’ll get to that in chapter 4 of the text.
    14. 14. Another Example of Building a Class and Testing it. LIS4930 © PIC class Movie { String title; String genre; int rating; void playIt() { System.out.println( “ Playing the movie”); } } public class MovieTestDrive { public static void main(String [ ] args) { Movie one = new Movie(); one.title = “Gone with the Stock”; one.genre = “Tragic”; one.rating = - 2; Movie two = new Movie(); two.title = “Lost in Cubicle Space”; two.title = “Comedy”; two.rating = 5; two.playIt(); Movie three = new Movie(); three.title = “Byte Club”; three.genre = “Tragic but ultimately uplifting”; three.rating = 100; } } class tester Why is there no main()?
    15. 15. Escape from Main(e)!?! LIS4930 © PIC As long as you are still in main() you are not truly using OOP design. It’s fine for a test program to run within the main method, but in a true OO application, you need objects talking to other objects, as opposed to a static main() methods creating and testing objects. The two uses of main: <ul><li>to test your real class </li></ul><ul><li>to launch/start your Java application </li></ul>A real Java application is nothing but objects talking to other objects. In this case, talking means objects calling methods on one another. As a ‘sneak preview’, though, of how a real Java application might behave, here’s a little example. The Guessing Game Summary The guessing game involves a ‘game’ object and three ‘player’ objects. The game generates a random number between 0 and 9, and the three player objects try to guess it. Classes GuessGame.class Player.class GameLauncher.class
    16. 16. Java Takes Out The LIS4930 © PIC Trash Each time an object is created in Java, it goes into an area of memory known as The Heap. All objects – no matter when, where, or how they’re created – live on the heap. But it’s not just any old memory heap; the java heap is actually called the Garbage-Collection Heap. When you create an object, Java allocates memory space on the heap according to how much that particular object needs. An object with, say, 15 instance variables, will probably need more space than an object with only two instance variables. But what happens when you need to reclaim that space? How do you get an object out of the heap when you’re done with it? Java manages that memory for you!

    ×