Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Peice of code confusing

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Peice of code confusing

    Hi,

    I am studying this peice of code used in bluej, it is part of the world of zuul game example but there is this part of it that I want to know what it does. what does the public static etc do, what does the game g = new game do and what does g.play do
    public static void main(String[] args)
    game g = newGame();
    g.play()



    public class Game 
    {
        private Parser parser;
        private Room currentRoom;
     
        //
        public static void main(String[] args)
        {   
            //
            Game g = new Game();
            //
            g.play();
     
        }
     
        /**
         * Create the game and initialise its internal map.
         */
        public Game() 
        {
            createRooms();
            parser = new Parser();
        }
     
        /**
         * Create all the rooms and link their exits together.
         */
        private void createRooms()
        {
            Room outside, theater, pub, lab, office;
     
            // create the rooms
            outside = new Room("outside the main entrance of the university");
            theater = new Room("in a lecture theater");
            pub = new Room("in the campus pub");
            lab = new Room("in a computing lab");
            office = new Room("in the computing admin office");
     
            // initialise room exits
            outside.setExit("east", theater);
            outside.setExit("south", lab);
            outside.setExit("west", pub);
     
            theater.setExit("west", outside);
     
            pub.setExit("east", outside);
     
            lab.setExit("north", outside);
            lab.setExit("east", office);
     
            office.setExit("west", lab);
     
            currentRoom = outside;  // start game outside
        }
     
        /**
         *  Main play routine.  Loops until end of play.
         */
        public void play() 
        {            
            printWelcome();
     
            // Enter the main command loop.  Here we repeatedly read commands and
            // execute them until the game is over.
     
            boolean finished = false;
            while (! finished) {
                Command command = parser.getCommand();
                finished = processCommand(command);
            }
            System.out.println("Thank you for playing.  Good bye.");
        }
     
        /**
         * Print out the opening message for the player.
         */
        private void printWelcome()
        {
            System.out.println();
            System.out.println("Welcome to the World of Zuul!");
            System.out.println("World of Zuul is a new, incredibly boring adventure game.");
            System.out.println("Type 'help' if you need help.");
            System.out.println();
            System.out.println(currentRoom.getLongDescription());
        }
     
        /**
         * Given a command, process (that is: execute) the command.
         * @param command The command to be processed.
         * @return true If the command ends the game, false otherwise.
         */
        private boolean processCommand(Command command) 
        {
            boolean wantToQuit = false;
     
            if(command.isUnknown()) {
                System.out.println("I don't know what you mean...");
                return false;
            }
     
            String commandWord = command.getCommandWord();
            if (commandWord.equals("help")) {
                printHelp();
            }
            else if (commandWord.equals("go")) {
                goRoom(command);
            }
            else if (commandWord.equals("quit")) {
                wantToQuit = quit(command);
            }
            // else command not recognised.
            return wantToQuit;
        }
     
        // implementations of user commands:
     
        /**
         * Print out some help information.
         * Here we print some stupid, cryptic message and a list of the 
         * command words.
         */
        private void printHelp() 
        {
            System.out.println("You are lost. You are alone. You wander");
            System.out.println("around at the university.");
            System.out.println();
            System.out.println("Your command words are:");
            parser.showCommands();
        }
     
        /** 
         * Try to in to one direction. If there is an exit, enter the new
         * room, otherwise print an error message.
         */
        private void goRoom(Command command) 
        {
            if(!command.hasSecondWord()) {
                // if there is no second word, we don't know where to go...
                System.out.println("Go where?");
                return;
            }
     
            String direction = command.getSecondWord();
     
            // Try to leave current room.
            Room nextRoom = currentRoom.getExit(direction);
     
            if (nextRoom == null) {
                System.out.println("There is no door!");
            }
            else {
                currentRoom = nextRoom;
                System.out.println(currentRoom.getLongDescription());
            }
        }
     
        /** 
         * "Quit" was entered. Check the rest of the command to see
         * whether we really quit the game.
         * @return true, if this command quits the game, false otherwise.
         */
        private boolean quit(Command command) 
        {
            if(command.hasSecondWord()) {
                System.out.println("Quit what?");
                return false;
            }
            else {
                return true;  // signal that we want to quit
            }
        }
    }


  2. #2
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Peice of code confusing

    If you want to learn Java programming, you should start from the basics until you can read the code and understand what it does. This code is something you should wait for until you understand some OOP. I'll answer your questions as best as possible, although you may not understand the answers or terminology.

    public static void main (String[] args) is commonly called the main method and the simplest way to think about it does is that any class containing it will be executable. That is, if a class doesn't contain it, then it cannot, "run". This is one, if not the first thing you learn about in Java programming.

    Game g = new Game(); is instantiating an object named g pertaining to the class Game. It has to be created in this class due to the visibility of the methods (i.e. non-static). At the same time, it executes the code within the no-argument constructor.

    g.play() is calling the method named play().

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Peice of code confusing

    Your question is similar to someone who doesn't know French, picking up a French book and asking someone what each word means. You don't learn French that way, and you don't learn Programming the way you're doing it. You're questions are basically answered in most all intro tutorials or first chapters of text books, and that's what I recommend that you do: Go to the Java tutorials or a decent Java text book and start at the beginning, learning the rudiments of Java first.

    --- Update ---

    Edit: On looking at your prior posts, it appears that you've been told all of this before. Please understand that learning to program is like learning math. You can't progress unless you learn the basics, and this will require time and effort. It looks like it is past time for you to get started. If you can't do this, then maybe it's time to drop the course.

Similar Threads

  1. Confusing LWJGL Error
    By vividMario52 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 14th, 2013, 12:20 PM
  2. a very confusing exception
    By deathlypest in forum Java Theory & Questions
    Replies: 29
    Last Post: June 11th, 2012, 12:17 PM
  3. What is Abstraction?The Confusing Concept Simplified
    By rainbow9 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 14th, 2011, 11:43 PM
  4. Button help, confusing error.
    By camboy8 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 17th, 2010, 10:25 PM
  5. While (logical confusing output)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: December 20th, 2009, 01:17 AM