[Question] Beginner Java question. menu

Shout-Out

User Tag List

Results 1 to 4 of 4
  1. #1
    greenegzofyoshi's Avatar Banned
    Reputation
    10
    Join Date
    Jun 2009
    Posts
    23
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Question] Beginner Java question.

    Hi I'm relatively new to programming and I'd like to ask a question about a homework assignment I was given. I don't really understand the API as of yet but am trying each day to make sense of things.

    My homework assingment is :

    Write a class ChristmasTree whose toString method yields a string depicting a Christmas tree. The Tester class outputs what I'm guessing is the memory location of the ChristmasTree object, but I can't figure out how I am supposed to return a string depicting a christmas tree via the toString method? My teacher does not explain anything(she hardly speaks English, how she got a job as a professor is beyond me). Any help you can offer, or even make sense of the question / point me in the right direction would be cool.

    These are my codes.
    Code:
    public class ChristmasTreeTester {
    	public static void main(String[] args)
    	{
    		ChristmasTree tree = new ChristmasTree();
    		System.out.println(tree.toString());
    	}
    
    }
    Code:
    public class ChristmasTree {
    	private String Tree;
    	public ChristmasTree()
    	{
    		Tree = ("       /\\       \n      /  \\ \n     /    \\ \n    /      \\ \n    -------- \n      \"  \"   \n      \"  \"   \n      \"  \"   ");
    	}
    }

    [Question] Beginner Java question.
  2. #2
    Strupantwn's Avatar Contributor
    Reputation
    115
    Join Date
    Apr 2007
    Posts
    1,045
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not an expert on java, but I believe I am able to help. First you should look up the toString() method on the java API and find out what it does (Object (Java 2 Platform SE v1.4.2) ). Now if you're a beginner, it may look like jibberish to you. Basically, toString() is a method of an object that converts whatever you need to a string. What you want to do, i'm guessing, is to override the method ( meaning that you want to replace what toString() does with what you want it to do, which is to print out a christmas tree).


    Code:
    public class ChristmasTree 
    {    
       private String Tree;     
       public ChristmasTree()    
       {         
           Tree = ("       /\\       \n      /  \\ \n     /    \\ \n    /      \\  \n    -------- \n      \"  \"   \n      \"  \"   \n      \"  \"   ");      
       }
    }
    What you did was to construct a ChristmasTree object with properties "Tree" meaning that ChristmasTree object will be able to print out a christmas tree if you so desire. Frankly I would just move everything to the toString() instead.

    Code:
    class ChristmasTree 
    {
        private String Tree;
        
        public String toString()
        {
            return Tree = ("       /\\       \n      /  \\ \n     /    \\ \n    /      \\ \n    -------- \n      \"  \"   \n      \"  \"   \n      \"  \"   ");
        }
    }
    But if your professor requires to go have a constructor, then this works as well:

    Code:
    class ChristmasTree 
    {
        private String Tree;
        
        public ChristmasTree()
        {
            Tree = ("       /\\       \n      /  \\ \n     /    \\ \n    /      \\ \n    -------- \n      \"  \"   \n      \"  \"   \n      \"  \"   ");
        }
        
        public String toString()
        {
           return Tree;
        }
    }
    Last edited by Strupantwn; 10-10-2011 at 03:13 PM.

  3. #3
    Phygar's Avatar ( ͡° ͜ʖ ͡°)
    Reputation
    444
    Join Date
    Nov 2007
    Posts
    1,591
    Thanks G/R
    7/5
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ^ what he said.
    All objects have a toString() method that will return some string representation of itself. By default, what it gives you is not very useful, but this method can be overridden, and you can create your own version of it. Simply create a method named toString() that returns a string representation of the Christmas tree.

  4. #4
    greenegzofyoshi's Avatar Banned
    Reputation
    10
    Join Date
    Jun 2009
    Posts
    23
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you both for the help, I appreciate your inputs and have given you reputation (;

Similar Threads

  1. [Question][Java] Setting terms for a walk function
    By kmJeepers in forum WoW Memory Editing
    Replies: 13
    Last Post: 10-15-2009, 01:37 PM
  2. Two beginner questions (Preferred core, and modifying instances)
    By swils in forum WoW EMU Questions & Requests
    Replies: 10
    Last Post: 07-17-2009, 07:46 AM
  3. Script making questions from a beginner
    By zerby in forum WoW EMU Questions & Requests
    Replies: 4
    Last Post: 07-01-2009, 04:02 AM
  4. Lua script help and general questions from beginner
    By WinKIller0 in forum World of Warcraft Emulator Servers
    Replies: 1
    Last Post: 02-23-2008, 04:38 AM
  5. [Question] Beginner looking for clarification
    By dmortuu in forum WoW ME Questions and Requests
    Replies: 2
    Last Post: 01-24-2008, 08:46 PM
All times are GMT -5. The time now is 12:43 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search