0

For this program, what i have written so far is in the code below. I am stuck. Any hints/help would be greatly appreciated.

Given the UML Class Diagram and the TestApp class, produce a working system that meets the following requirements:

Implement a Department class with toString and equals overrides. Two department objects are equal if both the name and building numbers match.

Create an abstract Employee class with toString and equals overrides. Two abstract employees are considered to be the same if they are in the same department.

Implement a Consultant class that has an hourlySalary. Two consultants are equal if they have the same rate (in pennies).

Implement a SalariedEmployee class. Salaried employees are equal if they are in the same department and have the same salary (in pennies).

Follow the TODO instructions in the TestApp class to read the departments from the input txt file and test the system.

Ouput should be:

Executive 100
Research 112
Engineering 115
Production 120
Accounting 122
Executive 100(100) Steve Jobs $1000000.00
Research 112(200) Bill Joy $300000.00 
Engineering 115(300) James Gosling $300000.00 
Production 120(1100) Craig McClanahan $250.00    
Accounting 122(400) Kevin Malone $51567.00  
Research 112

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class TestApp {

public static void main(String[] args) {
    ArrayList<Department> depts = 
        readDepartmentsFromFile("depts.txt");
    displayDepartments(depts);

    ArrayList<Employee> company = new ArrayList<>();
    populateEmployeeArray(company, depts);

    // TODO: Display everyone's pay w/o referring to the 
    // subclasses (i.e., using Employee only)

    // TODO: Display all departments that are equal to this one:
    Department d1 = new Department("Research", 112);

    // TODO: Display all departments that are equal to this one:
    Department d2 = new Department("Accounting", 50);

}

private static void displayDepartments(ArrayList<Department> list) {
    // TODO: Display the list to std error (NOT standard output)
}

private static ArrayList<Department> readDepartmentsFromFile(String filename) {
    ArrayList<Department> list = new ArrayList<>();
    // TODO: Use a Scanner to read the input file depts.txt
    // and create one Department object per line in the file.
    // Add all objects to the list.
    // Be sure to close the scanner object using a finally block.
    // Handle possible execptions by producing a nice error 
    // along with the execption's message to standard error 
    // (NOT standard output).
    return list;
}

public static void populateEmployeeArray(ArrayList<Employee> array, 
        ArrayList<Department> departments) {
    SalariedEmployee exec = new SalariedEmployee(100, "Steve", 
            "Jobs", departments.get(0), 100000000);
    SalariedEmployee architect = new SalariedEmployee(200, "Bill", 
            "Joy", departments.get(1), 30000000);
    SalariedEmployee engineer = new SalariedEmployee(300, "James", 
            "Gosling", departments.get(2), 30000000);
    Consultant consultant = new Consultant(1100, "Craig", 
            "McClanahan", departments.get(3), 25000);
    SalariedEmployee accountant = new SalariedEmployee(400, 
            "Kevin", "Malone", departments.get(4), 5156700);

    array.add(exec);
    array.add(architect);
    array.add(engineer);
    array.add(consultant);
    array.add(accountant);
}

}

* Updated TestApp

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class TestApp {

public static void main(String[] args) {
    ArrayList<Department> depts = 
        readDepartmentsFromFile("depts.txt");
    displayDepartments(depts);

    ArrayList<Employee> company = new ArrayList<>();
    populateEmployeeArray(company, depts);

    // TODO: Display everyone's pay w/o referring to the 
    // subclasses (i.e., using Employee only)
    for (Employee employee : company){
        System.out.println(employee);
    }

    // TODO: Display all departments that are equal to this one:
    Department d1 = new Department("Research", 112);
    for (Department department : depts)
        if(department.equals(d1)){
            System.out.println(department);
        }

    // TODO: Display all departments that are equal to this one:
    Department d2 = new Department("Accounting", 50);
    for(Department department : depts){
        if(department.equals(d2)){
            System.out.println(department);
        }
    }

}

private static void displayDepartments(ArrayList<Department> list) {
    // TODO: Display the list to std error (NOT standard output)
    System.err.println(department.toString());
}

private static ArrayList<Department> readDepartmentsFromFile(String filename) {
    ArrayList<Department> list = new ArrayList<>();
    // TODO: Use a Scanner to read the input file depts.txt
    // and create one Department object per line in the file.
    // Add all objects to the list.
    // Be sure to close the scanner object using a finally block.
    // Handle possible execptions by producing a nice error 
    // along with the execption's message to standard error 
    // (NOT standard output).
    File aFile = new File(filename);
    System.out.println(aFile.exists());
    Scanner sc = null;
    if (aFile.exists())
        try{
            sc = new Scanner(aFile);
            while(sc.hasNext()) {
                String deptName = sc.next();
                int buildNum = sc.nextInt();
                Department mallory = new Department(deptName, buildNum);
                list.add(mallory);

                }
            }
    catch(FileNotFoundException exec) {
        System.out.println(exec.getMessage());
    }
    finally {
        sc.close();
    }
    return list;
}

public static void populateEmployeeArray(ArrayList<Employee> array, 
        ArrayList<Department> departments) {
    SalariedEmployee exec = new SalariedEmployee(100, "Steve", 
            "Jobs", departments.get(0), 100000000);
    SalariedEmployee architect = new SalariedEmployee(200, "Bill", 
            "Joy", departments.get(1), 30000000);
    SalariedEmployee engineer = new SalariedEmployee(300, "James", 
            "Gosling", departments.get(2), 30000000);
    Consultant consultant = new Consultant(1100, "Craig", 
            "McClanahan", departments.get(3), 25000);
    SalariedEmployee accountant = new SalariedEmployee(400, 
            "Kevin", "Malone", departments.get(4), 5156700);

    array.add(exec);
    array.add(architect);
    array.add(engineer);
    array.add(consultant);
    array.add(accountant);
}

}

TEXT FILE - 
depts.txt - 
Executive 100
Research 112
Engineering 115
Production 120
Accounting 122
5
  • 1
    What's your question? Commented Sep 27, 2014 at 1:54
  • I am getting an error. I am not sure how to fix it. Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:635) at java.util.ArrayList.get(ArrayList.java:411) at TestApp.populateEmployeeArray(TestApp.java:46) at TestApp.main(TestApp.java:14) Commented Sep 27, 2014 at 1:56
  • Which line is TestApp.java:46? line 46? You're trying to use a size 0 array there. Commented Sep 27, 2014 at 1:57
  • 2
    What error are you getting? Posting more info about the problem would help other users help you. I could copy all of the code, go compile it and find out what's going on, but that isn't the point of Stack Overflow. I would suggest editing your question and only posting enough code to adequately show the problem area. Commented Sep 27, 2014 at 1:58
  • Any time you get an array out of bounds exception, you are going beyond the end of the array bounds. So make sure your indexing is done correctly and you are not trying to access a part of the array past the size of the array. Commented Sep 27, 2014 at 1:59

1 Answer 1

2

You're calling get(0) and get(1) on departments before putting anything into the ArrayList. Solution: don't do this! Fill the ArrayList and don't call get(...) ever on it before knowing that something exists at that index.

populateEmployeeArray(company, depts);  // passed into method

And here's how you fill the depts ArrayList:

// you might need a bit more code in here!!!!  The // TODO: means do it :)
private static ArrayList<Department> readDepartmentsFromFile(String filename) {
  ArrayList<Department> list = new ArrayList<>();
  // TODO: Use a Scanner to read the input file depts.txt
  // and create one Department object per line in the file.
  // Add all objects to the list.
  // Be sure to close the scanner object using a finally block.
  // Handle possible execptions by producing a nice error
  // along with the execption's message to standard error
  // (NOT standard output).
  return list;
}

As you can see, this code does nothing but returns an empty ArrayList, and so it shows that you're going about things a bit backwards. You will instead want to fix this part of your program, get it to read in the text file first, and then fix the displayDepartments(...) method so you can test that the readDepartmentsFromFile works before trying to use the depts ArrayList!

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

4 Comments

I would add to this that proper exception handling would help make the error easier to discover and properly deal with, but this seems like a homework project and chances are that exception handling may not have been covered yet.
@JamesH: except that you'll almost never want to catch an ArrayIndexOutOfBoundsException since it's an unchecked RuntimeException, and if it gets thrown, this indicates something is seriously wrong with your program that exception handling won't fix (as is the case in the code posted above).
Very true, was meant more as a general comment on pointing to the error if you were dealing with arrays in multiple areas of code but yes this is very true. It could help in finding the problem in complex code but really when dealing with arrays or arraylists, good design practice up front will eliminate any ArrayIndexOutOfBoundsException issues.
@malpal, looks like you're asking a completely different question, and perhaps this is what you should do -- post a new question.

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.