4

I am supposed to do the following:

Write a Java application (Client) program with a static method called generateEmployees( ) that returns a random list of 10 different types of Employee objects. You could either use an array or an ArrayList to store the employee objects that will be returned. Use a for loop to populate randomly different types of employee objects with some random data. You could possibly think a range of values like 1 – 4. If random value is 1, create a HourlyEmployee object with some randomly generated data, if 2, SalariedEmployee object with some random data and so on. I would leave it to your ingenuity to generate and populate these different Employee objects. As these objects are generated add them to your data structure (array or ArrayList that you are using). Finally the method returns this data structure.

In the same application class, implement the main( ) method. Call the generateEmployees( ) static method and using a for loop print the details of each of the employee along with their earnings on the terminal window.

My generateEmployees() static method is as follows (it might not be correct... also, the data hasn't been randomly generated because I'm not exactly certain how to do that, at least as far as the first and last names are concerned.):

public static Employee[] generateEmployees()
{
    Employee[] employees = new Employee[10];
    int randomNum = 0;
    
    for (int i = 0; i < 10; i++)
    {
        Random random = new Random();
        randomNum = random.nextInt(4) + 1;
         
         switch (randomNum)
         {
            case 0:
                employees[i] = new SalariedEmployee("Bri", "Gefroh", 123, 1000);
                break;
            case 1:
                employees[i] = new HourlyEmployee("Bri", "Gefroh", 123, 12.50, 10);
                break;
            case 2:
                employees[i] = new CommissionEmployee("Bri", "Gefroh", 123, 10000, 0.05);
                break;
            case 3:
                employees[i] = new BasePlusCommissionEmployee("Bri", "Gefroh", 123, 10000, 0.05, 2500);
                break;
         }
    }
    
    return employees;
}

How would I call this method and use it in the main() method? Each of those four types of employees are subclasses of the Employee class, and each subclass has its own toString() method, which is what I belivee I'm supposed to be outputting.

Community
  • 1
  • 1
Brittany Gefroh
  • 81
  • 2
  • 3
  • 7
  • Not sure exactly what you are asking here but static methods are associated with the class only as far as are in the namespace of the class. You can call a static method of one class from another using . so in your case you could call `Employee.generateEmployees()` if your main method is not in the Employee class if it is in the Employee class you can just call `generateEmployees()`. There is a third option of adding static method import statements see [documentation](http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html). – aakarsh Apr 27 '13 at 02:41
  • I guess maybe my real question is what's wrong with the way I tried to implement the generateEmployees() method in my main() method. The way I called the generateEmployees() method was pretty much what the answers here are saying, but my code throws a NullPointerException. `public static void main(String [] args) { Employee[] employees = new Employee[10]; employees = generateEmployees(); for (int i = 0; i < 10; i++) { System.out.println(employees[i].toString()); } }` – Brittany Gefroh Apr 27 '13 at 02:59
  • The issue is that you are using `random.nextInt(4)` which gives you values [0,3) 0-inclusive 4 exclusive which falls under your 4 cases but then you add 1 to it giving you a case unaccounted for as `case 4:` . One quick test would be handle the default case by throwing a `RuntimeException` or putting an `assert` or print statement there to make sure only the cases you expect occur. Also you could add a test for `employee[i]!=null` while generating the output. – aakarsh Apr 27 '13 at 08:34
  • Oops, sorry it seems your question has already been answered – aakarsh Apr 27 '13 at 08:36
  • @BrittanyGefroh Hi, If you think that your question was answered, mark the answer as accepted and/or upvote it. This way you will increase your accepted rate, and user will feel more willing to help you in future questions. – dreamcrash Apr 29 '13 at 04:52

4 Answers4

5

A static method is a class method, rather than an instance method. It's called on the class, not an instance of the class. The difference being that you can call a static method without having an instance first.

Employee.doSomething();

vs

Employee employee = new Employee();
employee.doSomethingElse();

So, if your generateEmployees() method is in the same class as your main, all you need is

 generateEmployees();

otherwise you'll need to do

 Employee.generateEmployees();

(if the Employee class contains generateEmployees()

Scott Woodward
  • 335
  • 1
  • 3
  • 13
  • As far as your NPE, the random.nextInt(4) + 1; returns a value between 1 and 4. nextInt should return between 0 Inclusive and 4 exclusive, which means 0-3. Adding one brings it to 1-4. Since you don't have a case for '4' specified, you're leaving certain spots in your array set to null. – Scott Woodward Apr 27 '13 at 03:26
0

If the method is in the same class, you should just be able to call it like any other method:

public static void main(String[] args)
{
    Employee[] employees = generateEmployees();

    // TODO: loop through and print out...
}

Since main and generateEmployees are both static, it should work. (If generateEmployees is non-static, you'd need to create an instance of the class first).

I'd suggest having a constant array of Strings with "names" in it, and use a random number to generate an index. That should help with randomising the names a little.

Ash
  • 9,296
  • 2
  • 24
  • 32
  • Okay, that's what I was thinking, but then I must be doing something else wrong. That line with System.out.println() throws a NullPointerException. `public static void main(String [] args) { Employee[] employees = new Employee[10]; employees = generateEmployees(); for (int i = 0; i < 10; i++) { System.out.println(employees[i].toString()); } }` – Brittany Gefroh Apr 27 '13 at 02:49
  • Is it the println itself, or inside the toString() that the NPE occurs? Are you sure that `random.nextInt(4) + 1;` always gives you a value between 0 and 3? – Ash Apr 27 '13 at 03:13
  • Ah, that's the problem. I generated random numbers 1-4 but put the cases as 0-3. It works now. – Brittany Gefroh Apr 27 '13 at 03:27
0

It's a static method, so ... it does not need to be accessed within the context of an instantiated object. You can just, you know, call it from your public static void main(...) method. If the class that contains your main() method is named Employee, then...

Employee.generateEmployees(); 

would do the trick.

scottb
  • 9,908
  • 3
  • 40
  • 56
0

Like Ash stated but if you need to process the records, here is no reason to introduce extra variable just do

 public static void main(String[] args)
 {
      for(Employee employee: generateEmployees())
         print(employee); // define static print somewhere too

 }
Singagirl
  • 465
  • 1
  • 3
  • 11