Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java Homework Question Help

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Java Homework Question Help

    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

    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(); }
    
    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
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Homework Question Help

    You have been asked several times to post your code correctly and links have been provided to show you how. Here is the link again. Advice has been given and questions have been asked in the other threads you've started, and you rarely (if ever?) return to provide feedback on the advice given or answer the questions asked.

    Without code posted correctly and timely feedback from you, I suspect the volunteers who help here are wondering why they should spend time on your posts. Observe the minimal forum rules and guildelines and make an effort to participate in the process which is all meant to be to your benefit, not ours.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Homework Question Help

    Also posted at Java Help Homework - Dev Shed
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Beginner struggling with homework question
    By mets3214 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 6th, 2013, 08:10 PM
  2. Homework question having to do with arrays...
    By TheoAnderson in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 3rd, 2013, 12:55 PM
  3. Predicting the Outcome without a compiler **Not a Homework Question**
    By sternfox in forum Java Theory & Questions
    Replies: 1
    Last Post: February 25th, 2013, 04:53 PM
  4. [SOLVED] Homework Question
    By EPC in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 24th, 2013, 01:33 PM
  5. Question about homework problem.
    By Rain_Maker in forum Java Theory & Questions
    Replies: 13
    Last Post: February 7th, 2013, 08:11 PM

Tags for this Thread