Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439

Compiled by Ken Eustace, May 2000-2004 using major works by Pavel Curtis, Jan Holmevik and Cynthia Haynes

Introduction

It all began with AusieMOO in September 1994, with the ISPG at Charles Sturt University hosting Australia's first educational and social MOO. Today AussieMOO has been set free and is operating at http://www.aussiemoo.org/ by a team of talented programmers and Web developers. You can download a copy of the Dank Fairy's The Ultimate AussieMOO Manual which is described as the definitive guide to AussieMOO, but also covers a great deal of MOO learning in general as well as a host of information specific to AussieMOO. Ten years later the ISPG provides two main MOO services:

for training and development of programmers, fun "learn-by-doing projects" and a space for MOO research.
A facility for professional groups, educators and students for workgroup practice.

The MOO programming workshop can take place at either MOO and may be applied for use at other MOOs.

According to Pavel Curtis in his MOO programming manual, one-line commands are parsed and interpreted by the MOO. Such commands may cause changes in the virtual reality, such as the location of a character, or may simply report on the current state of that reality, such as the appearance of some object. The job of interpreting those commands is shared between the two major components in the MOO system: the server and the database. Either way, this workshop will change your thinking as a programmer as you program your own self as an avatar object in an object-oriented virtaul world.

Some time ago I drew a diagram to explain how the architecture of the MOO may be of interest to client/server application developers as it shows horizontal scalabity by use of different clients and protocols - telnet and HTTP. The enCore Xpress system devised over several years by Jan Holmevik and Cynthia Haynes, uses Java applets to build a GUI interface and to extend the database core, as shown in figure 1. Multimedia users connect via HTTP while text-only users may use a telnet client. Many exist for customised telnet access, but I use Pueblo/UE. You can download the latest version from . I use HTTP with MOO classes and workshops and programming, while I also use Pueblo for patching upgrades and system tests. You can program the MOO with either client, but the enCore program editot is useful. The command are given in this workshop so you can program through either client type.

Figure 1. The client/server architecture of the enCore Xpress MOO system.

The MOO server is a program, written in a standard programming language, that manages the network connections, maintains queues of commands and other tasks to be executed, controls all access to the database, and executes other programs written in the MOO programming language.

The database contains representations of all the objects in the virtual reality, including the MOO programs, that the server executes to give those objects their specific behaviors. Almost every command is parsed by the server into a call on a MOO procedure, or verb, that actually does the work. Thus, programming in the MOO language is a central part of making non-trivial extensions to the database and thus, the virtual reality.

Objects in the MOO

The object with number `#494' in LC_MOO does not exist just because you write down its number, but it is a good idea that you write down object numbers on creation.. An explicit operation, the `create()' function described later, is required to bring an object into existence. Symmetrically, once created, objects continue to exist until they are explicitly destroyed by the `recycle()' function.

The identifying number associated with an object is unique to that object. It was assigned when the object was created and will never be reused, even if the object is destroyed. Thus, if we create an object and it is assigned the number `#1076', the next object to be created will be assigned `#1077', even if `#1076' is destroyed in the meantime. Every object is made up of three kinds of pieces that together define its behavior: attributes, properties, and verbs. Verbs are to MOO like methods toJava.

K9MOO uses a canine metaphor

Canine metaphor and themes are used as an “alternative world” to a university. With its learn-by-doing apporach, K9 has a simple topology for builders:

Creating your online persona

Stop and close you eyes. Imagine a scripting language sitting on top of a network interface. The server provides a basic framework, but all the real behaviors are programmed in the scripting language and part of the "database".
A MOO database is a collection of MOO objects, with inheritance, properties, and program code. It's not a relational database. In a MOO, an object in the game world sense is the same as an object in the programming sense.
, so harness your imagination and hang on to your seat!

Workshop 1: Designing your part of the canine world

In workshop 1 we must stop and design our character or avatar using text and images and by setting properties using the OPTIONS button on the menu applet .

1. Use a checklist as a design tool for a scalable, portable design. This is done by making 3 plans: Thematic,Technical and Community plans. At least have a thematic plan before you start to program.
Thematic plan – think of your part of the world as a theme park that must have:

Technical Plan
Which technologies, protocols, interfaces?

Community Plan

Here is my basic design for my avatar at LC_MOO, where my MOO wizard character is (#247):

Environment:
Theme: Tundra habitat/ children’s literature
Task/Role: Secretariat member; global warming researcher
Group space: #404
Personal space: - block of sea ice #494
Project space: #267

Table 1. Hopper's details

And at K9MOO my Wizard character is (#2):

Environment:
Theme: Gone to the dogs!
Task/Role: Top Dog to maintain admin position
Group space: (#161) or the (#62)
Personal space: (#202)
Project space: (#190)

Table 2. GrisWald's details.

2. Fill yours in now...

Environment:  
Theme:  
Task/Role:  
Group space:  
Personal space:  
Project space:  

Table 3. Your draft details.

3. Design the avatar as part of design. Add more properties to your character via Options button. Learn by doing. Make a change and see what happens. Use the URL for each multimedia content and icon. I suggest you make a folder in your ZOPE folder or any Web server where you can store documents and store encore MOO images, sounds etc for you MOO programming exercises:

Figure 2. Player object properties to edit..

Creating rooms, spaces or even a kennel!

Here are 3 ways to create a room by entering commands (Courier New font) directly into the telnet window. You may need a Wizard character to help allow rooms to be connected in certain areas. You can use MOO Mail (or e-mail) to post such a message to a Wizard character. To find out you object inventory and the object numbers of each of your objects, simple type @audit me in the telnet screen entry window.

If you have to make a change to an object's name, type @rename me to jane will change your name to Jane.

Room WITHOUT entrances and exits: @dig mykennel
Room WITH entrances and exits: @dig in|out to mykennel
Add entrance and exit to current room without them already, to the attic: @dig up|down to attic

The room names are interachangeable with room numbers, so instead of using "attic" which has a room number of #1313, we can use the object number: @dig up|down to #1313

Decorating your room or kennel

Stop and design what objects you will need.

You will need to add a bowl to your kennel for the next exercise. Try these commands, substituting "mybowl" for the name of the object.

@create $container called mybowl

Enter drop mybowl and hit the "Look" button or just type "look" into the telnet window to see it is in the room. When you create each object, it belongs to you. To leave it visible in a room, you must use the drop command.
look mybowl
to see its description.
open mybowl to see inside it as a $container class object
take mybowl will let you pick it up again.
@recycle mybowl
will remove or destroy the mybowl

Now edit its properties using the button to change properties:

Figure 3. The Object editor.

Exercise: Give a dog a bone

Here is a chance to revise your plans. Use the thematic, technical and community plan you are to add a bone to your mybowl object of class $container. Griswald has a bowl called with object number #1508 and parent generic container (#8) made with that command entered into the MOOTcan telnet applet window. Some have added some imagination to include objects inside their kennels such as a box of chocolates, a rug with a fleas verb and in one case the kennel is an Italian restaurant!

Workshop 2: Programming and building your canine world

You are now on your own. All dogs are now off the chain and free to roam and explore. Use the hypertext links below and put what you learn into action, through your thematic plan development which began in workshop 1.

This will allow you to learn-by-doing in your own time and each week for the duration of the workshop series, we will meet to discuss our progress, share our joys and frustrations as well as enjoy learning what each has done. Use the sub-forum to "show and tell". Good luck.

1

2
    2.1

3

4
    4.1
    4.2
    4.3
    4.4
    4.5
    4.6
    4.7
    4.8
       4.8.1
       4.8.2
       4.8.3
    4.9
       4.9.1
       4.9.2
    4.10
    4.11
    4.12
    4.13
       4.13.1
       4.13.2
       4.13.3
    4.14
    4.15
    4.16
    4.17
    4.18

Compiled by Ken Eustace, May 2000-2004 using major works by Pavel Curtis, Jan Holmevik and Cynthia Haynes. The recommended textbook to use with these programming notes is:  Cynthia Haynes, University of Texas at Dallas Jan Rune Holmevik, University of Bergen, Norway. Copyright: 2000 Format: Paper, 170 pp ISBN: 0-205-27114-6

1 How to program in an Object-orientedWorld

If you are new to the ideas expressed in object-oriented design andprogramming in software engineering (or don�t much care), then beware ofinformation technology specialists who suggest that this is a new paradigm.Firstly it has always been an object-oriented world, secondly Grace Hopperwas doing object-oriented programming just after World War 2, only we didn�tcall it that then. This led to the suggestion that in IT we don�t reallydo anything new, we just rename some old stuff. Renaming objects is a commonfeature in object-oriented programming and you can do it in a MOO.

So what does this paradigm suggest?

The object-oriented way of doing things makes the following points:

Big tasks are broken down into to easy-to-manage chunks. This is not new,but is often called top-down design (yes there is also a bottom-upapproach, too). A process called stepwise refinement is used to decomposea task into smaller units. Each unit can be further decomposed into detailedsub-units until a manageable solution to the problem is achieved. Datamodeling using entity-relationship diagram(s) and a data dictionary areuseful. So let us now look at a MOO object modeling case.

 

Figure 4. Client/ server object model

2 Object-oriented design inthe MOO

First we have some terms and concepts to collect and digest.

An OBJECT is usually an abstraction of a real-world entity. Everythingis an object in a MOO. Players, Rooms, Containers, Notes, WhiteBoards,Editors etc. Each object has an OBJECT NUMBER in the MOO database e.g.CrayfishBay is object number #267 at LC_MOO. The object number can be substitutedfor the object name at any time.

A CLASS is a collection of objects having common features. A good exampleis the set of player classes of wizard (administrator), programmer, builder,player and guest. Use @classes to see the public classes available foruse. The player classes represent a skill-based hierarchy that is flatteningout as you can then change yourself by getting your own MOO player objectby @requesting (type help @request) thus upgrading your parent class fromguest to player and so on. For higher levels such as programmer and wizard,these are awarded to experienced players on request to a layer of classwizard. (type '@wizards' for a list of on-line wizards).

MOO objects are grouped together into classes by a mechanism calledInheritance. Inheritance is a relationship between two classes ofobjects, such that one of them, the CHILD, inherits all of the relevantfeatures of the other class called the PARENT. Each class produces a particulartype of object. For example all player objects are descendants of somekind of $player class in a MOO. The $player class has sub-classes thatreveal a MOO character hierarchy, from least to most power and influence.Guest, builder, programmer and wizard are typical parts of the hierarchyof access levels and privileges. Once you get a MOO character yourself,use the @parents me command to list allclasses of your player object back to the root class. Here is mylist of classes for my character GrisWald(#2), at K9MOO:

GrisWald(#2) generic wizard(#57) generic programmer(#58)generic builder(#4) Frand's player class(#90) Generic Mail Receiving Player(#40)generic player(#6) enCore Web Object(#126) enCore Web Class(#138) RootClass(#1)

Each object inherits "layers of characteristics" form its parentclasses (super classes). The object has a new layer added for its own characteristics.This is illustrated by the construction of classes through inheritancein Figure 7.2 on p100 of Holmevik and Haynes.

2.1 A simple object modelingdesign technique

Sample tasks are taken from a MOO object modeling table to help showdevelopment of your ideas, using very simple object modeling techniques.Here is a simple way to model your objects. Use the level 1 and 3 tablesfor designing any object in a MOO.

Level1 � User and system tasks table

Whatthe user doesWhatthe systems does

 
 

 

 

Table 4. Simple OO use case.

Make a list of things done by the user and the system.

Level2 � Abstraction

The next step is called finding the level of abstraction, where MOOobjects build on each other to form classes from the most general and abstract� root class, to the more refined and concrete � what could be more concretethan a bottle of soda! So by the end of this step, you should have a goodidea about which list classes to use or make for your design, but it maybe much shorter than the list revealed by the @parents<object> command in the MOO.

Level3 � Object description table

Use the level3 table below to detail your design with the example used in object-oriented design.

On page 103, it is revealed how Pseudocode describes in generaltrems, what any verb is supposed to do. The Generic Drink (aka drink) withobject number #208 and parent generic thing (#5) is created and then anew object is created called soda with object number #209 and parent GenericDrink (#208). Note that the object numbers in column 1 of the level 2 tableare generated by the system.
 
Object No.
Object name
Class
Description
Verbs
Properties
#208
Generic Drink
$thing
 
Drink
Taste
#209
soda
Generic Drink
You see a bottle of soda
  
      

Table 5. Object description table.

NOTE: as a rule, you should only place your verbs and properties on generic objects like #208, which should NOT be used to interact with � instead create a subclass to use as a test object, as shown by #209.

3 Object-oriented programmingin the MOO � Is it hard?

MOO programming is easy once you get started with a few good resources,take your time and learn a few simple concepts about Object-oriented (OO)programming � a lot easier than learning Java and more fun. People areobjects. Your understanding of OO concepts will be enhanced at using aMOO as you behave as an object in an OO world, something that MOO programminghas over learning Java, Smalltalk and C++. Walker (1999) has the resourcesyou need to get started � the LambdaMOO programmers manual (Curtis, 1997)-usethis as a reference, and Yib�s Pet Rock: a programming primer for beginners(use this as a tutorial). Other resources also exist on the Web.

The MOO database core starts off with basic object types � classes calledGENERIC OBJECTS or GENERICS. Generic objects are intended for use as theparents of new objects. As you create new objects, the class is defined.Typical generic objects are $thing, $player, $room, $note and $container.The $root_class is the ROOT CLASS where all objects begin. If I wantedto create a computer object in the current room using the $note class:

    @create $note named "computer"

If I wanted to create a computer SUBCLASS, the command is:

    @create "computer" named "macintosh"
    @create "computer" named "compaq"

So it is easy to create objects and to make that object can either be an instance of a generic object or a subclass of a generic object, as was the case for "computer" as a sub-class of the generic object called $note . The next step is to attach special methods to an object in order to make an object do something. These methods are called VERBS and this is a second level of the MOO programming process. Most of the tutorials will take you through three main steps:

1 make sure that your character is a programmer or wizard sub class
2 create or find the object(s) in the database.
3 create, program and/or edit the verb(s) and properties for that object.

4 Programming in the MOO

Holmevik and Haynes (2000) provide an excellent step-by-step introductionto the way the MOO makes programming "fun and easy" in Chapter 7. MOO isalso a good preparation for learning object-oriented design and programmingtechniques that are used in C++ and Java. In my own opinion, MOO, VisualBASIC and Python are better for beginners then either C++ or Java. If newprogrammers still find programming "fun and easy" then let them loose withC++, Java or maybe even jPython (See )

4.1 Becoming a MOO Programmer:the First Steps

Let me digress and talk about the Java programming language, in orderto make a brief point. When teaching Java, I always tell my students toknow the language. Start by dissecting its parts and putting them backtogether. So off they go to study the class hierarchy in the various Javapackages before tackling the work at

The java.net package of classes for Java 1.2 is shown below:

java.net Class Hierarchy

class java.lang.Object
class java.net.Authenticator
class java.lang.ClassLoader
class java.security.SecureClassLoader
class java.net.URLClassLoader
class java.net.ContentHandler
class java.net.DatagramPacket
class java.net.DatagramSocket
class java.net.MulticastSocket
class java.net.DatagramSocketImpl(implements java.net.SocketOptions)
class java.net.InetAddress (implementsjava.io.Serializable)
class java.net.PasswordAuthentication
class java.security.Permission (implementsjava.security.Guard, java.io.Serializable)
class java.security.BasicPermission(implements java.io.Serializable)
class java.net.NetPermission
class java.net.SocketPermission (implementsjava.io.Serializable)
class java.net.ServerSocket
class java.net.Socket
class java.net.SocketImpl (implementsjava.net.SocketOptions)
class java.lang.Throwable (implementsjava.io.Serializable)
class java.lang.Exception
class java.io.IOException
class java.net.MalformedURLException
class java.net.ProtocolException
class java.net.SocketException
class java.net.BindException
class java.net.ConnectException
class java.net.NoRouteToHostException
class java.net.UnknownHostException
class java.net.UnknownServiceException
class java.net.URL (implements java.io.Serializable)
class java.net.URLConnection
class java.net.HttpURLConnection
class java.net.JarURLConnection
class java.net.URLDecoder
class java.net.URLEncoder
class java.net.URLStreamHandler

Interface Hierarchy

interface java.net.ContentHandlerFactory
interface java.net.FileNameMap
interface java.net.SocketImplFactory
interface java.net.SocketOptions
interface java.net.URLStreamHandlerFactory

Find the class java.net.Socket in the above package, byfollowing the links at the same Web site. If you look for the class methodto use to find out which port a MOO server is running you may want to usethe method below, from the class java.net.Socket

getLocalPort()

This method when installed, returns an integer value for the local portto which this socket is bound.

Is a Java method the same as a MOO verb?

4.2 Time for a drink break

After that heavy piece of thinking about comparing class hierarchies,your thirst to learn MOO programming continues after a drink.

4.3 Creating your genericobject

The construction of classes by inheritance and the anatomy of the MOO object depict verbs as the object processes and properties as the stored data about the object. Starting with a subclass of $thing:

@create $thing named Generic Drink, drink
@describe soda as "You see a bottle of soda."

The new test object named soda is now a subclass drinkand will inherit the verbs and properties used to define GenericDrink.

The next step in the design process uses pseudocode to describe any verb. The design suggests that the Generic Drink object (super class or parent of the soda object) requires a verb called Drink and a review of the design adds a property called taste.

4.4 Defining verbs andarguments

Verbs are like commands and are formed in two steps:

  1. Define the verb on an object
  2. Program the verb.
When executing the verb, the MOO server needs to know three things:
  1. on which object it is defined � this is called the direct object.
  2. whether a preposition is allowed
  3. whether an indirect object is allowed, in the command syntax.
Here is the general syntax of the @verb command:

@verb object:verb direct_object prepositionindirect_object

Note how the verb argument variables operate. You can now define the verb drink and its arguments on to the Generic Drink object:

@verb "Generic Drink":drink this none none
Verb added (0).

and now you may even have time to teach yourself how to throw a snowballat someone whicl drinking a bottle of soda J

4.5 Defining Properties

The drink verb also needs to define the message property. The five MOO data types that properties and other variables can hold.
 

Data type
Example
Object number#64
Integer42
Floating point (decimal)4.2
String"The time has come, the walrus said"
List (an array that can contain all the datatypes above){"The","cat","sat","on","the","mat"}

Table 6. MOO data types to consider..

The command to create properties on an object has the general format that uses a period to separate object names and property names.

@property object.property_name data_type initvalue

So if the Generic Drink object is to store text messages about how thedrink tastes � like in a property, then the data_type string is used. Seethe example in table 7.3. Now to add the property:

@property "Generic Drink".taste ""
Property added with value "".

Why an empty string or null of "" for the taste property? Each typeof drink, either soda or champagne will have its own taste message, whennew test objects form Generic Drink are created. The table below summariseswhat has been done to this stage.

That now defines the verb on an object, but step 2 of programming the verb is still to do. This will require a closer look at the "building blocks" of the MOO language and its expressions, statements and operators.

Object Created
Object name
Features
New object Generic Drink Super class Generic Thing
Test object Soda Super class Generic Drink
New verb Drink Arguments: this none none
New property Taste Data_type: string

Table 7. Verb programming table to keep track of names and features.

4.6 Permissions on an object,verb or property

If you are familiar with UNIX, you may already know abouit read, writeand execute permission on a file or directory. In a MOO we are concernedabout similar permissions on an object. Here are some useful notes on permissionsfrom

Acknowledgement: This text is a formatted version ofa message posted to *programmers on (now defunct) WorldMOO on Sat Mar 407:50:23 1995 PST by Defender (#160)

  1. Every object, verb, and property has an owner.
  2. Verbs are executed with a 'set of permissions', or a flag indicating whichplayer owns the task. Any verb you write will normally run with your permissions,which means it can affect things you own. Wizards can use a built-in functioncalled set_task_perms(), documented, to change the permissions of a particularverb's execution. This doesn't change the verb ownership, nor does it changepermissions for other verbs. Normally you don't need to worry about that.
  3. A verb with the permissions of the owner of something, or a wizard's permissions,can affect that something (read its value or write to it). For instance,you can create verbs and properties on objects you own, because the creativeverbs run with your permissions.
  4. Everything (objects, verbs, and properties) have a set of possible 'permissionbits', which control access to them. Two of these potential bits are alwaysR and W (for Readable and Writable). For verbs whose permissions aren'tthe owner and aren't wizardly, these flags are important.
  5. If the R bit for something is set, any set of permissions can read it.For example, if a verb you write has its R bit set, anybody can read theverb code. If it is not set, the object/verb/property is said to be "E_PERM'ed",referring to the error code E_PERM (Permission Denied), which will be returnedupon attempts to read the value. Unless you are deliberately needing tohide some information, it is good practice to make things readable by settingthe R bit.
  6. If the W bit for something is set, any set of permissions can write toit. For example, if you have a property whose W bit is set, anybody canset the property value to anything they want, or remove it altogether.The W bit is almost never set, and should NOT be set for verbs in particular.
  7. What reading and writing mean depends on what you are dealing with.
Table below summarizes the READ and WRITE permissions on object, verbsand properties:
 
 
read
write
Objects
You can use verbs() and properties() directlyto read the list of verbs and properties directly.You can define your own verbs and propertieson the object, which will be owned by you.
Verbs
You can use verb_info(), verb_args(), and verb_code()on the verb, and you can @list the code and you can @list the code.You can change the arguments, names, and programcode for the verb in question, using @edit.
Properties
You can see the permissions and value for theproperty.You can change the permissions and value forthe property.

Table 8. verb permission to consider.