Java Programming

Discussion in 'Mac Programming' started by xfactor0707, Sep 17, 2009.

  1. xfactor0707 macrumors newbie

    Joined:
    Aug 5, 2009
    #1
    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
     
  2. SilentPanda Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
  3. xfactor0707 thread starter macrumors newbie

    Joined:
    Aug 5, 2009
    #3
    /Users/ebalari56/Desktop/Picture 1.png
     

    Attached Files:

  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    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.
     
  5. xfactor0707 thread starter macrumors newbie

    Joined:
    Aug 5, 2009
    #5
    Im a noob to this, so i didnt understand much of what you said. So does this mean i have to add return statements
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    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
     
  7. xfactor0707 thread starter macrumors newbie

    Joined:
    Aug 5, 2009
    #7
    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.
     

    Attached Files:

  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    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
     
  9. sanPietro98 macrumors 6502a

    sanPietro98

    Joined:
    May 30, 2008
    Location:
    28.416834,-81.581214
    #9
    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.
     
  10. xfactor0707 thread starter macrumors newbie

    Joined:
    Aug 5, 2009
    #10

    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
     

    Attached Files:

  11. odinsride macrumors 65816

    odinsride

    Joined:
    Apr 11, 2007
    #11
    um, try e1.calculate() as your call to the calculate function.
     
  12. SilentPanda Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #12
    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."
     
  13. xfactor0707 thread starter macrumors newbie

    Joined:
    Aug 5, 2009
    #13
    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
     
  14. xfactor0707 thread starter macrumors newbie

    Joined:
    Aug 5, 2009
    #14
    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?
     
  15. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #15
    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
     
  16. daflake macrumors 6502a

    Joined:
    Apr 8, 2008
    #16
    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... ;)
     
  17. xfactor0707 thread starter macrumors newbie

    Joined:
    Aug 5, 2009
    #17
    sorry if i seem annoying, but i really am trying to learn. This is how both my classes look.
     

    Attached Files:

  18. daflake macrumors 6502a

    Joined:
    Apr 8, 2008
    #18
    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.
     
  19. xfactor0707 thread starter macrumors newbie

    Joined:
    Aug 5, 2009
    #19
    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());
    }
    }
     
  20. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #20
    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
     
  21. daflake macrumors 6502a

    Joined:
    Apr 8, 2008
    #21
    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!
     
  22. daflake macrumors 6502a

    Joined:
    Apr 8, 2008
    #22
    You could do it this way as well.
     
  23. xfactor0707 thread starter macrumors newbie

    Joined:
    Aug 5, 2009
    #23
    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
     

Share This Page