csci 1112 lecture 3
play

CSCI 1112: Lecture 3 Mona Diab RoadMap High level view of Object - PowerPoint PPT Presentation

CSCI 1112: Lecture 3 Mona Diab RoadMap High level view of Object Oriented Programming (Classes) In class exercise Arrays Object Oriented Programming OOP is an approach to programming which supports the creation of new data


  1. CSCI 1112: Lecture 3 Mona Diab

  2. RoadMap • High level view of Object Oriented Programming (Classes) • In class exercise • Arrays

  3. Object Oriented Programming • OOP is an approach to programming which supports the creation of new data types and operations to manipulate those types.

  4. What is this Object ? • There is no real answer to the question, but we ’ ll call it a “ thinking cap ” . • The plan is to describe a thinking cap by telling you what actions can be done to it.

  5. Using the Object ’ s Slots • You may put a piece of paper in each of the two slots (green and red), with a sentence written on each. • You may push the green button and the thinking cap will speak the sentence from the green slot ’ s paper. • And same for the red button.

  6. Example

  7. Example That ¡test ¡was ¡ ¡a ¡breeze ¡! ¡

  8. Example I ¡should ¡ study ¡harder ¡! ¡

  9. Thinking Cap Implementation • We can implement the thinking cap public ¡class ¡ThinkingCap ¡ ¡ using a data type { ¡ ¡ ¡ called a class. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡. ¡. ¡. ¡ ¡ ¡} ¡ ¡ ¡

  10. Thinking Cap Implementation • The class will have two components public ¡class ¡ThinkingCap ¡ ¡ called greenWords { ¡ ¡ ¡ ¡ ¡ ¡ ¡String ¡greenWords; ¡ and redWords. ¡ ¡ ¡ ¡ ¡String ¡redWords; ¡ ¡ ¡ ¡ ¡ ¡ ¡. ¡. ¡. ¡ These components ¡ ¡ ¡ ¡ ¡ ¡ ¡ are strings which } ¡ ¡ hold the information ¡ that is placed in the two slots. • Using a class permits two features . . .

  11. Thinking Cap Implementation The two components will be private instance public ¡class ¡ThinkingCap ¡ ¡ variables. This { ¡ ¡ ¡ ¡ private ¡String ¡greenWords; ¡ ensures that nobody ¡ ¡ ¡private ¡String ¡redWords; ¡ ¡ ¡ ¡ ¡. ¡. ¡. ¡ can directly access } ¡ this information. The ¡ ¡ only access is through methods that we provide for the class.

  12. Thinking Cap Implementation In a class, the methods which class ¡ThinkingCap ¡ ¡ manipulate the class { ¡ ¡ ¡ ¡private ¡String ¡greenWords; ¡ are also listed. ¡ ¡ ¡private ¡String ¡redWords; ¡ ¡ ¡ ¡ ¡. ¡. ¡. ¡ } ¡ ¡ ¡ Implementations of the thinking cap methods go here.

  13. Thinking Cap Implementation Our thinking cap has at least three methods: public ¡class ¡ThinkingCap ¡ ¡ { ¡ ¡ ¡ ¡private ¡String ¡greenWords; ¡ ¡ ¡ ¡private ¡String ¡redWords; ¡ ¡ ¡ ¡ ¡public ¡void ¡slots(String ¡newGreen, ¡String ¡newRed)... ¡ ¡ ¡ ¡public ¡void ¡pushGreen( ¡)... ¡ ¡ ¡ ¡public ¡void ¡pushRed( ¡)... ¡ ¡ } ¡

  14. Thinking Cap Implementation The code for a new class is generally put in a Java package, as shown here: package ¡edu.colorado.simulaCons; ¡ public ¡class ¡ThinkingCap ¡ ¡ { ¡ ¡ ¡ ¡private ¡String ¡greenWords; ¡ ¡ ¡ ¡private ¡String ¡redWords; ¡ ¡ ¡ ¡ ¡public ¡void ¡slots(String ¡newGreen, ¡String ¡newRed)... ¡ ¡ ¡ ¡public ¡void ¡pushGreen( ¡)... ¡ ¡ ¡ ¡public ¡void ¡pushRed( ¡)... ¡ ¡ } ¡

  15. Using the Thinking Cap • A program import ¡ that wants to edu.colorado.simulaCons.ThinkingCap; ¡ ¡ use the ... ¡ thinking cap can import the ThinkingCap class.

  16. Using the Thinking Cap • Just for fun, import ¡ the example edu.colorado.simulaCons.ThinkingCap; ¡ program will ¡ public ¡class ¡Example ¡ declare two { ¡ ThinkingCap ¡ ¡ ¡ ¡ ¡public ¡staCc ¡void ¡main( ¡) ¡ variables named ¡ ¡ ¡ ¡ ¡ { ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ThinkingCap ¡student; ¡ student and fan. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ThinkingCap ¡fan; ¡

  17. Using the Thinking Cap • The variables are examples of import ¡ reference edu.colorado.simulaCons.ThinkingCap; ¡ variables, which ¡ public ¡class ¡Example ¡ means that they { ¡ have the ¡ ¡ ¡ ¡ ¡public ¡staCc ¡void ¡main( ¡) ¡ capability of ¡ ¡ ¡ ¡ ¡ { ¡ referring to ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ThinkingCap ¡student; ¡ ThinkingCap ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ThinkingCap ¡fan; ¡ objects that we ¡ create with the ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ student ¡= ¡new ¡ThinkingCap( ¡); ¡ “new” operator. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡fan ¡= ¡new ¡ThinkingCap( ¡); ¡

  18. Using the Thinking Cap • Once the import ¡ ThinkingCap edu.colorado.simulaCons.ThinkingCap; ¡ objects are ¡ created, we public ¡class ¡Example ¡ { ¡ can activate ¡ ¡ ¡ ¡ ¡public ¡staCc ¡void ¡main( ¡) ¡ methods such ¡ ¡ ¡ ¡ ¡ { ¡ as slot for the ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ThinkingCap ¡student; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ThinkingCap ¡fan; ¡ student ¡ thinking cap ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ student ¡= ¡new ¡ThinkingCap( ¡); ¡ object. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡fan ¡= ¡new ¡ThinkingCap( ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡student . slots( ¡"Hello", ¡ ¡"Bye"); ¡

  19. Using the Thinking Cap • Once the import ¡edu.colorado.simulaCons.ThinkingCap; ¡ ThinkingCaps ¡ public ¡class ¡Example ¡ are created, { ¡ we can ¡ ¡ ¡ ¡ ¡public ¡staCc ¡void ¡main(String[ ¡] ¡args) ¡ activate ¡ ¡ ¡ ¡ ¡ { ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ThinkingCap ¡student; ¡ methods such ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ThinkingCap ¡fan; ¡ as slot for the ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ student ¡= ¡new ¡ThinkingCap( ¡); ¡ student ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡fan ¡= ¡new ¡ThinkingCap( ¡); ¡ thinking cap. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡student . slots( ¡"Hello", ¡ ¡"Bye"); ¡

  20. Using the Thinking Cap The method ¡ activation ¡ ¡ consists of four ¡ parts, starting ¡ with the ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡student . slots( ¡"Hello", ¡ ¡"Bye"); ¡ variable name.

  21. Using the Thinking Cap The variable ¡ name is followed ¡ ¡ by a period. ¡ ¡ ¡ ¡ ¡ ¡ ¡student . slots( ¡"Hello", ¡ ¡"Bye"); ¡

  22. Using the Thinking Cap After the ¡ period is the ¡ ¡ name of the ¡ method that you ¡ are activating. ¡ ¡ ¡ ¡student . slots( ¡"Hello", ¡ ¡"Bye"); ¡

  23. Using the Thinking Cap  Finally, the ¡ ¡ ¡ ¡ ¡ ¡ arguments for ¡ the method. In ¡ this example ¡ the first ¡ argument ¡ ¡ ¡ ¡ ¡student . slots( ¡"Hello", ¡ ¡"Bye"); ¡ (newGreen) is "Hello" and the second argument (newRed) is "Bye".

  24. A Quiz How would you ¡ ¡ ¡ ¡public ¡staCc ¡void ¡main(String[ ¡] ¡args) ¡ activate student's ¡ ¡ ¡ ¡ { ¡ pushGreen method ? ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ThinkingCap ¡student; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ThinkingCap ¡fan; ¡ ¡ What would be the ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ student ¡= ¡new ¡ThinkingCap( ¡); ¡ output of student's ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡fan ¡= ¡new ¡ThinkingCap( ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡student . slots( ¡"Hello", ¡ ¡"Bye"); ¡ pushGreen method at this point in the program ?

  25. A Quiz Notice that the ¡ ¡ ¡ ¡public ¡staCc ¡void ¡main(String[ ¡] ¡args) ¡ pushGreen method ¡ ¡ ¡ ¡ { ¡ has no arguments. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ThinkingCap ¡student; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ThinkingCap ¡fan; ¡ ¡ At this point, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ student ¡= ¡new ¡ThinkingCap( ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡fan ¡= ¡new ¡ThinkingCap( ¡); ¡ activating ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡student . slots( ¡"Hello", ¡ ¡"Bye"); ¡ student.pushGreen will print the string ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡student.pushGreen( ¡); ¡ Hello .

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend