1 / 19

Day 7

Day 7. Public vs. private instance variables Methods calling methods And type casting. Method Heading. public void writeOutput () { System.out.println (“Name =“ + name): }. Method Heading. Body of Method Definition. Methods calling methods. Warm-up Scenario. UML Class Diagram:.

dorcas
Télécharger la présentation

Day 7

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. Day 7 Public vs. private instance variablesMethods calling methods And type casting

  2. Method Heading public void writeOutput(){ System.out.println(“Name =“ + name): } Method Heading Body of Method Definition

  3. Methods calling methods

  4. Warm-up Scenario • UML Class Diagram:

  5. public vs. private instance variables • Write a class with public instance variables • Write a main program/file and change the values of the variables on different lines • Can you easily change the values of the variables? Whenever you want?

  6. Solution

  7. public vs. private instance variables • Now modify your class so that it has private instance variables • In the main program/file, try to modify the variables on different lines • Can you easily change the values of the variables? Whenever you want?

  8. public vs. private instance variables • In your class, create get and set methods so we can return the values or access them

  9. Revise with private variables • UML Class Diagram:

  10. Solution

  11. public vs. private instance variables • Observation: once it’s private, you can’t simply change the value of an instance variable directly. • You can only change it or access it upon calling methods in the class so that everything is encapsulated(actions can only take place by using the class’s methods)

  12. public vs. private instance variables • This way, if you’re sharing a class (i.e., 10 game developers are using one class), the methods are being called upon to update, set or return values.

  13. Type casting • Type cast involves changing the type of a value from its normal type to some other type

  14. Type casting • Type cast involves changing the type of a value from its normal type to some other type • Example: changing a 2.0 to a 2

  15. Type casting • Type cast involves changing the type of a value from its normal type to some other type • Example: changing a 2.0 to a 2 • double distance distance = 9.0;int points; points = distance;

  16. Type casting • Type cast involves changing the type of a value from its normal type to some other type • Example: changing a 2.0 to a 2 • double distance distance = 9.0;int points; points = distance; An illegal assignment

  17. Type casting • Type cast involves changing the type of a value from its normal type to some other type • Using it correctly: • double distance distance = 9.0;int points; points = (int)distance; A legal assignment

  18. Recap

  19. Assignment #1 Posted on Wiki

More Related