| | |
Conceptual Understand of Get and Set Methods
![]() |
•
•
Join Date: Sep 2008
Posts: 13
Reputation:
Solved Threads: 0
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.
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.
Java Syntax (Toggle Plain Text)
/** * CasualEmployee class works with the Payroll System to manage information * regarding Casual Laborer Employees. * CSCE 155 Fall 2008 * Assignment 1 * @author * @version */ // import statements import java.io.*; import java.util.*; public class CasualEmployee { // ------------------------------------------------------------------------- // You may add more data members to the following to describe a Casual // Employee. You may need to remove some data members as well. // ------------------------------------------------------------------------- //------------------------------------------------------------------------- //Declare the constants private final String DEFAULT_ORIGIN = "USA"; private final double FEDERAL_TAX_PERCENT = 0.15; //indicates the tax //percent for the //federal government private final double STATE_TAX_PERCENT = 0.08; //indicates the tax //percent for the //state government //----------------------------------------------------------------------- //private variables private String destination; private String employeeID; //ID for the employee private String employeeName; //Name for the employee private int hourlyRate; //the amount in dollors for employee private int numberHours; //number of hours worked each day private String origin; private Date startDate; //Date of employment public CasualEmployee(){ // Set all of the data values for the object on creation. // Need to initialize other data members. this.employeeName = ""; this.origin = this.DEFAULT_ORIGIN; }//end of constructor //------------------------------------------------------------------------- //Please complete the following get and set methods //Some may need to be added or removed. //------------------------------------------------------------------------- public String getDestination() { return destination; } public String getEmployeeID() { return employeeID; } public String getEmployeeName() { return employeeName; } public int getHourlyRate() { return hourlyRate; } public String getOrigin() { return origin; } public void setDestination(String destination) { } public void setEmployeeID(String employeeID) { this.employeeID = employeeID; } public void setEmployeeName(String employeeName) { } public void setHourlyRate(int hourlyRate) { this.hourlyRate = hourlyRate; } public void setNumberHours(int numberHours) { } // -------------------------------------------------------------------------- // In the following, we will just have some "simulated" methods since you do // do not have enough background to actually implement the following // behavior. Our calculation is simply to printout a statement "performing" // an action. You need to perform the actual calculations for this type // of employee. // Hint! // 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. // 2. Complete the calculateNetWeekly method. You will need to take some // of the information you have read from the getInput method and do the // appropriate calculations. You may need to break down this method // into several seperate methods and/or determine how to store the // calculated information. // 3. Complete the display method. You need to print out the information // that was input or calculated as needed for this employee type. // -------------------------------------------------------------------------- public void calculateNetWeekly(){ }//end of calculateNetWeekly public void display(){ System.out.println("Calculating Pay for Employee"); }//end of display public void getInput(){ }//end of getInput /** * This method reads an integer and * returns that integer to the caller of this method. */ private int readInteger() { int temp = 0; Scanner scanner = new Scanner(System.in); try { temp = scanner.nextInt(); // read integer } catch (InputMismatchException ex) { System.out.println(ex); } return temp; } // end readInteger /** * This method reads in a string and returns that string to the caller of * this method. */ private String readString() { String userInput = ""; Scanner scanner = new Scanner(System.in); userInput = scanner.nextLine(); return userInput; } // end readString }//end of class CasualEmployee
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
The get and set methods refer to getting and setting these variables:
You need them because they are private. They don't require any input from the user. The getInput method asks the user for information:
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.
Java Syntax (Toggle Plain Text)
//private variables private String destination; private String employeeID; //ID for the employee private String employeeName; //Name for the employee private int hourlyRate; //the amount in dollors for employee private int numberHours; //number of hours worked each day private String origin; 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.
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.
![]() |
Other Threads in the Java Forum
- Previous Thread: Get desktop rectangle
- Next Thread: defining objects, variables and checking validations for java
| Thread Tools | Search this Thread |
3d 6 @param affinetransform android api applet application arc array arrays automation binary bluetooth bold byte c++ chat class client code color compare component coordinates database detection doctype eclipse educational error file fractal froglogic game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide ideas image ingres input integer internet intersect j2me java java.xls javaexcel javaprojects jni jpanel jtextarea julia keytool keyword linux list loop map method methods mobile netbeans newbie nextline object pong problem producer program programming project projectideas read recursion recursive replaysolutions rim scanner sell server set size sms sort sql string swing terminal threads tree web websites windows






