M1G413283 Introduction to Programming 2 4. Enhancing a class:Room.

Slides:



Advertisements
Similar presentations
1 Copyright © 2005, Oracle. All rights reserved. Introducing the Java and Oracle Platforms.
Advertisements

Introduction To Java Objectives For Today â Introduction To Java â The Java Platform & The (JVM) Java Virtual Machine â Core Java (API) Application Programming.
Object Orientated Programming
ISYS 512 Business Application Design and Development with.Net David Chao.
Introduction to Java Programming
CENG 161 Introduction to Computer Science Instructor: Dr. Nurdan SARAN Lab Assistant: Arzu Burçak Sönmez.
1 Chapter 3 Programs and Packages. 2 Java Virtual Machine (JVM) Java programs execute on the JVM. The JVM is a virtual rather than a physical machine,
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
JAVA ENVIRONMENT JDK, API, JVM. JAVA ENVIRONMENT  Java environment includes development tools and many classes and methods. Java Environment JDK (Java.
Introduction to Java.
Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department.
Session-02. Objective In this session you will learn : What is Class Loader ? What is Byte Code Verifier? JIT & JAVA API Features of Java Java Environment.
Introduction to the JDK Java for Computational Finance
01 Introduction to Java Technology. 2 Contents History of Java What is Java? Java Platforms Java Virtual Machine (JVM) Java Development Kit (JDK) Benefits.
Marlene Galea.  The JDK (Java Development Kit)  An IDE (Integrated Development Environment) ◦ Different IDEs include:  JCreator  NetBeans  BlueJ.
CMSC 202 Computer Science II for Majors Fall 2009 Introduction.
Introduction to Java Programming. Contents 1. Java, etc. 2. Java's Advantages 3. Java's Disadvantages 4. Types of Java Code 5. Java Bytecodes 6. Steps.
Introduction 01_intro.ppt
ISYS 350 Business Application Development
Introduction to Java Tonga Institute of Higher Education.
McGraw-Hill© 2007 The McGraw-Hill Companies, Inc. All rights reserved. 1-1.
CSCI 224 Introduction to Java Programming. Course Objectives  Learn the Java programming language: Syntax, Idioms Patterns, Styles  Become comfortable.
ISYS 512 Business Application Design and Development with.Net David Chao.
Architecture of.NET Framework .NET Framework ٭ Microsoft.NET (pronounced “dot net”) is a software component that runs on the Windows operating.
Microsoft Visual Basic 2005: Reloaded Second Edition
Lecture Set 1 Part B: Understanding Visual Studio and.NET – Structure and Terminology 1/16/ :04 PM.
Lesley Bross, August 29, 2010 ArcGIS 10 add-in glossary.
CSC 494/594 C# and ASP.NET Programming. C# 2012 C# Object-oriented language with syntax that is similar to Java.
CMSC 202 Computer Science II for Majors Object-Oriented Programming.
An intro to programming. The purpose of writing a program is to solve a problem or take advantage of an opportunity Consists of multiple steps:  Understanding.
Introduction to the Java Virtual Machine 井民全. JVM (Java Virtual Machine) the environment in which the java programs execute The specification define an.
Sadegh Aliakbary Sharif University of Technology Fall 2012.
Tutorial 111 The Visual Studio.NET Environment The major differences between Visual Basic 6.0 and Visual Basic.NET are the latter’s support for true object-oriented.
UCSC All rights reserved. No part of this material may be reproduced and sold. 1 IT1202-Fundamentals Of Programming (Using JAVA) Interacting with.
The Basics of Javadoc Presented By: Wes Toland. Outline  Overview  Background  Environment  Features Javadoc Comment Format Javadoc Program HTML API.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Java and C# [this is a bonus – it is not a required lesson] ACO101: Introduction to Computer Science.
POS 406 Java Technology And Beginning Java Code
Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007.
ISYS 812 Business Software Development David Chao.
ISYS 350 Business Application Development David Chao.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
1. An Introduction A Programming Language A Technology Java Development Kit Java API One Language: Three Editions Standard Edition Enterprise Edition.
M1G Introduction to Programming 2 5. Completing the program.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
CSCI 212 Object-Oriented Programming in Java. Prerequisite: CSCI 111 variable assignment statement while loop for loop post-increment (i++) strong typing.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
ITP 109 Week 2 Trina Gregory Introduction to Java.
Introduction to Programming 1 1 2Introduction to Java.
Introduction to JAVA Programming
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
Introduction CMSC 202 Fall Instructors Mr. Ryan Bergeron – Lecture Section 01 Tues/Thu 1:00 – 2:15 am, Sondheim 111 – Lecture Section 04 Tues/Thu.
Before You Begin Nahla Abuel-ola /WIT.
Topic: Difference b/w JDK, JRE, JIT, JVM
Introduction Enosis Learning.
2.1. Compilers and Interpreters
Module 1: Getting Started
Introduction Enosis Learning.
Advanced Programming: C# Lecture 01: Introduction
Chapter 7 –Implementation Issues
(Computer fundamental Lab)
Chap 1. Getting Started Objectives
Chap 4. Programming Fundamentals
Review of Previous Lesson
Presentation transcript:

M1G 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 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

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

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

API Documentation The documentation for the Java SE API can be found at: 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

“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

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

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

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

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

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 will store Room objects each with a String as its key Introduction to Programming 2 4. Enhancing a class: Room 29

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

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

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

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

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

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

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