#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    1
    Rep Power
    0

    Java Help Homework


    hello all I have no idea how to program or structure this:

    The assignment is as follows:
    5) 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.
    Use the following template as a guideline on how to structure your accessor & modifier methods (“gets” & “sets”) and how to handle any errors:
    private void setSomeAttribute(argType someParameter) {
    if (someParameter > 100) // I used 100 as an example for error checking! {
    System.out.println("Bad value passed in for someAttribute: " + someParameter);
    System.exit(-1); }
    someAttribute = someParameter; }
    public returnType getSomeAttribute() {
    return someAttribute; }
    You should try to make your “set” method error messages as informative as possible. For example:
    Bad Last Name Size: Someverylonglastnamethatshouldcauseproblems:43 Employee Id must be 4 or more digits: 22
    Hourly Rate must be greater than zero: -23.48
    © Christopher Hield
    2 of 5
    CIS 314 Intermediate Object Oriented Programming
    6) Next, change the Employee constructor implementation so that rather than simply assigning the Employee’s data attributes directly to the incoming parameters with an “=” sign, it will now call the “set” methods for each of Employee’s data attributes.
    For Example:
    Rather than doing this:
    someAttribute = someParameter; We now want to do this:
    setSomeAttribute(someParameter);
    7) Finally, completely remove the “main” method from the Employee class. We will be doing something a little different for the “main” method in this assignment.
    8) At this point, your “Employee” class should compile cleanly – with no errors.
    9) Now, create a brand new Java class called “Driver” in the existing “coursework” package. We’ve created new classes before so you should know how to do this by now. If you have forgotten, refer to the NetBeans notes pack, or the instructions in your previous assignments.
    10) Add the following “main” method to your newly created “Driver” class (in between the opening and closing curly- braces of the “Driver” class):
    public static void main(String[] args) {
    // First we variable of type Employee.
    // We always set this to null as good practice. Employee e = null;
    // Declare variables to hold user inputs needed // by the Employee constructor
    String firstNameInput;
    String lastNameInput;
    int idInput; double rateInput;
    // Get input data values from the user (via the keyboard) Scanner userInput = new Scanner(System.in);
    System.out.println("Employee First Name: "); firstNameInput = userInput.next();
    System.out.println("Employee Last Name: "); lastNameInput = userInput.next();
    System.out.println("Employee Id: " ); idInput = userInput.nextInt();
    System.out.println("Employee Hourly Rate: "); rateInput = userInput.nextDouble();
    // Now allocate a new instance of an Employee,
    // passing test data to the constructor.
    e = new Employee(firstNameInput, lastNameInput,
    idInput, rateInput);
    // Print out the data with nice titles System.out.println();
    System.out.println("Name: " + e.getFirstName() + " "
    + e.getLastName()); System.out.println( "Emp. Id: " + e.getEmployeeId());
    System.out.println("Hourly Rate: $" + e.getHourlyRate());
    // We're done with this employee, so now free up
    // the space we allocated for that employee by setting // the "e" variable back to null
    e = null;
    System.out.println(); }
    © Christopher Hield
    3 of 5
    CIS 314 Intermediate Object Oriented Programming
    11) You will need to “import” “java.util.Scanner” at the top of the “Driver” class, below the “package statement:
    package coursework;
    import java.util.Scanner;
    public class Driver { ...
    ...
    }
    12) Now compile your project – the “Driver.java” file and the “Employee.java” files will be compiled. Fix any compiler errors as usual. Then - run the program.

    This is what I have so far..


    package coursework;

    /**
    *
    * @author smc
    */
    public class Employee {
    String firstName;
    String lastName;
    int employeeId;
    double hourlyRate;

    public Employee (String first, String last, int empId,
    double lyRate)
    {
    firstName = first;
    lastName = last;
    employeeId = empId;
    hourlyRate = lyRate;
    }
    public void printEmpDetails()
    {
    System.out.println("name: " + firstName + " " + lastName);
    System.out.println("emp. Id: " + employeeId);
    System.out.println("hourly Rate: $" + hourlyRate);
    System.out.println();
    }
    public static void main(String[] args) {
    Employee emp1 = new Employee("Henry","Cook" , 2350,23.50);
    Employee emp2 = new Employee("Tyler","Cook", 7080,19.75);
    emp1.printEmpDetails();
    emp2.printEmpDetails();

    }}
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Please edit your post and wrap the code in code tags.

    Do you have any specific questions about the program?

    Also posted at: http://www.javaprogrammingforums.com...tion-help.html
    Last edited by NormR; January 28th, 2014 at 07:46 AM.

IMN logo majestic logo threadwatch logo seochat tools logo