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 2 of 2

Thread: Needs help with code

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Needs help with code

    I was wondering how do I go about adding this to my code?

    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();
    }}


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Needs help with code

    Accessor methods are your get() methods; they allow outside classes to access the private members of another class.

    For example:

    public class ClassA
    {
     
       private int number;
     
       public ClassA(int theNumber)
       {
     
          number = theNumber;
     
       }
     
       //Now, we make a get() method for number, because it is private
       //and cannot be accessed by other classes
       public int getNumber()
       {
     
          return number;
     
       }
    }

    public class ClassATest
    {
     
       public static void main(String[] heeheeNotCallingThisArgs)
       {
     
          ClassA test = new ClassA(5);
     
          int cake = test.number; //This would throw an exception, since number is private
          int otherCake = test.getNumber(); //This works, because getNumber() is public
     
       }
    }

    Now, your modifier methods are your set() methods. I really have no clue why you'd want them to be private; this would mean that there's no way to edit the values in your class. Usually set() methods are public. Here's what a typical set() method looks like (using that previous class):

    public class ClassA
    {
     
       private int number;
     
       public ClassA(int theNumber)
       {
     
          number = theNumber;
     
       }
     
       public int getNumber()
       {
     
          return number;
     
       }
     
       //Now we'll add the modifier (set) method, so that number's value can be changed
       public void setNumber(int newNumber)
       {
     
          number = newNumber;
     
       }
    }

    public class ClassATest
    {
     
       public static void main(String[] heeheeNotCallingThisArgs)
       {
     
          ClassA test = new ClassA(5);
     
          test.number = 2; //This would throw an exception, since number is private
          test.setNumber(2); //This works, because setNumber() is public
     
       }
    }

    Does that clear things up at all?
    Last edited by snowguy13; February 25th, 2012 at 11:18 AM.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. code to refresh and check the code of a webpage..
    By vaggelis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2012, 07:43 AM
  2. Help merging program code with GUI code
    By Wilha in forum AWT / Java Swing
    Replies: 2
    Last Post: January 25th, 2012, 07:03 PM
  3. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  4. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  5. describe this program code by code ....
    By izzahmed in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2011, 11:03 PM