I wrote this piece of code. It is giving me an error that i cannot correct.

This piece is saved the file: CE.java

public class CE
{
private String first;
private String second;
private String ssn;
private float rate;
private float sales;


public CE(String fName, String lName, String secu, float commRate, float grossSales)
{
first = fName;
second = lName;
ssn = secu;
setRate(commRate);
setSales(grossSales);
}


public CE()
{
System.out.printf("\nError");
}


public void setRate(float commRate)
{
rate = commRate;
}


public float getRate()
{
return rate;
}


public void setSales(float grossSales)
{
sales = grossSales;
}


public float getSales()
{
return sales;
}


public float earning()
{
return getSales()*getRate();
}


public String getssn()
{
return ssn;
}
}



This piece is saved in the file: BCE.java


public class BCE extends CE
{
private float baseSalary;


public BCE(String fName, String lName, String secu, float commRate, float grossSales, float base)
{
super(fName, lName, secu, commRate, grossSales);
setbase(base);
}


public BCE()
{
System.out.printf("\nError");
}


public void setbase(float base)
{
baseSalary = base;
}


public float getbase()
{
return baseSalary;
}


public float earning()
{
return getbase() + super.earning();
}
}


And this in: IT.java


public class IT
{
public static void main(String args[])
{
CE emp1 = new CE("Martin", "Sheen", "420", 0.2, 100000);
BCE emp2 = new BCE("Rachael", "King", "421", 0.2,100000, 2000);


emp1.earning();
System.out.printf("\n");
emp2.earning();
}
}

I get the following error message

IT.java:5 cannot find symbol
symbol : constructor CE(java.lang.String,java.lang.String,
java.lang.String, double, int)
location: class CE
CE emp1 = new CE(-- data here --);
^
IT.java:6 cannot find symbol
symbol : constructor CE(java.lang.String,java.lang.String,
java.lang.String, double, int, int)
location: class CE
CE emp2 = new CE(-- data here --);
^

I've saved the above thre files in a folder and run the command:

javac -file names with .java extension-

from the command prompt.

Please Help

Dani AI

Generated

Quick diagnosis: the compiler is telling you the argument types it actually sees when you call the constructor. Java treats a decimal literal like 0.2 as a double by default, so the call in your main ends up with a double where the CE constructor expects a float. correctly pointed out the literal/type mismatch; the fix is to make the argument type match the parameter type (or change the parameter type).

Three practical fixes you can apply:

  • Make the literal a float (append an f/F) so it matches the existing float parameters (this is what suggested).
  • Change the class fields and constructors to use double instead of float. Example of changing the CE signature and related fields:
private double rate;
private double sales;

public CE(String fName, String lName, String secu, double commRate, double grossSales) {
    // assign fields...
}
  • Cast the literal at the call site to float if you want to keep float in the classes.

Also note two small issues that will help while testing: compile all sources together (for example javac *.java) so the compiler can resolve dependencies, and actually print the return value of earning() — calling it without printing will produce no visible output. For example:

System.out.printf("emp1 earning: %.2f%n", emp1.earning());
System.out.printf("emp2 earning: %.2f%n", emp2.earning());

For reference on literal behavior and primitive conversions see the Java Language Specification on floating-point literals and widening conversions: Floating‑Point Literals (JLS) and Widening Primitive Conversion (JLS). Small additional tip: use double (or BigDecimal for money) for decimal arithmetic unless you need float for memory/performance constraints.

Recommended Answers

All 3 Replies

The respective carrot signs are under the new keyword.

CE emp1 = new CE("Martin", "Sheen", "420", 0.2, 100000);
public CE(String fName, String lName, String secu, float commRate, float grossSales)

When you write: 0.2 java interprets it as a double. But your constructor accepts a float. So you need to "tell" java to treat 0.2 as a float not a double:

CE emp1 = new CE("Martin", "Sheen", "420", 0.2F, 100000);
BCE emp2 = new BCE("Rachael", "King", "421", 0.2F,100000, 2000);

Thanx a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.