Home > Computer Programming > Class task example

Class task example

October 22, 2012 Leave a comment Go to comments

This is the task we looked at in class today.

Notes:

  • 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();
        e.name = "Mr Robertson";
        e.jobTitle = "teacher";
        e.department = "Computer Science";
        e.salary = 10000;
        if (e.isTeacher()){
            e.sayHello();
        }
    }
}

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");
    }
}

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s