This file is saved as Employee.java

// Employee.java
//Employee Class with a construuctor to initialize the first name, last .

public class Employee
{
 private String FirstName; //first name for this Employee
 private String LastName ; //last name for this Employee
 private double MonthlySalary; //Monthly salary for this Employee

 //constructor initializes FirstName and LastName with String argument and initializes MonthlySalary with double argument
 public Employee(String FN, String LN, double MS)
 {
 String FirstName  = FN; // initializes FirstName
  String LastName = LN;  //initializes LastName
 double MonthlySalary = MS; //initializes MonthySalary
  // MonthlySalary (MS); 
 }//end constructor

 //method to set the FirstName
 public void setFirstName(String FN)
 {
      FirstName  = FN; //store the course name

 }

 //method to set the LastName
 public void setLastName(String LN)
 {
      LastName = LN;
 }

 //method to set the MonthlySalary
 public void setMonthlySalary(double MS)
 {
   //if statement to make sure that monthly salary is not negative
    if(MonthlySalary >= 0)
      MonthlySalary = MS;
    else
      MonthlySalary = 0; 
 }

 //method to retrieve the FirstName
 public String getFirstName()
 {
      return FirstName;

 }//end method getFirstName

 //method to retrieve the LastName
 public String getLastName()
 {
       return LastName;
 }

  //method to retrieve the MonthlySalary
 public double getMonthlySalary()
 {
    return MonthlySalary;
 }

 //display a welcome message to the Employee user
 public void displayEmployees()
 {
  //this statement calls getFirstName, getLastName, getMonthlySalary
  System.out.printf( "%s %s makes %d monthly \n",
  getFirstName(), getLastName(), getMonthlySalary());

 }//end method displayMessage
}//end class Employee

This one is saved as EmployeeTest.java

//EmployeeTest.java
//Employee consturctor used to specify the  first and last name of two employees and their monthly and yearly pay.  

public class EmployeeTest
{
 //main method begins program execution
 public static void main( String[] args )
 {
 //create GradeBook object
 Employee worker1 = new Employee(
 " Josh", " King ", "3,265" );
 
 Employee worker2 = new Employee(
 " Sasuke", " Uchiha", "10,000");

 //display initial value of courseName for each GradeBook
 System.out.printf( " %s %s makes %d monthly \n" ,
  worker1.getFirstName(),  worker1.getLastName(),  worker1.getMonthlySalary());
  
 System.out.printf( " %s %s makes %d monthly \n" ,
  worker2.getFirstName(),  worker2.getLastName(),  worker2.getMonthlySalary());
  
 }//end main 
}//end class EmployeeTest

Errors:

2 errors found:
File: J:\Documents\Java Programming\HW2\Extra Credit\EmployeeTest.java [line: 11]
Error: J:\Documents\Java Programming\HW2\Extra Credit\EmployeeTest.java:11: cannot find symbol
symbol : constructor Employee(java.lang.String,java.lang.String,java.lang.String)
location: class Employee
File: J:\Documents\Java Programming\HW2\Extra Credit\EmployeeTest.java [line: 14]
Error: J:\Documents\Java Programming\HW2\Extra Credit\EmployeeTest.java:14: cannot find symbol
symbol : constructor Employee(java.lang.String,java.lang.String,java.lang.String)
location: class Employee

The constructor in your Employee class takes a String (first name), a String (last name) and a double (monthly salary). But in your test program you are trying to create an Employee object using 3 Strings. The error message tells you that Java cannot find a constructor for Employee that takes 3 strings.

To fix this, in your test program, take away the quotes around the salary value to make it numeric rather than String, and you also have to get rid of the commas.

Edited 6 Years Ago by kramerd: n/a

Comments
Thank You!!!

Thank You!!

is this right:

//method to set the MonthlySalary
 public void setMonthlySalary(double MS)
 {
   //if statement to make sure that monthly salary is not negative
    if(MonthlySalary >= 0)
      MonthlySalary = MS;
    else
      MonthlySalary = 0; 
 }

is supposed to set the MonthlySalary if it's not negative and if it is negative do nothing.

Edited 6 Years Ago by __avd: Added [code ] tags.

Your if statement is testing the variable MonthlySalary. I think you probably want to test MS instead?

// Employee.java
//Employee Class with a construuctor to initialize the first name, last .

public class Employee
{
 private String FirstName; //first name for this Employee
 private String LastName ; //last name for this Employee
 private double MonthlySalary; //Monthly salary for this Employee

 //constructor initializes FirstName and LastName with String argument and initializes MonthlySalary with double argument
 public Employee(String FN, String LN, double MS)
 {
 String FirstName  = FN; // initializes FirstName
  String LastName = LN;  //initializes LastName
 double MonthlySalary = MS; //initializes MonthySalary
  // MonthlySalary (MS); 
 }//end constructor

 //method to set the FirstName
 public void setFirstName(String FN)
 {
      FirstName  = FN; //store the course name

 }

 //method to set the LastName
 public void setLastName(String LN)
 {
      LastName = LN;
 }

 //method to set the MonthlySalary
 public void setMonthlySalary(double MS)
 {
   //if statement to make sure that monthly salary is not negative
    if(MS >= 0)
      MonthlySalary = MS;
  //  else
      
    //  MonthlySalary = 0; 
      //System.out.println("MonthlySalary is negative"); 
 }

 //method to retrieve the FirstName
 public String getFirstName()
 {
      return FirstName;

 }//end method getFirstName

 //method to retrieve the LastName
 public String getLastName()
 {
       return LastName;
 }

  //method to retrieve the MonthlySalary
 public double getMonthlySalary()
 {
    return MonthlySalary;
 }

 //display a welcome message to the Employee user
 public void displayEmployees()
 {
  //this statement calls getFirstName, getLastName, getMonthlySalary
  System.out.printf( "%s %s makes %d monthly \n",
  getFirstName(), getLastName(), getMonthlySalary());

 }//end method displayMessage
}//end class Employee
//EmployeeTest.java
//Employee consturctor used to specify the  first and last name of two employees and their monthly and yearly pay.  

public class EmployeeTest
{
 //main method begins program execution
 public static void main( String[] args )
 {
 //create GradeBook object
 Employee worker1 = new Employee(
 " Josh", " King ", 3265 );
 
 Employee worker2 = new Employee(
 " Sasuke", " Uchiha", 10000);

 //display initial value of courseName for each GradeBook
 System.out.printf( " %s %s makes %d monthly \n" ,
  worker1.getFirstName(),  worker1.getLastName(),  worker1.getMonthlySalary());
  
 System.out.printf( " %s %s makes %d monthly \n" ,
  worker2.getFirstName(),  worker2.getLastName(),  worker2.getMonthlySalary());
  
 }//end main 
}//end class EmployeeTest

This is what happens when i run it:

> run EmployeeTest
 null null makes java.util.IllegalFormatConversionException: d != java.lang.Double
	at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
	at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
	at java.util.Formatter$FormatSpecifier.print(Unknown Source)
	at java.util.Formatter.format(Unknown Source)
	at java.io.PrintStream.format(Unknown Source)
	at java.io.PrintStream.printf(Unknown Source)
	at EmployeeTest.main(EmployeeTest.java:18)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)

Edited 6 Years Ago by Transcendent: n/a

%d is used with printf for integer arguments. But monthly salary is a double. Use %f for floating point argument instead.

Hey, Your program crashes anywhere you had
"System.out.printf( " %s %s makes %d monthly \n" ,..."

%d represents an integer data type. Your monthly is of type double.

Also on your constructor in your Employee class.

"private String FirstName; //first name for this Employee
 private String LastName ; //last name for this Employee
 private double MonthlySalary; //Monthly salary for this Employee

 //constructor initializes FirstName and LastName with String argument and initializes MonthlySalary with double argument
 public Employee(String FN, String LN, double MS)
 {
 String FirstName  = FN; // initializes FirstName
  String LastName = LN;  //initializes LastName
 double MonthlySalary = MS; //initializes MonthySalary
  // MonthlySalary (MS); 
 }//end constructor"

Since you've declared those firstname, lastname and monthly as global variables, you don't need to re declare the type when you are setting values to those variables.

here's what I mean.

public Employee(String FN, String LN, double MS)
 {
 FirstName  = FN; // initializes FirstName
  LastName = LN;  //initializes LastName
 MonthlySalary = MS; //initializes MonthySalary
  // MonthlySalary (MS); 
 }//end constructor"

This page may be of some help.

http://download.oracle.com/javase/tutorial/java/data/numberformat.html

Edited 6 Years Ago by __avd: Added [code] tags.

Comments
Thank You

Thanks again but now the answer i get when i run it is:

> run EmployeeTest
 null null makes 0.000000 monthly 
 null null makes 0.000000 monthly 
>

haranaboy pointed out the issue that's causing your problem. You are declaring your variables FirstName, LastName and MonthlySalary twice. Take out the declarations from your constructor. Change to:

public Employee(String FN, String LN, double MS)
{
   FirstName = FN; // remove "String" from beginning of line
   LastName = LN; // same here
   MonthlySalary = MS; // remove "double" from beginning of line
}

My if statement is still not working.

public void setMonthlySalary(double MS)
{
//if statement to make sure that monthly salary is not negative
if(MonthlySalary < 0)
MonthlySalary = 0;
}

I'm trying to see if the MonthlySalary is less than 0 and if it is the MonthlySalary is 0.

but when i run it, it has negative on it.

The code you show in your post should prevent MonthlySalary from going negative. Is there more to that method? Are you doing something with MS? If so, please post the rest of the method. If not, post the code that shows the error.

// Employee.java
//Employee Class with a construuctor to initialize the first name, last .

public class Employee
{
 private String FirstName; //first name for this Employee
 private String LastName ; //last name for this Employee
 private double MonthlySalary; //Monthly salary for this Employee

 //constructor initializes FirstName and LastName with String argument and initializes MonthlySalary with double argument
 public Employee(String FN, String LN, double MS)
 {
  FirstName  = FN; // initializes FirstName
  LastName = LN;  //initializes LastName
  MonthlySalary = MS; //initializes MonthySalary
  // MonthlySalary (MS); 
 }//end constructor

 //method to set the FirstName
 public void setFirstName(String FN)
 {
      FirstName  = FN; //store the course name

 }

 //method to set the LastName
 public void setLastName(String LN)
 {
      LastName = LN;
 }

 //method to set the MonthlySalary
 public void setMonthlySalary(double MS)
 {
   //if statement to make sure that monthly salary is not negative
    if(MonthlySalary < 0)
      MonthlySalary = 0;
 }

 //method to retrieve the FirstName
 public String getFirstName()
 {
      return FirstName;

 }//end method getFirstName

 //method to retrieve the LastName
 public String getLastName()
 {
       return LastName;
 }

  //method to retrieve the MonthlySalary
 public double getMonthlySalary()
 {
    return MonthlySalary;
 }

 //display a welcome message to the Employee user
 public void displayEmployees()
 {
  //this statement calls getFirstName, getLastName, getMonthlySalary
  System.out.printf( "%s %s makes %.2f monthly \n",
  getFirstName(), getLastName(), getMonthlySalary());

 }//end method displayMessage
}//end class Employee
//EmployeeTest.java
//Employee consturctor used to specify the  first and last name of two employees and their monthly and yearly pay.  

public class EmployeeTest
{
  //main method begins program execution
  public static void main( String[] args )
  {
    //create Employee objects
    Employee worker1 = new Employee("Naruto", "Uzumaki", -3265 );
    
    Employee worker2 = new Employee("Sasuke", "Uchiha", 10000);
    
    
    //call displayEmployee, display first, last name of employee; their monthly, yearly and raise pay
    worker1.displayEmployees(); 
    System.out.printf( "%s %s makes $%.2f yearly \n" ,
           worker1.getFirstName(),  worker1.getLastName(),  worker1.getMonthlySalary()*12);
    
    System.out.printf("%s %S raised salary is $%.2f \n\n",  
           worker1.getFirstName(),  worker1.getLastName(),worker1.getMonthlySalary()*12*1.1); 
    
    worker2.displayEmployees(); 
    System.out.printf( "%s %s makes $%.2f yearly \n" ,
           worker2.getFirstName(),  worker2.getLastName(),  worker2.getMonthlySalary()*12);
    
    System.out.printf("%s %S raised salary is $%.2f \n", 
           worker2.getFirstName(),  worker2.getLastName(),  worker2.getMonthlySalary()*12*1.1);
    
  }//end main 
}//end class EmployeeTest

Edited 6 Years Ago by Transcendent: n/a

1. In your Employee constructor you take the input MS and set the MonthlySalary variable directly, so there is nothing preventing it from becoming negative. If you use your method to setMonthlySalary instead, then the check will be done to prevent it from becoming negative.
2. In your setMonthlySalary method, you should be checking whether MS is negative, not MonthlySalary.
3. In your setMonthlySalary method you also need to handle the case where MS is not negative.

1. In your Employee constructor you take the input MS and set the MonthlySalary variable directly, so there is nothing preventing it from becoming negative. If you use your method to setMonthlySalary instead, then the check will be done to prevent it from becoming negative.
2. In your setMonthlySalary method, you should be checking whether MS is negative, not MonthlySalary.
3. In your setMonthlySalary method you also need to handle the case where MS is not negative.

can you give me some examples?

// constructor
public Employee(String FN, String LN, double MS)
{
   FirstName  = FN;
   LastName = LN;
   MonthlySalary = MS;   // what if MS is negative here?
}
public void setMonthlySalary(double MS)
{
   if(MonthlySalary < 0)   // this should say (MS < 0)
      MonthlySalary = 0;
   // put an else here - this is the case where MS >= 0
}
This article has been dead for over six months. Start a new discussion instead.