First Post Here, Question about classes and constructors.

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

First Post Here, Question about classes and constructors.

 
0
  #1
Jul 4th, 2007
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?
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

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

 
0
  #2
Jul 5th, 2007
No way I can understand so much of code without code tags..
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

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

 
0
  #3
Jul 5th, 2007
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)
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,273
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 494
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

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

 
0
  #4
Jul 5th, 2007
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
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

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

 
0
  #5
Jul 5th, 2007
Ok, let me give this a try. I will put my first class between these code tags
  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.
  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?
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

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

 
0
  #6
Jul 5th, 2007
Originally Posted by no1zson View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

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

 
0
  #7
Jul 5th, 2007
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:
  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:
  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
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

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

 
0
  #8
Jul 5th, 2007
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":
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

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

 
0
  #9
Jul 5th, 2007
Thank you for that explaination. You were 100% correct.
I appreciate you help.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

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

 
0
  #10
Jul 6th, 2007
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:
  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]
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 3470 | Replies: 22
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC