Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

xfactor0707

macrumors newbie
Original poster
Aug 5, 2009
28
0
Alright guys in in my first semester of college and im taking a java programming class. My first project is due sunday but my first file is not correct according to netbeans. can you guys please help me find the error. im still a noob with all this programming.

Code:
public class Employee
{
    private String employeename;
    private double salary,
                    bonus,
           salaryafterbonus;

  Employee(String e, double s, double b)
    {
      employeename = e;
      salary = s ;
      bonus = b ;
    }
    public String getEmployeename()
    {
        return employeename;
    }
    public String getSalary()
    {
        return salary;
    }
    public String getBonus()
    {
        return bonus;
    }
    void calculate()
    {
        salaryafterbonus= salary + bonus;
    }
}

Im writing my test class now
 

xfactor0707

macrumors newbie
Original poster
Aug 5, 2009
28
0
/Users/ebalari56/Desktop/Picture 1.png
 

Attachments

  • Picture 1.png
    Picture 1.png
    698.6 KB · Views: 168

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
What is the type of "salary"? What is the type of "bonus"? What is the return type of "getSalary"? what is the return type of "getBonus"?

When i compile using javac from the command line, i get:
Code:
Employee.java:20: incompatible types
found   : double
required: java.lang.String
        return salary;
               ^
Employee.java:24: incompatible types
found   : double
required: java.lang.String
        return bonus;
               ^
2 errors

Should be pretty straightforward. java.lang.String has a valueOf function that takes a double, if you really want to return a java.lang.String. Otherwise, changing the return type of the functions is pretty straightforward.

-Lee

EDIT: For projects like this, you're much better off with javac and java at the command line. If you hover your mouse over the ! in Netbeans the error text should appear, but... you really don't need the power an IDE affords (at the cost of complexity) at this point.
 

xfactor0707

macrumors newbie
Original poster
Aug 5, 2009
28
0
What is the type of "salary"? What is the type of "bonus"? What is the return type of "getSalary"? what is the return type of "getBonus"?

When i compile using javac from the command line, i get:
Code:
Employee.java:20: incompatible types
found   : double
required: java.lang.String
        return salary;
               ^
Employee.java:24: incompatible types
found   : double
required: java.lang.String
        return bonus;
               ^
2 errors

Should be pretty straightforward. java.lang.String has a valueOf function that takes a double, if you really want to return a java.lang.String. Otherwise, changing the return type of the functions is pretty straightforward.

-Lee

EDIT: For projects like this, you're much better off with javac and java at the command line. If you hover your mouse over the ! in Netbeans the error text should appear, but... you really don't need the power an IDE affords (at the cost of complexity) at this point.

Im a noob to this, so i didnt understand much of what you said. So does this mean i have to add return statements
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
There are already return statements. They are returning variables declared as doubles. The functions are declared to return String. These are not the same. Either change the return types to double, declare salary and bonus as a String, or convert salary and bonus from doubles to Strings before returning them.

-Lee
 

xfactor0707

macrumors newbie
Original poster
Aug 5, 2009
28
0
ty dude. i understand now, i just needed to change it from sting to double because i already classified it as double. So now it shows up as error free. thank you :).

Now i have another problem my test class is having some problems let me post it up.
 

Attachments

  • Picture 2.png
    Picture 2.png
    729.8 KB · Views: 80

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Are you reading the errors being produced? They should point you in the right direction.
el calculate();
Is a syntax error. You got the rest of the calls into instance methods of Employee right... what piece of punctuation is missing?

Otherwise... java is case-sensitive. el.getsomething() is not the same as el.getSomething(). The rest of the errors seem related to this.

-Lee
 

sanPietro98

macrumors 6502a
May 30, 2008
642
1
28.416834,-81.581214
Alright guys in in my first semester of college and im taking a java programming class. My first project is due sunday but my first file is not correct according to netbeans. can you guys please help me find the error. im still a noob with all this programming.

Code:
    public String getSalary()
    {
        return salary;
    }
    public String getBonus()
    {
        return bonus;
    }
    void calculate()
    {
        salaryafterbonus= salary + bonus;
    }

salary is a double not a String. So your getSalary() method should return a double not a string.

Same for your getBonus() method.

in your calculate method, you are using a variable that isn't declared (salaryafterbonus). Make it a private double.
 

xfactor0707

macrumors newbie
Original poster
Aug 5, 2009
28
0
Are you reading the errors being produced? They should point you in the right direction.
el calculate();
Is a syntax error. You got the rest of the calls into instance methods of Employee right... what piece of punctuation is missing?

Otherwise... java is case-sensitive. el.getsomething() is not the same as el.getSomething(). The rest of the errors seem related to this.

-Lee


ok, so i fixed the capitalizing errors. But i dont understand what your saying with the e1 calculate. And im still getting errors on the get salaryafterbonus lines and its saying
 

Attachments

  • Picture 4.png
    Picture 4.png
    686.9 KB · Views: 73

odinsride

macrumors 65816
Apr 11, 2007
1,149
3
ok, so i fixed the capitalizing errors. But i dont understand what your saying with the e1 calculate. And im still getting errors on the get salaryafterbonus lines and its saying

um, try e1.calculate() as your call to the calculate function.
 

SilentPanda

Moderator emeritus
Oct 8, 2002
9,992
31
The Bamboo Forest
If you look at the tool tip in your screenshot, it's telling you a few things.

It seems to be aware that e1 is an employee object. This is good. But it's telling you that it can't find the method, "getSalaryafterbonus()". So the error could be in 2 places. 1, you could be typing the method name incorrectly. Is it getSalaryAfterBonus? This is what it would probably be named. If that is not the issue you need to look back in your Employee class.

2, If you go back to your Employee class, there are two normal reasons why you would be unable to use that method. The first one would be that it just doesn't exist! The second one would be that the method is labeled private or protected.

So look for these things.

Here's a "trick". After you type "e1." wait a bit. It should pop up a list of all the available methods. Most modern IDE's provide this functionality which can be quite helpful.

If you read through the tooltip it's saying "I can't find this symbol, the symbol I can't find is getSalarayafterbonus, and you are telling me it's in the Employee class but I can't see it. Also, I found your car keys."
 

xfactor0707

macrumors newbie
Original poster
Aug 5, 2009
28
0
thanks dude. now the only problem i have left is with the getsalaryafter bonus line. the error message is saying cannot find symbol, symbol: method getsalaryafterbonus, location: class employee
 

xfactor0707

macrumors newbie
Original poster
Aug 5, 2009
28
0
yea i think it might be that i have in my employee class.

Void calculate()
{salaryafterbonus= salary+bonus;
}

So im guessing the void doesnt let my test class read that statement. What can i put in place of this?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
thanks dude. now the only problem i have left is with the getsalaryafter bonus line. the error message is saying cannot find symbol, symbol: method getsalaryafterbonus, location: class employee

What is the name of this method as defined in your Employee class? It is not getsalaryafterbonus. There must be different capitalizations or something.

When you are calling EVERY OTHER method in employee, you are typing e1 or e2, some piece of punctuation, then the name of the method. With calculate, you just put a space there. So you have the name of a class, a space, then the name of a method. Just look at every other call you make, and fix this. The method being void doesn't matter, it just means it doesn't return anything. That's fine, you're not assigning the result of this.

-Lee
 

daflake

macrumors 6502a
Apr 8, 2008
920
4,329
I might be a little thick, but it looks to me like he is passing the salary and bonus as a String and then trying to add them. You can't do this if I remember correctly. I believe that you will have to convert them to int or double and then add them. After that convert back to a String and then return the value.

It has been a while and I might be wrong... ;)
 

xfactor0707

macrumors newbie
Original poster
Aug 5, 2009
28
0
sorry if i seem annoying, but i really am trying to learn. This is how both my classes look.
 

Attachments

  • Picture 1.png
    Picture 1.png
    557 KB · Views: 50
  • Picture 2.png
    Picture 2.png
    587.4 KB · Views: 94

daflake

macrumors 6502a
Apr 8, 2008
920
4,329
Screen shots don't do us a bit of good as we can't see it all.

Please post up the code and we can load it up in our IDE's to help. I still think you are having problems because you are trying to add Strings and can't do this.
 

xfactor0707

macrumors newbie
Original poster
Aug 5, 2009
28
0
Employee.java


public class Employee
{
private String employeename;
private double salary,
bonus,
salaryafterbonus;

Employee(String e, double s, double b)
{
employeename = e;
salary = s ;
bonus = b ;
}
public String getEmployeename()
{
return employeename;
}
public double getSalary()
{
return salary;
}
public double getBonus()
{
return bonus;
}
void calculate()
{
salaryafterbonus= salary + bonus;
}
}


TestEmployee.java

public class TestEmployee
{
public static void main(String arg[])
{
System.out.println(" Office Max Payroll");
System.out.println("------------ Employee ------------");
Employee e1 = new Employee("Enrique Balari",75000.9,12000);
e1.calculate();
System.out.println("Employee name: " + e1.getEmployeename());
System.out.println("Salary before bonus:$ " + e1.getSalary());
System.out.println("Bonus amount:$" + e1.getBonus());
System.out.println("Salary after bonus:$ " + e1.getSalaryafterbonus());
System.out.print("------------ Employee ------------");
Employee e2 = new Employee("Papi Chulo",50000.8,9000);
e2.calculate();
System.out.println("Employee name:" + e2.getEmployeename());
System.out.println("Salary before bonus:$" + e2.getSalary());
System.out.println("Bonus amount:$" + e2.getBonus());
System.out.println("Salary after bonus:$" + e2.getSalaryafterbonus());
}
}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
You don't have an accessor that returns your instance variable salaryafterbonus. Write getSalaryafterbonus in Employee that returns this, and you should be fine.

-Lee
 

daflake

macrumors 6502a
Apr 8, 2008
920
4,329
Two problems... First, you need to read up on the use of public and private and how they can be unique to methods and a class. Try putting the word "public" in front of your variables to allow them to cross methods in the Employee class.

Secondly, the way you are asking for the salary/bonus sum is not correct in your test class. You wouldn't use a "get" there. You already added them up in your Employee class and the answer you are seeking is sitting in the variable (salaryafterbonus). Just go get it....

System.out.println("Salary after bonus:$ " + e1.salaryafterbonus);

You were very close to be honest and I was wrong about the string. Good job!
 

xfactor0707

macrumors newbie
Original poster
Aug 5, 2009
28
0
Alright guys i fixed the problem. Thanks a million for all your guys helped i really appreciate it you this is a great forum.Hopefully someday ill be the one helping others out.

Well this is what i changed it to, to fix it


public double getSalaryafterbonus()
{
return (salary + bonus);
}



Again thanks a lot for the help. This wont be the last of me here muahahahahahaha
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.