1

for my exams in Java in a couple of weeks i go trough any old exams i get my fingers on. But there is one task i can't get my head arround. I need a solution for this.

You should creat a abstract Class called Plant(just with Attributes for height and weight). There are three Sub Classes (Tomato, Bellpeper and Onions)

Then you need a Class called Greenhouse, in this Greenhouse is room for Excactly 30 Plants in total. It dosent matter what kind of Plants.

If you want to store more then 30 Plants then it should drop a Exception.

i know i need:

  • a Final attribute for the 30 plants.
  • Array.Add to get the Objects in my Array.
  • and a Method that calculated 30 - the length of my Array List.

but i dont know how to use this. There a lot of Tutorials for stuff like this in the same class but not from another class.

I hope someone can help me.

Greetings from Germany

2
  • Can you post the code you have attempted? There many different ways to approach this problem, and it more beneficial to fix your solution rather than create the entire solution for you. It is not very clear what your question exactly is either. Commented Jan 16, 2020 at 16:21
  • Greeting from India -> please share your code here whatever you have tried Commented Jan 16, 2020 at 16:27

1 Answer 1

1

You just need a method in your Greenhouse class. for example

public void addPlant(Plant p) throws Exception //Placeholder exception

Where you take the Plant as an argument, check if you have room in the Greenhouse arraylist, and either add it, or throw the exception. It doesn't matter which class creates the Plant, it will be passed to the Greenhouse class via this method.

Sign up to request clarification or add additional context in comments.

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.