RU beehive logo ITEC dept promo banner
ITEC 120
2019fall
asbrennem
ibarland

arrays of objects
RobotHQs have members!

We will add to our RobotHQ from a previous lab (including its Robot.java and Song3.java), adding one more field, members, which is an array-of-Robots.
The president and secretary are not (necessarily) regular members.

  1. Add the field, and modify your constructor and two-example-HQs appropriately.
  2. Write a method newRobotHQ which takes in our original fields (address, president, secretary, anthem) plus an int (number of regular members to create) plus one Robot, and creates a RobotHQ with that many members, (each a reference to the provided Robot).

    That is, newRobotHQ( "800 E Main St", optimus, r2d2, 7, bumblebee ) (where optimus, r2d2, bumblebee are all variables holding a previously-created Robot) would return:
    a RobotHQ whose members array has 7 elements (all references to bumblebee3).

    Of course, you should call your new constructor from above.

  3. Write a method RobotHQ#size() which returns how many robots are in this RobotHQ (including the president & secretary).
  4. Write a method RobotHQ#countEvils which returns how many evil robots are in this RobotHQ (including the president & secretary).
  5. Write a method RobotHQ#anySpies which returns whether or not there are both evil and non-evil Robots in the same RobotHQ (including the president & secretary).
    Hint: By being clever, we can do this in one line, calling the above methods.

  6. Challenge

    The following are things we'll talk about more in future lecture/labs.

  7. Write a Robot constructor which takes in a java.util.Scanner and reads all the felds from there (using "yes"). It will not print out any prompts, though!
  8. Finally, create a new RobotHQ Which takes the address, president, secretary, anthem as before, plus: a number and a scanner — and it creates that many Robots from the Scanner as its members.

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.