0

I am currently working a program that creates Cone objects and adds them to an ArrayList. You are then the user is able to type in certain char's to get certain feedback or abilities to do stuff with the cones. I am currently stuck on a method where you need to be able to edit the radius or height of a cone that is in the array list. You choose the cone by typing in its label. Cone has three parameters (label, height radius). this is what the instructions are for the method -

"Takes three parameters (label, height, and radius), uses the label to find the corresponding the Cone object. If found, sets the height and radius to the values passed in as parameters, and returns true. If not found, simply returns false. "

Can't seem to even begin where to start other than method stub. Any help would be great. Thanks.

4
  • 1
    Can you share some code? It's impossible to help you without some more context. Commented Oct 11, 2016 at 20:34
  • 1
    What have you done already? What are you stuck on? Commented Oct 11, 2016 at 20:35
  • 1
    this sounds like homework help... The description of the method is literally telling you how it's supposed to be implemented.. Commented Oct 11, 2016 at 20:36
  • Welcome to Stack Overflow! Unfortunately, "How do I get started" questions are a bit too broad for Stack Overflow's format. Could you try out a way to get started and then edit it into your question? Commented Oct 11, 2016 at 20:52

2 Answers 2

1

Here is some code that should be able to help point you in the right direction

public boolean editCone(String label, double height, double radius)
{
    for(int index = 0; index < arrayOfCones.size(); index++)
    {
        //if the labels are the same then change the values
        if(arrayOfCones.get(index).getLabel().equals(label))
        {
            arrayOfCones.get(index).setHeight(height);
            arrayOfCones.get(index).setRadius(radius);
            return true;
        }
    }
    //if we get to this point then we haven't found a matching
    //label in our array list of cone objects
    return false;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You may have a look at following example solution:
https://gist.github.com/audacus/3ffa11e184d24869599e61b5d4763dea

// create cone array list
List<Cone> cones = new ArrayList<>();
...
// add cones
cones.add(new Cone("bar", 13.37, 4.2));
...
// method to find and change cones
boolean findAndAdjustCone(String label, double height, double radius) {
    // default is false
    boolean found = false;
    // iterate over all cones
    for (Cone cone : cones) {
        // if cone label equals the given label in the arguments...
        if (cone.label.equals(label)) {
            // set found to true
            found = true;
            // change values of cone
            cone.height = height;
            cone.radius = radius;
            // break for-loop because cone was found
            break;
        }
    }
    // return if cone was found or not
    return found;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.