944,131 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 3932
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 4th, 2007
0

First Post Here, Question about classes and constructors.

Expand Post »
Like many of the posts I see out here I have decided to learn Java. It is a bit more than I bargained for. From what I see every class in the world starts with a payroll program, mine is no different.
I have done ok with it up to this point, but I have been asked to take my current working program, and alter it.

import java.util.Scanner; //uses class Scanner

public class Payroll
{
public static void main(String[] args)
{
// create another Scanner to obtain name from command window
Scanner input2 = new Scanner(System.in);

char eName; // name of employee

// create Scanner to obtain input from command window
Scanner input = new Scanner(System.in);

double rate; // hourly rate of employee
double hours; // hours worked by employee

double result; // product of rate and hours

System.out.print("Enter Employee's Name or Enter STOP to Exit: "); // prompt for name
String name = input2.nextLine(); //name input from user

while (!(name).equalsIgnoreCase("STOP"))
{

System.out.print("Enter Employee's Hourly Pay Rate: "); // prompt for rate
rate = input.nextDouble(); // rate input from user.
while (rate <= 0)
{
System.out.print("Rate Must Be Greater Than Zero. Enter Rate: ");
rate = input.nextDouble(); // rate loop from user.
} // End while

System.out.print("Enter Hours Employee Worked This Week: "); // prompt for hours worked
hours = input.nextDouble(); // hours student worked this week.
while (hours <= 0)
{
System.out.print("Hours Worked Must Be Greater Than Zero. Enter Hours: ");
hours = input.nextDouble(); // Hours loop from user
} // End while

result = rate * hours; // figures students salary

System.out.print(name); // display name
System.out.printf("'s salary for the week is %c%.2f\n", '$', result); //display salary

System.out.print("Enter Employee's Name or Enter STOP to Exit: "); // internal loop prompt
name = input2.nextLine(); //name input from user

} // End While
System.out.print("Ending Program.");
}

}


I have been told I need two classes, a constructor to initialize the Employee object, and methods to compute weekly pay and such. I am confused and a bit lost at the moment.
This is what I have tried: (first class)
public class Payroll
{
public static void main(String[] args)
{
// create Employee object nextEmployee
// pass employee name to constructor
Employee nextEmployee = new Employee("stupid");

nextEmployee.displayMessage(); // display message
}
} // end class Payroll

and (second class)

import java.util.Scanner; //uses class Scanner

public class Employee
{
private String employeeName; // name of employee

// constructor inititalizes employeeName
public Employee(String eName)
{
employeeName = eName; // initializes employeeName
} // end constructor

// method to set the employee's name
public void setEmployeeName(String eName)

{
employeeName = eName; // store the employee name
} // end method setEmployeeName

// method to retrieve the course name
public String getEmployeeName()
{
return employeeName;
} // end method getEmployeeName

// display message
public void displayMessage()
{
// getEmployeeName gets the name of the employee
System.out.printf("Please Enter Employee's Name: \n%s!\n\n",
getEmployeeName() );
} //end method display message

}

The resulting program compiles, but will only work if I actually put something in the parenthesis of the first class. I still need the program to prompt and store whatever name is entered. I want to get the name part working and understood before I move on to the math, but I am completely stumped.
Can anyone offer any direction?
Similar Threads
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 5th, 2007
0

Re: First Post Here, Question about classes and constructors.

No way I can understand so much of code without code tags..
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Jul 5th, 2007
0

Re: First Post Here, Question about classes and constructors.

Not sure what a "code tag" is, but I have just about every other line documented.
Does not matter, I solved it myself.
Thanks for the reply. :o)
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 5th, 2007
0

Re: First Post Here, Question about classes and constructors.

Code tag is the hash sign "#" in the toolbar when you create post or make answer to post in advanced view. This will paste [ c o d e] and [ / c o d e] in text area and you put the code you wish to post between these two tags. Also you may type these tags stright into message too.

ext thing, it will be nice if you can post solution to given problem for other people to see it
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Jul 5th, 2007
0

Re: First Post Here, Question about classes and constructors.

Ok, let me give this a try. I will put my first class between these code tags
Java Syntax (Toggle Plain Text)
  1. public class Payroll
  2. {
  3. public static void main(String[] args)
  4. {
  5. // create Employee object nextEmployee
  6. // pass employee name to constructor
  7. Employee nextEmployee = new Employee();
  8.  
  9. nextEmployee.displayMessage(); // display message
  10. }
  11. } // end class Payroll

I used that class to call the class that does all the work. I will put it between these code tags.
Java Syntax (Toggle Plain Text)
  1. mport java.util.Scanner; //uses class Scanner
  2.  
  3.  
  4. public class Employee
  5. {
  6. // Employee class has 3 fields
  7. public char eName;
  8. public double rate;
  9. public double hours;
  10. public double result;
  11.  
  12. // Employee class has one constructor
  13. public void Employee(char newEname)
  14. { // initialize name of employee
  15. eName = newEname;
  16. }
  17.  
  18. // Employee class has four methods
  19.  
  20. // get the employee name
  21. public String getEmployeeName(String eName)
  22. {
  23. return eName;
  24. }
  25.  
  26. // display message
  27. public void displayMessage()
  28. {
  29. Scanner newEname = new Scanner(System.in);
  30. System.out.print("Enter Name: ");
  31. String name = newEname.nextLine();
  32.  
  33. while (!(name).equalsIgnoreCase("STOP"))
  34.  
  35. {
  36. Scanner input = new Scanner(System.in);
  37.  
  38. System.out.print("Enter Employee's Hourly Pay Rate: "); // prompt for rate
  39. rate = input.nextDouble(); // rate input from user.
  40. while (rate <= 0)
  41. {
  42. System.out.print("Rate Must Be Greater Than Zero. Enter Rate: ");
  43. rate = input.nextDouble(); // rate loop from user.
  44. } // End while
  45.  
  46. System.out.print("Enter Hours Employee Worked This Week: "); // prompt for hours worked
  47. hours = input.nextDouble(); // hours student worked this week.
  48. while (hours <= 0)
  49. {
  50. System.out.print("Hours Worked Must Be Greater Than Zero. Enter Hours: ");
  51. hours = input.nextDouble(); // Hours loop from user
  52. } // End while
  53.  
  54. result = rate * hours; // figures students salary
  55.  
  56. System.out.print(name); // display name
  57. System.out.printf("'s salary for the week is %c%.2f\n", '$', result); //display salary
  58.  
  59. System.out.print("Enter Employee's Name or Enter STOP to Exit: "); // internal loop prompt
  60. name = newEname.nextLine(); //name input from user
  61.  
  62. } // End While
  63. System.out.print("Ending Program.");
  64. }
  65. }

Proper use of code tags? Or should I place them throughout my code, perhaps at each method or something?

The only thing I am thinking about it cleaning up this code somewhat. Separating it in to more methods, using some set and get statements for rate, hours, and all that so I can place up in the main class. Is that good technique? Is it even possible without creating more sub classes?
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 5th, 2007
0

Re: First Post Here, Question about classes and constructors.

Click to Expand / Collapse  Quote originally posted by no1zson ...
Proper use of code tags? Or should I place them throughout my code, perhaps at each method or something?

The only thing I am thinking about it cleaning up this code somewhat. Separating it in to more methods, using some set and get statements for rate, hours, and all that so I can place up in the main class. Is that good technique? Is it even possible without creating more sub classes?
Yes, those code tags are fine You don't need to break out all of the methods, code tags just preserve the formatting so indentation and such remains visible and the blocks are easily seen.

You could certainly add methods to get and set other properties on your Employee, you don't need any subclasses for that. If you add getters and setters for those properties, make the variables private to the class.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 5th, 2007
0

Re: First Post Here, Question about classes and constructors.

Can someone explain this? It is my understand that a constructor has no return type, not even void. So in my employee class I removed the "void" from what I had labeled as a constructor, thinking in reality it was just another method
So it then looked like this:
Java Syntax (Toggle Plain Text)
  1. public Employee(char newEname)
  2. { // initialize name of employee
  3. eName = newEname;
  4. }

It still compiled fine so I thought I was good.
I went back to recomplie my main class just to be safe, and now it no longer works. I receive this message:
Java Syntax (Toggle Plain Text)
  1. Payroll.java:7: cannot find symbol
  2. symbol : constructor Employee()
  3. location: class Employee
  4. Employee nextEmployee = new Employee();

I do not understand. I have played with everything in this line of code I can think of, but with no void in the Employee class, the Payroll class will not run. I put void back in and everything is great.
???
Last edited by no1zson; Jul 5th, 2007 at 1:31 pm. Reason: incomplete word
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 5th, 2007
0

Re: First Post Here, Question about classes and constructors.

Actually, the constructor does have a return type - it is an object instance of your class. That is it's entire purpose. Your first "constructor":
Java Syntax (Toggle Plain Text)
  1. public void Employee(char newEname)
  2. { // initialize name of employee
  3. eName = newEname;
  4. }
wasn't really a constructor. It was a method called Employee. The reason your code worked previously is that when no constructor is specified at all, the compiler will provide a default no-argument constructor for the class. If you specify a constructor, as you did by removing the return type, you no longer had the default Employee() constructor and so it failed. If you want to create the employee with no parameter to the constructor, you will need to give it a default constructor like so:
Java Syntax (Toggle Plain Text)
  1. public Employee(){
  2. // whatever you need to do to set up the object
  3. }
Actually though, you probably want the main program to obtain the name of the employee (as you do in the displayMessage() method) and pass that name to your constructor with the name parameter. The name also should be a string, since char will only hold a single character.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 5th, 2007
0

Re: First Post Here, Question about classes and constructors.

Thank you for that explaination. You were 100% correct.
I appreciate you help.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 6th, 2007
0

Re: First Post Here, Question about classes and constructors.

Hey guys, I am trying something different to try and clean this up, but I must be missing something. I am trying to use set and get commands in place of what I have above, so I have changed (starting with employee name) my code to this:
Java Syntax (Toggle Plain Text)
  1. import java.util.Scanner; //uses class Scanner
  2.  
  3.  
  4. public class Employee
  5. {
  6. // Employee class has 4 fields
  7. public char eName;
  8. public double rate;
  9. public double hours;
  10. public double result;
  11.  
  12. // Employee class has one constructor
  13. public Employee()
  14.  
  15. { // initialize name of employee
  16. employeeName = "";
  17. }
  18. // sets values
  19. public void setEmployeeName(String eName)
  20. {
  21. employeeName = eName;
  22. }
  23.  
  24. // get the employee name
  25. public String getEmployeeName()
  26. {
  27. return (eName);
  28. }
  29.  
  30. // display message
  31. public void displayMessage()
  32. {
  33. Scanner newEname = new Scanner(System.in);
  34. System.out.print("Enter Name: ");
  35. String name = newEname.nextLine();
  36.  
  37. while (!(name).equalsIgnoreCase("STOP"))
  38.  
  39. {
  40. Scanner input = new Scanner(System.in);
  41.  
  42. System.out.print("Enter Employee's Hourly Pay Rate: "); // prompt for rate
  43. rate = input.nextDouble(); // rate input from user.
  44. while (rate <= 0)
  45. {
  46. System.out.print("Rate Must Be Greater Than Zero. Enter Rate: ");
  47. rate = input.nextDouble(); // rate loop from user.
  48. } // End while
  49.  
  50. System.out.print("Enter Hours Employee Worked This Week: "); // prompt for hours worked
  51. hours = input.nextDouble(); // hours student worked this week.
  52. while (hours <= 0)
  53. {
  54. System.out.print("Hours Worked Must Be Greater Than Zero. Enter Hours: ");
  55. hours = input.nextDouble(); // Hours loop from user
  56. } // End while
  57.  
  58. result = rate * hours; // figures students salary
  59.  
  60. System.out.print(name); // display name
  61. System.out.printf("'s salary for the week is %c%.2f\n", '$', result); //display salary
  62.  
  63. System.out.print("Enter Employee's Name or Enter STOP to Exit: "); // internal loop prompt
  64. name = newEname.nextLine(); //name input from user
  65.  
  66. } // End While
  67. System.out.print("Ending Program.");
  68. }
  69. }

Now I get the following 3 errors no matter what I try. I think something is wrong with my "employeeName, or eName, but I am at a loss as to what it is.
Any help would be appreciated.
[CODE][Employee.java:15: cannot find symbol
symbol : variable employeeName
location: class Employee
employeeName = "";
^
Employee.java:19: cannot find symbol
symbol : variable employeeName
location: class Employee
employeeName = eName;
^
Employee.java:25: incompatible types
found : char
required: java.lang.String
return eName;
^/CODE]
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Why my Netbean 5.0 is not MDI Application
Next Thread in Java Forum Timeline: Phone book application





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC