Need help creating constructor (java)?
Need help creating constructor (java)?
I need some help tying this program together. I am really close to completing it. Im using NetBeans and the line “Employee employee = new Employee();” is underlined and it says that it cannot find the symbol, but i dont see where the problem is. If you also see anything else wrong please point it out so i dont have to repost hehe thank you so much
package weeklypay;
import java.util.Scanner;
public class WeeklyPay
{
public static void main(String[] args)
{
Scanner input= new Scanner (System.in);
double rate;
double hours;
double weekly;
String fname;
String lname;
String Lastname;
String Firstname;
double Rate;
double Hours;
System.out.println(“Hello!”); //Welcome Text
System.out.println(“This program computes an employees weekly pay”); //Explains what its for
do
{
System.out.print(“Please enter the employees first name:”); //Asks for employees name
fname = input.next(); //Saves next input as name
if(fname.equals(“stop”))
break;
System.out.print(“Please enter the employees last name:”); //Asks for employees name
lname = input.next(); //Saves next input as name
if(lname.equals(“stop”))
break;
System.out.print(“Please enter the hourly rate of the employeee:”); //Asks for hourly rate
hours = input.nextDouble(); //Saves next input as hours
System.out.print(“Please enter the number of hours the employee works a week:”); //Asks for number of hours worked in a week
rate = input.nextDouble(); //Saves number of hours a week
Employee employee = new Employee();
System.out.printf( “Employee Name: %s,%s,Weekly pay is $%,.2f%n”, lname, fname, employee.getWeeklyPay() );
}
while( ! (lname.equalsIgnoreCase(“stop”)) );
System.out.println(“Program has been terminated”); //Program will terminate
}
}
package weeklypay;
public class Employee
{
private String lname;
double hours;
double rate;
double weekly;
private String fname;
Employee(String Firstname,String Lastname, double Rate, double Hours)
{
fname = Firstname;
lname = Lastname;
rate = Rate;
hours = Hours;
}
private void computeWeeklyPay()
{
weekly = hours* rate;
}
public double getWeeklyPay ()
{
return weekly;
}
}
Best answer:
Simple problem, the only constructor you have takes 4 parameters. You cannot use a constructor with 0 arguments (Employee() ) because it does not exist.
Tags: constructor..., creating, HELP, java, need
Under Forum

1 Comment for Need help creating constructor (java)?
1. xaargh | June 6th, 2007 at 4:27 pm
The guy above is right. To create a new Employee, you have to either add the correct parameters:
Employee employee = new Employee(“name”, “lastname”, rate, hours)
OR create a second constructor that doesn’t require parameters, like
public Employee(){
fname = “”;
lname = “”;
rate = 0;
hours = 0;
}
(If you did this you’d also need functions to modify that data later)
Leave a Comment for Need help creating constructor (java)?
You must be logged in to post a comment.
Trackback this post | Subscribe to the comments via RSS Feed