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 6 of 6

Thread: HashMap to Call a Method

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

    Post HashMap to Call a Method

    Hi Guys, I'm new to this forum , and Really need some help

    I'm working under BlueJ as this is how the university is teaching us

    I have a program that when you type "Circle" to the Console , the Circle will appear (See Below)
    1.jpg

    This is done because I have a method that makes the circle visible and a execute to call it

    2.jpg

    Now I want to be able to use a HashMap (as required for the task) and be able to add more Options such as "move.Left" or "move.Right" when I type Circle move left or Circle move right into the console
    I tried so many things!
    Really need advice.

    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HashMap to Call a Method

    What code do you have now and what questions do you have about how to change the code to do what you want it to do?
    Be sure to wrap posted code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HashMap to Call a Method

     <
    /**
     * A class that allows the user to program with shapes.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class ShapeProgrammer
    {
        // Obtain commands from the user.
        private InputReader reader;
     
        /**
         * Constructor for objects of class ShapeProgrammer
         */
        public ShapeProgrammer()
        {
            reader = new InputReader();
        }
     
        /**
         * Allow the user to program with shapes interactively.
         * Repeatedly request commands until the user types "quit".
         */
        public void program()
        {
            printWelcome();
            // Keep reading commands until the user types quit.
            boolean programming = true;
     
            while(programming) {
                String[] commands = reader.getInput();
                if(commands.length > 0) {
                    // Handle program exit here.
                    if(commands[0].equals("quit")) {
                       programming = false;
                    }
                    else {
                        execute(commands);
                    }
                }
            }
     
            printGoodbye();
        }
     
        /**
         * Print a welcome message.
         */
        private void printWelcome()
        {
            System.out.println("Welcome to the shape programmer.");
            printHelp();
        }
     
        /**
         * Print a goodbye message.
         */
        private void printGoodbye()
        {
            System.out.println("Goodbye");
        }
     
        /**
         * Print the help.
         */
        private void printHelp()
        {
            System.out.println("Type commands like: ");
            System.out.println("    circle");
            System.out.println("to create a circle.");
            System.out.println("Type: ");
            System.out.println("    quit");
            System.out.println("to finish.");
        }
     
        /**
         * Execute the command records in commands.
         * @commands The individual words of the command.
         *           commands.length > 0
         */
        private void execute(String[] commands)
        {
            String basicCommand = commands[0];
            if(basicCommand.equals("circle")) {
                makeACircle(commands);
            }
            else if(basicCommand.equals("help")) {
                printHelp();
     
            }
            else {
                System.out.println("Unknown command: " + basicCommand);
            }
        }
     
        /**
         * Create a Circle and make it visible.
         * @param commands The current command words.
         *                 Not currently used.
         */
        private void makeACircle(String[] commands)
        {
            Circle c = new Circle();
            c.makeVisible();
        }
     
     
     
    }
    >

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HashMap to Call a Method

    what questions do you have about how to change the code to do what you want it to do?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HashMap to Call a Method

    Well, The question says, Improve the program so You have more option such as move the circle left or right, when typing into the terminal window.
    at the moment I can just make the circle appear when typing the word "circle"

    --- Update ---

    They Also advice you to use a HashMap

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HashMap to Call a Method

    Describe the steps the changed program needs to take to solve the problem.
    What problems are you having coding those steps?

    What does the program do with what the user types in? What more does it need to do?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How can I call doGet method?
    By Nuha_IT in forum Java Servlet
    Replies: 4
    Last Post: November 27th, 2012, 07:27 AM
  2. Only if one method is true does it call the other?
    By ColeTrain in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2012, 03:28 PM
  3. How do I call a method from the main method?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 19th, 2011, 08:37 PM
  4. call super method outside of 'this'
    By questionmark in forum Object Oriented Programming
    Replies: 1
    Last Post: January 29th, 2011, 11:29 AM
  5. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM