Jump to content

I keep posting in here for help, but I'm tryin to undertan this, and learn from what you guys/girls have to offer

 

You're really helpful, and thanks for all the help thus far.

 

Here's my question, I've got this class given to me by my professor and he want me to

create a mehod that returns a single string that contains all the trees that can be found in the array list. initialize a local

variable of the class string. Traverse the entire aray list of tres and or each one o them invoke the ethod toString() rom the class Tree.

append the value returned by invoking the method to String to the String result, adding a "\n" at the end.

Once the entire array list has been traversed return the String result.

/** * * * */import java.util.ArrayList;//imported the java utility containing//methods called add, get, length//gives us the ability to create array lists without//creating a ton of Strings, methods of those strings//then the overcomplication of having values associated with those stringspublic class TreeDB{// instance variables - replace the example below with your own//constructor array list//created a private arraylist <type tree>//the array's name    private ArrayList < Tree > db;    /**     * Constructor for objects of class TreeDB     */    public TreeDB()    {        //new array list invoked they called it "db"        // new array list contains a string name, the low value for height        //and the        db = new ArrayList < Tree > ();        Tree t1 = new Tree("Pin Oak", 59, 72);        Tree t2 = new Tree("White Pine",50,80);        Tree t3 = new Tree("Silver Maple",50,80);        Tree t4 = new Tree("Tulip Tree",70,90);        Tree t5 = new Tree("River Birch",40,70);        db.add(t1);        db.add(t2);        db.add(t3);        db.add(t4);        db.add(t5);    }    /***********************************     * to String method is supposed to return the     * name of the tree as a string     ********************************/    public String toString(){        String result = "";        //we need somewhere to put our result as a string        //for statement must have compatible types o the array        //he array I'm tryin to get the names of is db        //we must invoke the method toString onto db        for(String Tree : toString(db) ){              System.out.println("the name of the trees are: " + tree);        }                result = this_tree;        return result;    }
Link to comment
https://linustechtips.com/topic/229784-trees-trees-everywhere/
Share on other sites

Link to post
Share on other sites

From my understanding of your explanation, the toString() method of the Tree class will return the name. So you just use a loop (like for or while) to traverse the list and concatenate the values returned from toString() together. If you need to add in a separator like a comma you can do that too.

Link to comment
https://linustechtips.com/topic/229784-trees-trees-everywhere/#findComment-3144979
Share on other sites

Link to post
Share on other sites

        @­Override    public String toString() {        StringBuilder stringBuilder = new StringBuilder();        for (Tree tree : db) {            stringBuilder.append(tree.toString());            stringBuilder.append(System.lineSeparator());        }                return stringBuilder.toString();    }

Ugh it keeps adding a link to this guy so there is an invisable char between @ and Override

Link to comment
https://linustechtips.com/topic/229784-trees-trees-everywhere/#findComment-3145572
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×