• Post Reply Bookmark Topic Watch Topic
  • New Topic

Am I missing a constructor?  RSS feed

 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am taking a Beginning Java class and just finished up the programming assignment for this week... the code works as it should but I can't help but feel like I am missing the constructor the instructor is asking for. Could someone please take a gander and let me know how I could put a constructor in it. I tried to put one in there put could not get the calculation for the weekly pay to come out right because I initialized the values to 0.0 and it appeared to not take what I entered in the console. I think I understand how to use constructors that initialize parameters with something within the code, but not when the there is scanner input.

Here is the assignment the instructor posted:
____
Modify the Payroll Program so that it uses a class to store and retrieve the employee's name, the hourly rate, and the number of hours worked. Use a constructor to initialize the employee information, and a method within that class to calculate the weekly pay. Once stop is entered as the employee name, the application should terminate. Make sure the program maintains all the functionality required in previous assignments and your source code is readable and well documented. Use feedback you have received from the instructor to make any needed modifications.

For this part 3, a class must be used and a constructor must be created to initialize the information (name, hourly rate, hours worked). The result of this program is the same as for part 2, but this solution is more elegant because of the use of a class, constructor, and method.

You need to have 2 classes: employee and payroll. The employee class contains the employee info (name, hourly rate, hours worked), and the getName, setName, getHourlyRate, setHourlyRate, getHoursWorked, setHoursWorked, and calculatePay methods. This is a class by itself. Then you create the payroll class to use the employee class by initializing it.

____

I have two files

One is Employee.java


and the other is Payroll.java



Thanks in advance.
Amie
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well let's see your attempt at a constructor. And by the way, you are using a constructor when you do new Employee(). This is the default constructor, but nothing special happens when you call it other than an Employee being created.

So think about what a more useful constructor would do. It would not only create an Employee, but would also start your employee out with some meaningful properties.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the constructor I would put in the Employee.java file:


and I would edit line 16 in the Payroll.java file to read this:


I would do this because I don't want to put actual values in the employee instance because I want to get them from the user input.

But when I tried to convert the employee to String format and then print the employee to the console, I would get the Name back, but not the weekly pay - it would end up as $0.00
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your first code snippet is golden. (Although when dealing with doubles and ints, make sure they are lowercase until you know what uppercase does).

Amie Mac wrote:
I would edit line 16 in the Payroll.java file to read this:



Passing in those non-values achieves the same thing as not having a constructor. You want to be passing meaningful information to your constructor.

Amie Mac wrote:
I would do this because I don't want to put actual values in the employee instance because I want to get them from the user input.

Who's to say that you can't pass values to your constructor that you received from user input?
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel wrote:
Who's to say that you can't pass values to your constructor that you received from user input?


So, that's what I'm confused about. Is it possible to put in the variable names as the parameters?

Such as:


Although, I've tried this and get compiler errors...

 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amie Mac wrote:
So, that's what I'm confused about. Is it possible to put in the variable names as the parameters?

Such as:


Although, I've tried this and get compiler errors...



Yes, you can most definitely do that. Any place where you can use a literal value (e.g. "Hello", 7.34), you can use a variable instead.

You just have to make sure you refer to the variables after they're created of course.

Post your updated Payroll class with any errors or questions you may have.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the updated payroll program. I have put the variable names as the parameters, but I am not sure how to refer to the variables. I have a hard time seeing how using a constructor in this program is useful... or what I need to do different in the rest of the program to refer to the variables.



Here are the compiler errors:

C:\Users\Amie\Documents\IT215JavaProgramming\Week4\PayrollProgram\Payroll.java:16: error: cannot find symbol
Employee employee = new Employee(firstName, lastName, hourlyRate, hoursWorked);
^
symbol: variable firstName
location: class Payroll
C:\Users\Amie\Documents\IT215JavaProgramming\Week4\PayrollProgram\Payroll.java:16: error: cannot find symbol
Employee employee = new Employee(firstName, lastName, hourlyRate, hoursWorked);
^
symbol: variable lastName
location: class Payroll
C:\Users\Amie\Documents\IT215JavaProgramming\Week4\PayrollProgram\Payroll.java:16: error: cannot find symbol
Employee employee = new Employee(firstName, lastName, hourlyRate, hoursWorked);
^
symbol: variable hourlyRate
location: class Payroll
C:\Users\Amie\Documents\IT215JavaProgramming\Week4\PayrollProgram\Payroll.java:16: error: cannot find symbol
Employee employee = new Employee(firstName, lastName, hourlyRate, hoursWorked);
^
symbol: variable hoursWorked
location: class Payroll
4 errors

Tool completed with exit code 1
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler is confused because you are passing in non-existent variables to the constructor. What you need to do is first create your variables and populate them with data from user input as you did before. After you've done that, then you can pass these variables to your constructor, and the compiler won't freak out. Note that this time, you shouldn't need to call your set methods, because all that will be handled within the constructor.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. I have added in the variables so the constructor knows what they are. The program works again and I've pasted the code below. I still feel like I am missing the point of the constructor. You also stated that I shouldn't need to call the set methods... the instructor states on the assignment that we need to have a constructor to initialize the information such as name, hours worked, and hourly rate and to have the set and get methods. Is this confusing to have both in the program? I am also still lost as to how to use the constructor instead of the set methods. would I just take those lines out? If so, how would the Employee.java file store and recall the information to print it to the screen? Sorry for all the questions... all the examples I have seen put actual data in the parameters of the constructor and I haven't seen anything useful when obtaining input from the keyboard. It's hard for me to wrap my head around this.


 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amie Mac,

Probably good improvement of your code would be to decompose it into a small methods, where each method suppose to do a certain task.
Also, make method "main" only to run your program and nothing else. Please check this (<- link), where staff member explained that.

The advantages of decomposing your whole code into a smaller methods would be obvious, so you could see your program flow, and could easier identify where program pitfalls are. Also, would make your code more readable.

 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Hi Amie Mac,

Probably good improvement of your code would be to decompose it into a small methods, where each method suppose to do a certain task.
Also, make method "main" only to run your program and nothing else. Please check this (<- link), where staff member explained that.

The advantages of decomposing your whole code into a smaller methods would be obvious, so you could see your program flow, and could easier identify where program pitfalls are. Also, would make your code more readable.



Thanks, Liutauras, for this information.

Are these newer best practices for creating Java programs? I haven't seen writing the code this way at all in my textbook. The textbook is covering Java SE 7 and was last updated in 2012.
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, these are not new practices, but indeed good. When you get time, you could go through Oracle's tutorial (<- link). So you could see how to create methods.
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amie Mac wrote:Are these newer best practices for creating Java programs? I haven't seen writing the code this way at all in my textbook. The textbook is covering Java SE 7 and was last updated in 2012.

In textbooks where they may be trying to concentrate on one thing only, they may cram everything into one method. But in practice, this is not the best. A method should ideally do one thing only (get a file, total columns, etc.)
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Avoid using the \n escape, particularly in printf, unless you have been instructed to use a particular character. Use %n instead in printf.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that tip. I haven't seen %n being used in the text book. It's all been \n. May I ask what the difference is and why not to use it?
Thanks
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because different operating systems use different line ends: \r\n \n or \r, though you may never see a computer using \r because it is obsolescent. By trying to specify the exact characters (unless somebody has told you to use CRLF or CR or LF) you lose portability: a program designed for one line end might work incorrectly on a different OS.
If you use %n it picks the correct line end for your operating system. Some text books do use \n but even the Java™ Tutorials say to use %n. You are right to use printf bceause it gives much more control over the output that println, but you need to remember the %n at the end.

The real names for the characters are Campbell Ritchie Carriage Return = CR = \r and Line Feed = LF = \n. If you look on Wikipedia, you will find some even weirder line end combinations which have been used in the past.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!