1 / 36

M1G413283 Introduction to Programming 2

M1G413283 Introduction to Programming 2. 4. Enhancing a class:Room. The Java platform. Java is not just a programming language. It also provides: the Java Application Programming Interface (API) which is a set of library classes which Java developers can use within their own applications

chogan
Download Presentation

M1G413283 Introduction to Programming 2

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. M1G413283Introduction to Programming 2 4. Enhancing a class:Room

  2. The Java platform • Java is not just a programming language. It also provides: • the Java Application Programming Interface (API) which is a set of library classes which Java developers can use within their own applications • the Java Virtual Machine (JVM) which is the platform that allows Java programmes to run on a computer Introduction to Programming 2 4. Enhancing a class: Room 2

  3. Java APIs • There are actually many Java APIs, both official and third-party • We will look here only at classes which are provided within the official core Java API, which is contained within the Java Development Kit (JDK) • The current version of the core API is known as Java SE 7 – SE stands for Standard Edition Introduction to Programming 2 4. Enhancing a class: Room 3

  4. Java APIs • The Java SE API provides classes which help with many aspects of programming • working with databases • creating graphical user interfaces • for working with networks • etc. • These classes are organised in packages • Each contains a set of related classes • e.g. java.sql, java.awt, java.net Introduction to Programming 2 4. Enhancing a class: Room 4

  5. API Documentation • The documentation for the Java SE API can be found at: http://docs.oracle.com/javase/7/docs/api/ • This website provides javadoc pages for all the classes in the API • It is virtually impossible to write a “real” Java program without referring to the API documentation Introduction to Programming 2 4. Enhancing a class: Room 5

  6. API Documentation Introduction to Programming 2 4. Enhancing a class: Room 6

  7. Other languages and their APIs • Most object-oriented programming languages have a similar set of library classes • For example, Microsoft’s C# and Visual Basic.NET are supported by the library classes in the .NET Framework • Web-based documentation is on the MSDN (Microsoft Developer Network) website Introduction to Programming 2 4. Enhancing a class: Room 7

  8. Java versions • The Java Runtime Environment (JRE) • This provides the JVM and the core Java classes. • To run programs it is sufficient to have a JRE installed on the system • The Java Software Development Kit (JDK or Java SDK) • This includes the JRE, together with compilers and debuggers and other development tools • To develop Java programs, a JDK must be installed on your system Introduction to Programming 2 4. Enhancing a class: Room 8

  9. Java Virtual Machine • A Java source code file must be compiled to an intermediate language called bytecode • Bytecode is interpreted by the Java Virtual Machine(JVM) • JVM translates into the machine code for the specific computer system it runs on • Java source files have a .java extension. • Bytecode (compiled) files have a .class extension Introduction to Programming 2 4. Enhancing a class: Room 9

  10. Other languages and their execution • Other programming languages often have similar platforms for running programmes • For example, C# and Visual Basic.NET source code is compiled to an intermediate language called Microsoft Intermediate Langauge (MSIL) • Interpreted by a component called the Common Language Runtime (CLR) Introduction to Programming 2 4. Enhancing a class: Room 10

  11. Using library classes • Choosing the right library classes can make programs easier to write and maintain • We have seen some examples in the labs • e.g. Date class for handling dates • We will look at some classes which can be used for storing collections of information instead of arrays Introduction to Programming 2 4. Enhancing a class: Room 11

  12. Problems with arrays • The array size is fixed – what happens if we want to add more items than the maximum number? • We need to keep track separately of the number of items which have been stored in the array • The code for removing items from the array is a bit clumsy and may involve moving elements around within the array Introduction to Programming 2 4. Enhancing a class: Room 12

  13. The ArrayList class • ArrayList is a library class which is part of the JavaSE API • The name sounds quite similar to an array, but ArrayList has a number of very useful features: • No need to track number of items • Resizes itself as required • Provides methods to add, get, remove, etc. Introduction to Programming 2 4. Enhancing a class: Room 13

  14. The ArrayList class Introduction to Programming 2 4. Enhancing a class: Room 14

  15. ArrayList methods Introduction to Programming 2 4. Enhancing a class: Room 15

  16. ArrayList fields and constructors Introduction to Programming 2 4. Enhancing a class: Room 16

  17. Declaring an ArrayList • Declares an ArrayList which holds Items • No need to track number of items as ArrayList’s size method gives this Introduction to Programming 2 4. Enhancing a class: Room 17

  18. Constructing an ArrayList • Initial capacity is 10 by default • Will resize itself if it needs to Introduction to Programming 2 4. Enhancing a class: Room 18

  19. Using ArrayList methods Introduction to Programming 2 4. Enhancing a class: Room 19

  20. Using ArrayList methods Introduction to Programming 2 4. Enhancing a class: Room 20

  21. Room and Item: revised object diagram • Don’t need to look inside ArrayList object, just use its methods • Information hiding Introduction to Programming 2 4. Enhancing a class: Room 21

  22. Re-testing Room and Player • Demonstration Introduction to Programming 2 4. Enhancing a class: Room 22

  23. Linking rooms together • So far we can only have one room in the game • Once a player has been placed in a room there is no way out of that room • We will need to make it possible for rooms to have exits • An exit should lead into another room • This will allow us to join rooms together to design a game “world” Introduction to Programming 2 4. Enhancing a class: Room 23

  24. “Self” relationship • Room must be associated with the other room or rooms next to it in the game world • Shown in the class diagram by a self-relationship Introduction to Programming 2 4. Enhancing a class: Room 24

  25. Code Pattern: “Self” • Problem: how do you implement a self- relationship, where an object is linked to other objects of the same class • Solution: the class has a field whose type is the same as the name of the class Introduction to Programming 2 4. Enhancing a class: Room 25

  26. Code example of self pattern Introduction to Programming 2 4. Enhancing a class: Room 26

  27. Providing more than one exit • Need to allow a room to have several exits. • Could use an ArrayList? • There may be another library class which would be a better choice here • The Java SE API provides many classes for storing collections of objects in different ways • Known as the Collections Framework Introduction to Programming 2 4. Enhancing a class: Room 27

  28. Providing more than one exit • It would be useful to be able to store each exit along with a label to identify it • This will make it easy to select a room based on the player’s choice • For example, a room might have exits labelled “east”, “west”, and so on • The player will then be able to choose to “go east”, or “go west”, and so on Introduction to Programming 2 4. Enhancing a class: Room 28

  29. Using a Hashmap • The HashMap library class is a good choice here • You can look up the documentation for this and see if you agree • A HashMap stores objects (as many as needed) along with a label, or key, for each one • A HashMap<String, Room> will store Room objects each with a String as its key Introduction to Programming 2 4. Enhancing a class: Room 29

  30. Using a HashMap • Declaration • In constructor of Room Introduction to Programming 2 4. Enhancing a class: Room 30

  31. Getting objects in and out • HashMap documentation • Room methods Introduction to Programming 2 4. Enhancing a class: Room 31

  32. More changes to Room – test again • Demonstration Introduction to Programming 2 4. Enhancing a class: Room 32

  33. Creating the game “world” • We can now add a method to the Game class to create the game “world” as a set of linked rooms • All players will start in the same room and can then navigate through the world by moving from room to room • The Game object only needs to store a reference to the starting Room object Introduction to Programming 2 4. Enhancing a class: Room 33

  34. Game world map Introduction to Programming 2 4. Enhancing a class: Room 34

  35. Game world setup code • Demonstration Introduction to Programming 2 4. Enhancing a class: Room 35

  36. Summary • Programming techniques: library classes, using API documentation, using ArrayList and HashMap • Class associations: “self” relationships Introduction to Programming 2 2. Creating Classes: Game and Player 36

More Related