Class task example

This is the task we looked at in class today.


  • Don’t get mixed up between returning a value and printing a value. Your methods should only print if the questions explicitly tells you to print.
  • Notice that definition of the Employee (the class) is totally separate from the instantiation of Employees (objects).
  • There is no constructor here, but you will be expected to create them in your quiz.
  • You will also need to correctly specify method headers. The method header for the isTeacher method, for example, is boolean isTeacher(). Method headers always have the form [return type] methodName (parameter 1, parameter 2, …), where you need to decide what the return type is, you need to decide what the name is, and you need to decide what, if any, parameters there are.


Here is the code that I would consider to be a perfect answer.

package employeemain;

public class EmployeeMain {

    public static void main(String[] args) {
        Employee e = new Employee(); = "Mr Robertson";
        e.jobTitle = "teacher";
        e.department = "Computer Science";
        e.salary = 10000;
        if (e.isTeacher()){

class Employee{
    String name;
    String jobTitle;
    String department;
    double salary;

    void sayHello(){
        System.out.println("Hello. My name is " + name + ". I am a " + jobTitle + " and I work in the " + department + " department.");

    boolean isTeacher(){
        return jobTitle.equalsIgnoreCase("teacher");

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s