Presentation is loading. Please wait.

Presentation is loading. Please wait.

M1G413283 Introduction to Programming 2 5. Completing the program.

Similar presentations


Presentation on theme: "M1G413283 Introduction to Programming 2 5. Completing the program."— Presentation transcript:

1 M1G413283 Introduction to Programming 2 5. Completing the program

2 Different kinds of items At the moment there is only one kind of item in the game Could have different kinds of items which would behave differently when used Common behaviour, but some kinds of items may do some things differently, or have their own special extra behaviour Add BonusItem - its extra feature is that it can reveal a secret bonus keyword Introduction to Programming 2 5. Completing the program 2

3 Code pattern: “is-a” Problem: how do you implement a relationship where one class is a specialized version of another more general class and shares some of its behaviour Solution: the specialised class extends the more general class and adds new methods or overrides methods of the general class Introduction to Programming 2 5. Completing the program 3

4 Inheritance Defining a new class to create a new type can involve a lot of effort Sometimes a class already exists that is close to what you need You can extend that class to produce a new class that is exactly what you need You can extend your own classes, or you can extend other classes (for example the Java API classes) Introduction to Programming 2 5. Completing the program 4

5 Inheritance When you extend a class, the new class is called the subclass and the class that was extended is called the superclass. To extend another class you use the extends keyword in your new class declaration: Introduction to Programming 2 5. Completing the program 5

6 What is inherited? The subclass inherits all of the variables and all of the methods defined the superclass As if you had completely defined the new class from scratch, and had reproduced code already defined in the existing superclass Possible to define a new class with a minimum requirement to write new code Reuse the code that was previously written in superclass Introduction to Programming 2 5. Completing the program 6

7 Overriding Can change inherited behaviour by overriding some methods in your new class To override a method in your new class, define a method in your new class with the same name and signature as the original Then provide a body for the new method Write code in that body to cause the behaviour of the overridden method to be appropriate for an object of your new class Introduction to Programming 2 5. Completing the program 7

8 Additional behaviour Often a new subclass needs to implement additional behaviour You can simply add new methods to the subclass Introduction to Programming 2 5. Completing the program 8

9 Inheriting from Object Every class in Java extends some other class If you don't explicitly specify the class that your new class extends, it will automatically extend the class named Object All classes in Java are in a class hierarchy where the class named Object is the root of the hierarchy Introduction to Programming 2 5. Completing the program 9

10 The BonusItem class The BonusItem class extends the Item class, and inherits its use method It adds a new method, bonus, which prints out the value of a new field, codeWord Introduction to Programming 2 5. Completing the program 10

11 BonusItem object diagram Note that there is only one object here In the other relationships we have seen, the classes are used to create two or more collaborating objects Here, a single object is created by combining template information from two classes Introduction to Programming 2 5. Completing the program 11

12 BonusItem code Introduction to Programming 2 5. Completing the program 12

13 Polymorphism The word “polymorphism” literally means “one name, many forms” Polymorphism is an important idea in object- oriented programming One form of polymorphism makes use of inheritance Introduction to Programming 2 5. Completing the program 13

14 Polymorphism declare a variable of type Item, like this: This declaration says that there will be a variable called myItem which can refer to an object of type Item The object doesn’t exist yet - need to create, or instantiate it, using the new keyword Introduction to Programming 2 5. Completing the program 14

15 Polymorphism Polymorphism allows us to do a trick here. A variable of type Item can refer to either: an Item object, OR an object whose type is a subclass of Item This means we can do this: Introduction to Programming 2 5. Completing the program 15

16 Late binding Variable is declared with a specific type, known as the reference type But the actual type of the object it refers to may not be defined until the program is actually running The actual object type is the run-time type This is runtime polymorphism, sometimes also referred to as late-binding Introduction to Programming 2 5. Completing the program 16

17 Polymorphism in collections Polymorphism is particularly useful when dealing with collections of objects. The Room class has an ArrayList which can hold Item objects Through polymorphism, a reference to an Item can also refer to any subclass of Item When we add an item to the room, we can add either one of Item or BonusItem ArrayList can hold either type of object Introduction to Programming 2 5. Completing the program 17

18 Casting myItem has run-time type BonusItem The reference type is still Item You cannot call a method which is not defined in the object’s reference type Need to cast to run-time type: Introduction to Programming 2 5. Completing the program 18

19 Introducing interactivity to the game Up to this point the adventure game is lacking in interactivity There is no way for someone who is playing the game to control what happens In a text-based adventure game, players interact with the game by typing commands There is usually a limited set of commands which the game understands and which may cause some change in the game state Introduction to Programming 2 5. Completing the program 19

20 Commands An example of a command might be: go west The result of this command is that the Player object will go to another room, using the exit marked west First command word (go) indicates the type of action to take Second word (west) gives additional information about how to perform the action Introduction to Programming 2 5. Completing the program 20

21 Commands Some commands may have only one command word, for example: help This command would simply list the valid (first) command words in the game Introduction to Programming 2 5. Completing the program 21

22 The Command class A command is fairly simple –just one, or possibly two, strings. Useful, though, to have a class which represents a command Player object will then process a Command object within its takeTurn method Easier to write the new code in Player to do this if it can get a command as a single object rather than two separate strings Introduction to Programming 2 5. Completing the program 22

23 The Command class We can also put some additional methods into Command to make it more convenient to use A method hasSecondWord will provide an easy way to check whether a one-word or two-word command has been entered Another method isUnknown will provide an easy way to check whether an command with an invalid first word has been entered. Introduction to Programming 2 5. Completing the program 23

24 The Command class code Demonstration Introduction to Programming 2 5. Completing the program 24

25 Relationship between Player and Command There needs to be a relationship between Player and Command Player object will need to be able to send messages to a Command object to, for example, get the command words The Player object does not need to own the Command, it simply uses it in order to get information about what action to perform Introduction to Programming 2 5. Completing the program 25

26 Relationship between Player and Command This is another example of the “uses-a” pattern takeTurn method of Player has local variable of type Command: How does Command get created? Introduction to Programming 2 5. Completing the program 26

27 Turning user input into a Command There is something missing We need something which will take the players’ keyboard input and turn it into commands which can be processed The player could potentially type anything at all: one word, two words or more valid or invalid commands complete or partial commands Introduction to Programming 2 5. Completing the program 27

28 Turning user input into a Command This “something” needs to: Read in a line of text typed at the command prompt Split the input text into individual words Check that the first word is a valid command word Construct a Command object using the first word and the second word (if there is one), ignoring any additional words Introduction to Programming 2 5. Completing the program 28

29 The Parser class A Parser object performs a role but does not represent information in the game Introduction to Programming 2 5. Completing the program 29

30 Code pattern: “creates-a” Problem: how do you implement a “creates” relationship, where an object creates an instance of another class? Solution: the class which creates the instance has a method which returns a value whose type is the name of the other class. The instance is newly constructed within this method Introduction to Programming 2 5. Completing the program 30

31 Parser class getCommand method Introduction to Programming 2 5. Completing the program 31 return type is Command Command object created and returned

32 Complete version of takeTurn The variable commands is an array of type String which contains all the valid command words The command list is defined as a field in Player, which is then passed into the constructor of Parser Introduction to Programming 2 5. Completing the program 32

33 Processing a Command: the processCommand method The Player class has a method processCommand which uses the command word to decide what action to take print a message if the command word is “?” print a help message if the command word is “help” go to another room if the command word is “go” return true if the command word is “quit” – this will act as a flag to stop the game loop Introduction to Programming 2 5. Completing the program 33

34 Choosing from several options Introduction to Programming 2 5. Completing the program 34

35 The switch statement Introduction to Programming 2 5. Completing the program 35

36 The goRoom method Demonstration Introduction to Programming 2 5. Completing the program 36

37 The modified game loop Demonstration Introduction to Programming 2 5. Completing the program 37

38 Running the game Demonstration Introduction to Programming 2 5. Completing the program 38

39 Compiling and running without an IDE IDEs help programmers to do their job and be more productive Useful to know how to work “without a tightrope” Use command line tools in JDK to compile and run Java programs There are also many other tools in the JDK, including the javadoc tool Introduction to Programming 2 5. Completing the program 39

40 Compiling javac – the Java compiler Examples: javac Game.java javac *.java Creates a.class file for each Java class javac.exe needs to be in the PATH Usually located in bin folder inside JDK installation folder Introduction to Programming 2 5. Completing the program 40

41 Running java – the Java application launcher Example: java Game Requires Game.class to exist Class must have a main method All required classes must be in current folder or in the CLASSPATH java command can also execute and create JAR files with –jar option Introduction to Programming 2 5. Completing the program 41

42 Summary Programming techniques: Inheritance, polymorphism, casting, switch statement Class associations: “is-a” relationships, “uses-a” relationships, “creates-a” relationships Introduction to Programming 2 2. Creating Classes: Game and Player 42

43 What’s next? How to write programs with graphical interfaces How to write programs with web page interfaces How to write graphics-based games How to write programs which work with databases How to write programs which communicate over networks How to use other languages, such as C# or C++ How to use more advanced development tools, such as NetBeans or Visual Studio and so on... Introduction to Programming 2 5. Completing the program 43


Download ppt "M1G413283 Introduction to Programming 2 5. Completing the program."

Similar presentations


Ads by Google