Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: Calling a static method in the main() method
    primarykey
    data
    text
    <p>I am supposed to do the following:</p> <blockquote> <p>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.</p> <p>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.</p> </blockquote> <p>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.):</p> <pre><code>public static Employee[] generateEmployees() { Employee[] employees = new Employee[10]; int randomNum = 0; for (int i = 0; i &lt; 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; } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. CONot 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 <Classname>.<static_method_name> 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).
      singulars
    2. COI 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()); } }`
      singulars
    3. COThe 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.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload