1 Replies - 204 Views - Last Post: 28 January 2014 - 10:10 PM Rate Topic: -----

#1 zng690  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-January 14

Java Class Help

Posted 28 January 2014 - 10:02 PM

HI All, I just started this class and the assignment is





Now, create public accessor & private modifier methods (gets & sets) in the “Employee.java” file for each of the 4 data
members. Use the following as the error-checking criteria in your modifier (“set”) methods:
- First Name & Last Name: Must have size greater than zero, and size less than or equal to 20.
- Employee Id: Value must be greater than or equal to 1000, but less than or equal to 9999.
- Employee Rate: Must be greater than zero.

public class Employee {
 
public String firstName;
public String lastName;
private double hourlyRate;
private int employeeId;
 
 
private Employee(String first, String last, double hourly, int employeeID) 
{
firstName = first;
lastName = last;
hourlyRate = hourly;
employeeId = employeeID;
}
 
 
public static void main(String[] args)
{
 
Employee e = new Employee("John", "Doe", 14.79, 1597);
 
System.out.println("Name: " + e.firstName + " " + e.lastName);
System.out.println("Hourly: $" + e.hourlyRate);
System.out.println("EmployeeID: " + e.employeeId);
System.out.println(); // This will simply create a blank line
 
e = new Employee("Sarah", " Dao", 17.55, 2574);
 
System.out.println("Name: " + e.firstName + " " + e.lastName);
System.out.println("Hourly: $" + e.hourlyRate);
System.out.println("EmployeeID: " + e.employeeId);
System.out.println(); 
}}




Is This A Good Question/Topic? 0
  • +

Replies To: Java Class Help

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4356
  • View blog
  • Posts: 12,175
  • Joined: 18-April 07

Re: Java Class Help

Posted 28 January 2014 - 10:10 PM

Well the first thing you need to do is create functions for your class. Ones called getters typically have a name like getFirstName() and return a value... in this case the first name stored in the firstName variable you have there. Others called "setters" usually take the form of setFirstName(parameter) which take in "parameter" and set's a variable to this passed in value and sets the class variable... in this case firstName = parameter;

Now in your setters, you should take in the parameter and check it to make sure that it is a good value. That is where your tests come in. Here you have to make sure the length is proper and that it is less than or equal to 20.

public void setFirstName(String name) {
   int nameLength = name.length();

   if ((nameLength > 0) && (nameLength <= 20)) {
      // Here we are setting our class variable firstName to the passed in variable "name"
      // But only if it has a length greater than 0 and less than or equal to 20
      firstName = name;
   }
}



Just to give you an idea of how this works. Do similar functions and tests for your other variables. :)

This post has been edited by Martyr2: 28 January 2014 - 10:11 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1