| | |
First Post Here, Question about classes and constructors.
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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 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.
I'm no ones son, unforgiven.
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
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Ok, let me give this a try. I will put my first class between these code tags
I used that class to call the class that does all the work. I will put it between these code tags.
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?
Java Syntax (Toggle Plain Text)
public class Payroll { public static void main(String[] args) { // create Employee object nextEmployee // pass employee name to constructor Employee nextEmployee = new Employee(); nextEmployee.displayMessage(); // display message } } // 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)
mport java.util.Scanner; //uses class Scanner public class Employee { // Employee class has 3 fields public char eName; public double rate; public double hours; public double result; // Employee class has one constructor public void Employee(char newEname) { // initialize name of employee eName = newEname; } // Employee class has four methods // get the employee name public String getEmployeeName(String eName) { return eName; } // display message public void displayMessage() { Scanner newEname = new Scanner(System.in); System.out.print("Enter Name: "); String name = newEname.nextLine(); while (!(name).equalsIgnoreCase("STOP")) { Scanner input = new Scanner(System.in); 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 = newEname.nextLine(); //name input from user } // End While System.out.print("Ending Program."); } }
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.
I'm no ones son, unforgiven.
•
•
•
•
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?
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.
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:
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:
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.
???
So it then looked like this:
Java Syntax (Toggle Plain Text)
public Employee(char newEname) { // initialize name of employee eName = newEname; }
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)
Payroll.java:7: cannot find symbol symbol : constructor Employee() location: class Employee 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.
I'm no ones son, unforgiven.
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": 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: 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.
Java Syntax (Toggle Plain Text)
public void Employee(char newEname) { // initialize name of employee eName = newEname; }
Java Syntax (Toggle Plain Text)
public Employee(){ // whatever you need to do to set up the object }
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:
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]
Java Syntax (Toggle Plain Text)
import java.util.Scanner; //uses class Scanner public class Employee { // Employee class has 4 fields public char eName; public double rate; public double hours; public double result; // Employee class has one constructor public Employee() { // initialize name of employee employeeName = ""; } // sets values public void setEmployeeName(String eName) { employeeName = eName; } // get the employee name public String getEmployeeName() { return (eName); } // display message public void displayMessage() { Scanner newEname = new Scanner(System.in); System.out.print("Enter Name: "); String name = newEname.nextLine(); while (!(name).equalsIgnoreCase("STOP")) { Scanner input = new Scanner(System.in); 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 = newEname.nextLine(); //name input from user } // End While System.out.print("Ending Program."); } }
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.
I'm no ones son, unforgiven.
![]() |
Similar Threads
- Need guidance as to where to post question (Windows Software)
- implemetation question regarding interfaces and classes (Java)
- exam technique? (C++)
- a question about inherite classes (Java)
- need some help please (C++)
- Finding the first number char in a string (Java)
- This is my last post (Geeks' Lounge)
- OOP char [8] to char (C++)
- Zend PHP Certification (PHP)
Other Threads in the Java Forum
- Previous Thread: Why my Netbean 5.0 is not MDI Application
- Next Thread: Phone book application
Views: 3470 | Replies: 22
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application apps arguments array arrays automation awt binary bluetooth businessintelligence busy_handler(null) card chat class classes client code collision component constructor database draw eclipse error event eventlistener exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me jar java javafx javamicroeditionuseofmotionsensor javaprojects jmf jni jpanel jtree julia link linux list loop machine map method methods mobile netbeans newbie nls number object oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms socket sort sortedmaps sql string swing test textfield threads time transfer tree unlimited webservices windows






