Conceptual Understand of Get and Set Methods

Reply

Join Date: Sep 2008
Posts: 13
Reputation: Gerbilkit is an unknown quantity at this point 
Solved Threads: 0
Gerbilkit Gerbilkit is offline Offline
Newbie Poster

Conceptual Understand of Get and Set Methods

 
0
  #1
Sep 15th, 2008
Well I just started a university class in Java, I am used to PHP so this has been a confusing shift in many ways. Right now I'm struggling a bit with my first homework assignment, although more in a general understanding of java than nitty gritty details.

The code is as follows, and my question is this. If there is already a section of get and set methods to get the values of the variables, then what is the purpose of the getInput() method? Or vice versa if the getInput() method already obtains all the data from the user, why do I need all these get and set methods? As of right now I'm think one of the two should probably go, but perhaps I simply have a weak understanding of the purpose of these code constructions in java.

Any help would be appreciated. I can code the statements themselves, just not sure if both should exist.

  1. /**
  2.  * CasualEmployee class works with the Payroll System to manage information
  3.  * regarding Casual Laborer Employees.
  4.  * CSCE 155 Fall 2008
  5.  * Assignment 1
  6.  * @author
  7.  * @version
  8.  */
  9.  
  10. // import statements
  11. import java.io.*;
  12. import java.util.*;
  13.  
  14. public class CasualEmployee {
  15.  
  16. // -------------------------------------------------------------------------
  17. // You may add more data members to the following to describe a Casual
  18. // Employee. You may need to remove some data members as well.
  19. // -------------------------------------------------------------------------
  20.  
  21. //-------------------------------------------------------------------------
  22. //Declare the constants
  23. private final String DEFAULT_ORIGIN = "USA";
  24. private final double FEDERAL_TAX_PERCENT = 0.15; //indicates the tax
  25. //percent for the
  26. //federal government
  27. private final double STATE_TAX_PERCENT = 0.08; //indicates the tax
  28. //percent for the
  29. //state government
  30.  
  31. //-----------------------------------------------------------------------
  32. //private variables
  33. private String destination;
  34. private String employeeID; //ID for the employee
  35. private String employeeName; //Name for the employee
  36. private int hourlyRate; //the amount in dollors for employee
  37. private int numberHours; //number of hours worked each day
  38. private String origin;
  39. private Date startDate; //Date of employment
  40.  
  41. public CasualEmployee(){
  42. // Set all of the data values for the object on creation.
  43. // Need to initialize other data members.
  44. this.employeeName = "";
  45. this.origin = this.DEFAULT_ORIGIN;
  46.  
  47. }//end of constructor
  48.  
  49.  
  50. //-------------------------------------------------------------------------
  51. //Please complete the following get and set methods
  52. //Some may need to be added or removed.
  53. //-------------------------------------------------------------------------
  54. public String getDestination() {
  55. return destination;
  56. }
  57.  
  58. public String getEmployeeID() {
  59. return employeeID;
  60. }
  61.  
  62. public String getEmployeeName() {
  63. return employeeName;
  64. }
  65.  
  66. public int getHourlyRate() {
  67. return hourlyRate;
  68. }
  69.  
  70. public String getOrigin() {
  71. return origin;
  72. }
  73.  
  74. public void setDestination(String destination) {
  75.  
  76. }
  77.  
  78. public void setEmployeeID(String employeeID) {
  79. this.employeeID = employeeID;
  80. }
  81.  
  82. public void setEmployeeName(String employeeName) {
  83.  
  84. }
  85.  
  86. public void setHourlyRate(int hourlyRate) {
  87. this.hourlyRate = hourlyRate;
  88. }
  89.  
  90. public void setNumberHours(int numberHours) {
  91.  
  92. }
  93.  
  94.  
  95. // --------------------------------------------------------------------------
  96. // In the following, we will just have some "simulated" methods since you do
  97. // do not have enough background to actually implement the following
  98. // behavior. Our calculation is simply to printout a statement "performing"
  99. // an action. You need to perform the actual calculations for this type
  100. // of employee.
  101. // Hint!
  102. // 1. Complete the getInput method. You will need to prompt the user for
  103. // all of the information related to the employee. Use the provided
  104. // readInteger and readString methods within your code to do the actual
  105. // reading or information from the keyboard. Store all of the
  106. // information appropriately.
  107. // 2. Complete the calculateNetWeekly method. You will need to take some
  108. // of the information you have read from the getInput method and do the
  109. // appropriate calculations. You may need to break down this method
  110. // into several seperate methods and/or determine how to store the
  111. // calculated information.
  112. // 3. Complete the display method. You need to print out the information
  113. // that was input or calculated as needed for this employee type.
  114. // --------------------------------------------------------------------------
  115. public void calculateNetWeekly(){
  116.  
  117. }//end of calculateNetWeekly
  118.  
  119. public void display(){
  120. System.out.println("Calculating Pay for Employee");
  121.  
  122. }//end of display
  123.  
  124. public void getInput(){
  125.  
  126.  
  127. }//end of getInput
  128.  
  129.  
  130. /**
  131.   * This method reads an integer and
  132.   * returns that integer to the caller of this method.
  133.   */
  134. private int readInteger() {
  135.  
  136. int temp = 0;
  137.  
  138. Scanner scanner = new Scanner(System.in);
  139.  
  140. try {
  141. temp = scanner.nextInt(); // read integer
  142. } catch (InputMismatchException ex) {
  143. System.out.println(ex);
  144. }
  145.  
  146. return temp;
  147.  
  148. } // end readInteger
  149.  
  150. /**
  151.   * This method reads in a string and returns that string to the caller of
  152.   * this method.
  153.   */
  154. private String readString() {
  155.  
  156. String userInput = "";
  157.  
  158. Scanner scanner = new Scanner(System.in);
  159.  
  160. userInput = scanner.nextLine();
  161.  
  162. return userInput;
  163.  
  164. } // end readString
  165.  
  166. }//end of class CasualEmployee
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Conceptual Understand of Get and Set Methods

 
0
  #2
Sep 15th, 2008
The get and set methods refer to getting and setting these variables:

  1. //private variables
  2. private String destination;
  3. private String employeeID; //ID for the employee
  4. private String employeeName; //Name for the employee
  5. private int hourlyRate; //the amount in dollors for employee
  6. private int numberHours; //number of hours worked each day
  7. private String origin;
  8. private Date startDate; //Date of employment


You need them because they are private. They don't require any input from the user. The getInput method asks the user for information:

// 1. Complete the getInput method. You will need to prompt the user for
// all of the information related to the employee. Use the provided
// readInteger and readString methods within your code to do the actual
// reading or information from the keyboard. Store all of the
// information appropriately.
There are times when you may need to change or access the variables and don't need to prompt the user for any information. For example, you initially might call the getInput method to ask for the initial values. Later you might give an employee a dollar per hour raise and you wouldn't need or want to prompt the user for input again. You could also make a variety of calculations using the values which also wouldn't require the user input, so you'd go directly to the get and set methods.

Not that there is no Input variable, unlike the the other get and set variables, so the meaning is slightly different. You aren't passing getInput any parameters and it isn't returning anything, unlike the otehr get and set methods, so that's a clue that the function's purpose is different. Again, in this case, the big difference is that it requires interaction with the user for input and the others do not. If the variables were public rather than private, those other get and set methods could be deleted, but getInput could not.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC