Results 1 to 3 of 3
Thread: Create an employee class
- 10-05-2011, 10:13 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
Create an employee class
I have been given an assignment to create an Employee class that hold a bunch of information. I have simplified the problem becasue this is all so new and complicated to me. For now, I just want the class to hold the employees first name. I need to create an application that uses the Employee class.
Below is my Employee class
Now I need to enter a first name in my test application. This is where I am stuck. I have read through my chapter numerous times. I have searched the internet. I just don't fully understand getters and setters and the syntax used. Below is my test application.Java Code:public class Employee { // create data fields private String firstName; private String lastName; private String phoneNumber; private String address; private int id; private String title; private double Salary; // Construct a default Employee object public Employee() { } // Construct a second constructor public Employee (String newFirstName, String newLastName) { firstName = newFirstName; lastName = newLastName; } //This method returns a String with the contents of //the variable firstName to whatever object calls it public String getFirstName() { return firstName; } //This method allows the contents of firstName //to be changed to store a different String value, should that be required public void setFirstName (String newFirstName) { firstName = newFirstName; } }
I have also attached my files. Thanks.Java Code:import java.util.Scanner; public class TestEmployee { public static void main (String[] args) { //create a new scanner Scanner input = new Scanner(System.in); //create a new employee Employee employee1 = new Employee(); //invoke getFirstName method employee1.getFirstName(); System.out.println ("Enter employee first name"); employee1.newFirstName = input.next(); //invoke setFirstName method employee1.setFirstName(employee1.getFirstName()); } }
- 10-05-2011, 10:24 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Create an employee class
Setters take something as an argument and change something about the class. If for instance you have a method
This would change the firstName instance variable. If you passed in the strong "Bob", then the employees name will be Bob.Java Code:setFirstName(String s);
Setters allow you to view the information which makes up a class. If you wanted to print the persons first name you could type
Since all the instance variables are private you cannot directly access them. Since you cannot directly access them you can't modify a person, and that is why you provide getters and setters.Java Code:System.out.println(e.getFirstName()); //where e is an employee
- 10-11-2011, 04:41 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
Re: Create an employee class
Thank you Sunde887. This makes sense! My program has progressed. I now have all my getters and setters. I have run into a new problem though that involves inputing the address. When I ask for an address, many strings are returned. I can't figure out how to combine all the strings. I'm using the nextLine() method. It just returns a blank space. I kind of understand why. It is because, if the nextLine() is invoked after a token-reading method, the method reads characters that start from this delimiter and end with the line seperator. The line seperator is read, but it is not part of the string returned by nextLine(). Ok, that's why it happens. But how do I fix this problem.
Here is my new code:
Java Code:import java.util.Scanner; public class TestEmployeeV4 { public static void main (String[] args) { //Create variables String nameFirst = ""; String nameLast = ""; String phone = ""; String addressOfEmp = ""; int idEmp = 0; String titleEmp = ""; double salaryEmp = 0.0; //add this array to UML int numberOfEmployees = 5; //create a new scanner Scanner input = new Scanner(System.in); //create a default employee Employee myEmployee = new Employee(); //create a new employee Employee myEmployee1 = new Employee(nameFirst, nameLast); System.out.println ("Enter employee first name"); nameFirst = input.next(); System.out.println ("Enter employee last name"); nameLast = input.next(); System.out.println ("Enter employee phone number"); phone = input.next(); //how do you tie all these strings together System.out.println ("Enter employee address"); addressOfEmp = input.nextLine(); System.out.println ("Enter employee ID"); idEmp = input.nextInt(); System.out.println ("Enter employee title"); titleEmp = input.next(); System.out.println ("Enter employee salary"); salaryEmp = input.nextDouble(); //invoke setFirstName method myEmployee1.setFirstName(nameFirst); //invoke setLastName method myEmployee1.setLastName(nameLast); //invoke setPhoneNumber method myEmployee1.setPhoneNumber(phone); //invoke setAddress method myEmployee1.setAddress(addressOfEmp); //invoke setID method myEmployee1.setID(idEmp); //invoke setTitle method myEmployee1.setTitle(titleEmp); //invoke setSalary method myEmployee1.setSalary(salaryEmp); System.out.println("The employee's name is " + myEmployee1.getFirstName() + " " + myEmployee1.getLastName()); }
Similar Threads
-
Class employee,company,test: Read data from text file.
By jeskoston in forum New To JavaReplies: 4Last Post: 03-01-2011, 01:50 AM -
employee
By srish in forum Suggestions & FeedbackReplies: 7Last Post: 07-21-2010, 04:52 AM -
search employee id
By raman_v in forum Java ServletReplies: 2Last Post: 12-24-2009, 08:44 AM -
Implement a class Employee-Help due tmrw!
By Britt7 in forum New To JavaReplies: 3Last Post: 10-27-2008, 01:37 AM -
How to create main class link to another two class?
By pearllymary78 in forum New To JavaReplies: 6Last Post: 07-16-2008, 11:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks