M1G413283 Introduction to Programming 2 5. Completing the program.

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 6 Object Oriented Programming in Java Language Basics Objects.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
ITEC200 – Week03 Inheritance and Class Hierarchies.
Object-Oriented PHP (1)
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Games and Simulations O-O Programming in Java The Walker School
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming.
Object Oriented Software Development
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
M1G Introduction to Programming 2 4. Enhancing a class:Room.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
Object Oriented Software Development
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
Introduction to Object-Oriented Programming Lesson 2.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Object-Oriented Programming: Inheritance and Polymorphism.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Object-Oriented Programming: Polymorphism Chapter 10.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Interfaces and Inheritance
Designing for Inheritance
Week 6 Object-Oriented Programming (2): Polymorphism
Object-Oriented Programming: Inheritance and Polymorphism
Review of Previous Lesson
Object-Oriented PHP (1)
Presentation transcript:

M1G Introduction to Programming 2 5. Completing the program

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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