Java
OOP GUIDE / WORK
EMPLOYEE CLASS
Topics
- Encapsulation
- Using multiple classes
TASK –
PART 1 – SETUP: NAME CLASS
Copy
the following Name class into your IDE.
It is a simple class but take a few minutes to understand how it
works.
public class
Name
{
private String
first;
private
String middle;
private
String last;
public Name(String fi,
String mi, String la)
{
this.first = fi.substring(0,1).toUpperCase() + fi.substring(1).toLowerCase();
this.middle = mi.substring(0,1).toUpperCase() + mi.substring(1).toLowerCase();
this.last = la.substring(0,1).toUpperCase() + la.substring(1).toLowerCase();
}
public
String getFirst()
{
return first;
}
public
String getMiddle()
{
return middle;
}
public
String getLast()
{
return last;
}
public
String getInitials()
{
return first.substring(0,1)
+ "." + middle.substring(0,1)
+ "." + last.substring(0,1)
+ ".";
}
public
String toString()
{
return first + "
" + middle + "
" + last;
}
}
|
TASK –
PART 2 – SETUP: ADDRESS CLASS
Copy
the following Address class into your IDE.
It is a simple class but take a few minutes to understand how it
works.
public class
Address
{
private int streetNumber;
private
String streetName;
private
String streetType;
private
String aptNumber;
private
String city;
private
String province;
private
String country;
private
String postalCode;
public Address(int snu, String sna, String st, String an,
String ci, String pr,
String co, String pc)
{
streetNumber = snu;
streetName = sna;
streetType = st;
aptNumber = an;
city = ci;
province = pr;
country = co;
postalCode = pc;
}
public Address(int snu, String sna, String st, String ci,
String pr,
String co, String pc)
{
streetNumber = snu;
streetName = sna;
streetType = st;
aptNumber = "";
city = ci;
province = pr;
country = co;
postalCode = pc;
}
public int getStreetNumber()
{
return streetNumber;
}
public
String getStreetName()
{
return streetName;
}
public
String getStreetType()
{
return streetType;
}
public
String getAptNumber()
{
return aptNumber;
}
public
String getCity()
{
return city;
}
public
String getProvince()
{
return province;
}
public
String getCountry()
{
return country;
}
public
String getPostalCode()
{
return postalCode;
}
public
String traditionalAddress()
{
String apt = "";
if (!aptNumber.equals(""))
{
apt = apt + ",
Apt #" + aptNumber;
}
String add = "";
add = add + streetNumber + "
" + streetName + "
" + streetType + apt + ",\n";
add = add + city + ",
" + province + ",
" + country + "\n";
add = add + postalCode;
return add;
}
public
String canadaPostAddress()
{
String apt = "";
if (!aptNumber.equals(""))
{
apt = aptNumber + "-";
}
String add = "";
add = add + apt + streetNumber + "
" + streetName + "
" + streetType + ",\n";
add = add + city + ",
" + province + ",
" + country + "\n";
add = add + postalCode;
return add;
}
public
String toString()
{
return canadaPostAddress(); //alternate:
traditionalAddress();
}
}
|
TASK –
PART 3 – SETUP: JOB CLASS
Copy
the following Job class into your IDE.
It is a simple class but take a few minutes to understand how it
works.
public class Job
{
private
String title;
private
String description;
private double salary;
private int vacationDays;
public Job(String ti,
String de, double sal, int vd)
{
title = ti;
description = de;
salary = sal;
vacationDays = vd;
}
public
String getTitle()
{
return title;
}
public
String getDescription()
{
return description;
}
public double getSalary()
{
return salary;
}
public int getVacationDays()
{
return vacationDays;
}
public String toString()
{
String s = "Position:
" + title + "\n";
s = s + "Description:
" + description + "\n";
s = s + "Salary:
$" + salary + "\n";
s = s + "Vacation
days: " + vacationDays + "\n";
return s;
}
}
|
TASK –
PART 4 – TESTING CLASSES
Copy
the following Tester class into your IDE.
It simply creates a Name, Address and Job object. Take a few minutes to understand the
code. Feel free to experiment.
public class
Tester
{
public static void
main(String[] args)
{
Name n = new Name("Pat", "Omer", "Campeau");
System.out.println(n);
System.out.println(n.getInitials());
System.out.println("");
Address a = new Address(1234, "Western", "avenue", "524", "Val
Caron", "Ontario", "Canada", "P3B
1V8");
System.out.println(a);
System.out.println("");
Job j = new Job("Waiter/Waitress", "Take
food orders, deliver orders, clean tables, socialize with customers",
40000.00, 15);
System.out.println(j);
}
}
|
TASK –
PART 5 – EMPLOYEE CLASS
We
are finally ready to create our own class that will make use of the Name,
Address and Job classes.
Create
the following Employee class with the following specifications:
INSTANCE VARIABLES
- private int id – the employee’s
id number such as 123456
- private Name empName
– the employee’s name
- private Address empAddress – the employee’s address
- private Job empJob
– the employee’s job
CONSTRUCTORS
- A constructor that gets and int,
a Name, an Address and a Job and simply passes each one to the
corresponding instance variable.
- A constructor that gets an int
for the id, three Strings for the name, one int and seven Strings for
the address and 1 int, 1 double and 2 Strings for the job. It then creates and initializes all
the instance variables. Note that
this is a little painful simply because there are so many parameters.
METHODS
- Four get methods; one for each
instance variable.
- A getMailingAddress()
method that returns the employee’s Name followed by their Address.
- A toString() method
that returns the employee’s name, the employee’s id and the employee’s
job title.
TASK –
PART 6 – TESTING THE EMPLOYEE CLASS
Inside
the Tester class that you previously copied to your IDE, at the bottom of the
main function, create an Employee object.
You might want to use the existing Name, Address and Job objects to
save you some time.
Test
out all of the Employee methods.
|