0

I have 3 classes: Company, Department, and Employee. The Company is made of Departments, and the Departments are made up of Employees. Pretty basic. But when I try to set each employee's Department, I get the following errors:

  • Syntax error on token "countingGuru", VariableDeclaratorId expected after this token
  • Syntax error on token(s), misplaced construct(s)

I have googled the errors, but I'm still having trouble figuring out what I did wrong.

Here is the code:

public class Company {
    static String[] validDeptNames = {
        "Accounting", "Human Resources", "Information Systems", "Marketing"
    };

    static Department accounting = new Department("Accounting");
    static Department marketing = new Department("Marketing");
    static Department infoSys = new Department("Information Systems");
    static Department humanRes = new Department("Human Resources");

    public static void main(String[] args) {

    }  
}

import java.util.ArrayList;

public class Department {
    Department department;

    ArrayList<Employee> employees;
    Department(String deptName){
        employees = new ArrayList<Employee>();

    }

    static Employee countingGuru = new Employee("Counting Guru", 55);
    static Employee countingPro = new Employee("Counting Pro", 45);
    static Employee countingSavvy = new Employee("Counting Savvy", 40);
    static Employee countingNovice = new Employee("Counting Novice", 25);
    static Employee salesGuru = new Employee("Sales Guru", 50);
    static Employee salesPro = new Employee("Sales Pro", 48);
    static Employee salesSavvy = new Employee("Sales Savvy", 38);
    static Employee hiringGuru = new Employee("Hiring Guru", 58);
    static Employee hiringPro = new Employee("Hiring Pro", 47);
    static Employee hackingPro = new Employee("Hacking Pro", 46);
    static Employee hackingGuru = new Employee("Hacking Guru", 51);
    static Employee hackingSavvy = new Employee("Hacking Savvy", 38);
    static Employee hackingNovice = new Employee("Hacking Novice", 23);

    public void addEmployee(Employee employee){
        employee.setDepartment(this);
        employees.add(employee);
    }

    accounting.addEmployee(countingGuru);

}
public class Employee implements Comparable<Employee> {
    String empName;
    int empAge;
    Department department;

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    String name;
    int age;
    public Employee(String name, int age) {
        this.name = name;
        this.age  = age;
    }

    public String getName() {
        return empName;
    }

    public void setName(String name) {
        this.empName = name;
    }

    public int getAge() {
        return empAge;
    }

    public void setAge(int age) {
        this.empAge = age;
    }

    @Override
    public int compareTo(Employee arg0) {
        return 0;
    }
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • Just a general note, you have fields with setters and getters but they are not declared private. Java fields are package-access by default unless explicitly declared otherwise. Some languages differ I know, notably Objective-C is the opposite (private by default). Having setters and getters for public/protected fields is of course redundant. – Radiodef Oct 24 '13 at 18:14
  • Your code is a total mess. You should read a tutorial first. – Damian Leszczyński - Vash Oct 24 '13 at 18:16
  • @Vash i think it's pretty clear i am just starting out. i first looked at java 6 days ago. i would love to make it not a "total mess," so could you post something constructive instead of being rude? thanks! – thatpaintingelephant Oct 24 '13 at 18:20
  • @thatpaintingelephant, I do not know why do you find my advice rude. I think that a tutorial is good place to start. – Damian Leszczyński - Vash Oct 24 '13 at 18:22
  • @thatpaintingelephant Not meant as a negative comment but if you are just starting out maybe you should master the basics before using stuff like Comparables and Collections. You can do those same things with conditional statements and arrays and the problems you will have will be much simpler to debug. – Radiodef Oct 24 '13 at 18:23
  • i have done the lynda.com essential java training; i didn't find it all that great, but i'm in a time crunch to do this assessment, so i've been trying to power through it before i jump into another tutorial. do you have a tutorial to suggest? thanks! – thatpaintingelephant Oct 24 '13 at 18:25
  • @Radiodef thank you, that's very helpful. sometimes i get stuck in a google vortex and start trying to implement things that are way over my head because i don't know there is a simpler option. – thatpaintingelephant Oct 24 '13 at 18:26
  • @thatpaintingelephant: sorry, but I've got to agree with Vash on this one. The comment you left below shows that you need work on some basic object-oriented programming principles. This is a fairly advanced program for someone who started with Java six days ago, and trying to fix it is actually just going to reinforce bad habits. Even if you have previous OO experience, I'd recommend looking at a good Java book and/or tutor. Head First Publishing has an excellent basic Java book. – musical_coder Oct 24 '13 at 18:26
  • @musical_coder thanks, i will pick it up! – thatpaintingelephant Oct 24 '13 at 18:28

2 Answers2

2

You have accounting.addEmployee(countingGuru); outside of any function. A non-variable declaration like this needs to be in a method, or a constructor, or a static block.

Since accounting is a static member of Company, you'll need to reference it as Company.accounting.addEmployee(countingGuru);.

Btw, this isn't great design to have so many things be static, because it means that every single Company object you ever create will have the same accounting, marketing, infoSys, and humanRes departments. You should make those fields non-static and initialize them in Company's constructor.

Community
  • 1
  • 1
musical_coder
  • 3,886
  • 3
  • 15
  • 18
  • ahh, i see. so something like this? public void addToDepartment() { accounting.add(countingGuru); } – thatpaintingelephant Oct 24 '13 at 18:14
  • Because accounting and countingGuru are both static members it is also possible to enclose the statement in a static block: `static { accounting.add(countingGuru); }` but I don't recommend that. I agree with @musical_coder that so much use of static is unneeded. I point this out merely because you said you are learning and this is syntax you can do. (And there are some cases where a method call during static initialization is necessary.) – Radiodef Oct 24 '13 at 18:30
  • thank you. i hate to admit defeat, but i agree that i need to spend more time with the basics. i don't want to develop bad habits and throw myself into something i'm not prepared for. – thatpaintingelephant Oct 24 '13 at 18:36
  • 1
    @thatpaintingelephant: well said! Props to you for making the effort to learn this right. – musical_coder Oct 24 '13 at 18:42
1

Syntactically, if you change the constructor of Department to this

Department(String deptName){
    employees = new ArrayList<Employee>();
    Company.accounting.addEmployee(countingGuru);
}

and remove the line

    accounting.addEmployee(countingGuru);

then, your code will be error free.

Vikrant Goel
  • 654
  • 6
  • 20