|
The Java Language
|
CS 161 - Java |
- Chapter 4 - Classes, Objects, and Methods
- Chapter 5 - Programming with Classes and Methods
- Write a simple Java program from scratch;
- Learn how to write a Java class with fields and methods.
-
To start thinking like a programmer to break a problem into
smaller pieces.
- To start getting the computer to calculate answers.
To start learning to be exact when giving the
computer instructions.
Do the following:
-
Write a Java class named Employee. This class should have the following
instance variables:
- an ID value
- the name of a person associated with the Employee
- a field to store a value for the annual salary
- a field to store a reference to another Employee object
(for the Employee's boss, eventually)
-
Write methods with the following signatures
- a main method to start things off with the signature:
public static void main (String [] args)
see below for what main should do.
- a public constructor for the class that will insure that Employee
objects always have a name field with the signature
public Employee( String name)
- a method to set an Employee object's salary field with the signature
public void setSalary( double value )
- a method to get an Employee object's current salary field value
with the signature
public double getSalary( )
- a method to set an Employee object's field referencing another
Employee object with the signature
public void setBoss ( Employee boss )
- a method to return a String identifying the field contents of an
Employee with the signature
public String toString()
-
The main method for this class should:
- create a single instance of the Employee class,
- assign a name and a salary value (any value), using the constructor
and the setSalaray() methods
- Access the current salary value (using the getSalary() method)
and increase the Employee's salary by 10% using the setSalary method again.
- then have the Employee
object identify itself and it's current salary value.
Hint: Do the reading assignment before doing the homework.
In following assignments you are going to create several Employee
objects. This assignment's goal is to create an object that will
store information about a single employee. Next week we'll explore
ways to create a number of Employee objects.
You should not do everything in the main method, do only
what is asked for in the main method. Look at the in-class examples
as a model for how the Employee class should be created.
Do only what is asked above. Do not add any extra code to prompt
for input or otherwise add capabilities not asked for, simply use your
name and whatever salary value you choose in the program. Be sure to
create methods with the signatures requested. Output similar to the
following is all the Employee class needs to produce:
Joe Student salary: 55000
Hint: To start, create the outline of your class definition by defining
empty methods with the signatures listed above. Compile it to be sure
the outline is in the right form. Then go back and start filling in
the details of each method. Compile often so you can correct errors
easily.
Hand in listings of your source file, and the usual transcript of its
execution.