|
Java Arrays, Objects, Methods
|
CS 161 - Java |
- Chapter 6 - Inheritance - through the section "Constructors in
Derived Classes" that ends on page 301 (you may skip the remainder of
Chapter 6)
- Chapter 9 - Streams and File I/O - Up to but not including the
Programming Example that starts at the bottom of page 495.
-
Write a second Java class and use the two classes
written so far, this week's uses last's.
-
Learn about instances of classes and storing them in an array.
on them.
- To start building solutions from smaller pieces: Java classes;
to get those pieces to interact.
For this assignment you will write a Java class named Boss.
This class will make use of the Employee class created last week. If
you have not completed that assignment yet, you may use the minimal
version on the next page to do this week's assignment.
- Your Boss class should have the following instance
variables:
- a bossName
- an array named employees in which to store Employee objects
- an int field named count to store the number of Employees who
the Boss has.
-
Write methods with the following signatures
- a main method to start things off with the signature:
public static void main (String [] args)
see below for what main should do.
- a public constructor for the class that will insure that Boss
objects always have a name field with the signature.
public Boss( String name )
This constructor should also instantiate an array to store Employee
objects (at least 60) and initialize the count of employee's to 0. It
should not initialize the array contents; just create the array.
- a method to add an Employee object to the array of Employees with
the signature
public void hire( Employee emp )
This method should increase the count of Employees
associated with the Boss as each Employee is added.
- a method that will cause the Boss to hire a group of people. This
method should have the signature
public void staffUp()
This method should create a number of Employee objects (at least 5)
and then use the hire method to add them to the boss. You may
use any names and id values you choose. One way to initialize a
group of names to use would be to get them from an array that is
initialized like:
String [] names = {"John", "Paul", "George", "Ringo", "Fred" };
You may use these names or any of your choosing and any technigue of
your choosing to give each Employee a different name as an input
parameter for your Employee constructor. Use any id values you choose.
- a method to print out the Boss name followed by the names
of the Employees who are managed by a Boss in an order different
than they were added. This method should have the signature
public void list()
-
The main method for this class should:
- create a single instance of the Boss class,
- call the staffUp method on the Boss object
- call the list method on the Boss object
Do only what is asked above. Do not add any extra code to prompt
for input or otherwise add capabilities not asked for. Be sure to
create methods with the signatures requested. Output similar to the
following is all the Boss class needs to produce:
Boss Bruce:
Fred
Ringo
George
Paul
John
Hint: To start, create the outline of your class definition by defining
empty methods with the signatures listed above. Compile it to be sure
the outline is in the right form. Then go back and start filling in
the details of each method. Compile often so you can correct errors
easily.