Results 1 to 6 of 6
  1. #1
    higashi is offline Member
    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    Default Problem using methods from other class

    This is a simple school assignment and I dun understand why it won't compile. I am supposed to write a program that is able to display a Staff's name, salary and total working hours. So I created a Staff superclass with StoreSupervisor and StoreAssistant as sub classes. I then created another class called StaffApp that will display the Staff's name, salary and total working hours.

    So here are the codes.

    This is the super class, Staff

    Java Code:
    public class Staff 
    {
    	String staffID;
    	int totalWorkHours;
    	
    	int getTotalWorkHours()
    	{
    		return totalWorkHours;
    	}
    	
    	void setTotalWorkHours(int totalOfWorkHour)
    	{
    		totalWorkHours = totalOfWorkHour;
    	}
    	
    	public double computeSalary(double salary)
    	{
    		
    		salary = totalWorkHours*15;
    		return salary;
    	}
    This is the sub class, StoreSupervisor

    Java Code:
    public class StoreSupervisor extends Staff
    {
    	String storeSupervisorID;
    	int totalAdminHours;
    	int totalWorkHours;
    	int totalHoursWorked;
    	double salary;
    	
    	public StoreSupervisor(String supervisorID, int workHours, int adminHours)
    	{
    		storeSupervisorID = supervisorID;
    		totalWorkHours = workHours;
    		totalAdminHours = adminHours;
    	}
    	
    	public int getWorkHours ()
    	{
    		return totalWorkHours;
    	}
    	
    	public void computeSalary() //overrides Staff computerSalary method
    	{	
    		salary = (this.totalWorkHours * 15) + (this.totalAdminHours * 20);
    	}
    	
    	void viewDetails()
    	{
    		totalHoursWorked = totalWorkHours + totalAdminHours;
    		System.out.println(storeSupervisorID);
    		System.out.println(totalAdminHours);
    		System.out.println(totalWorkHours);
    		System.out.println(salary);
    	}
    	
    	public static void main(String[] args)
    	{
    		
    	}
    	
    }
    This is the other subclass, StoreAssistant

    Java Code:
    public class StoreAssistant extends Staff
    {
    	String storeAssistantID;
    	int totalWorkHours;
    	double salary;
    	
    	public StoreAssistant(String staffID, int workHours)
    	{
    		storeAssistantID = staffID;
    		totalWorkHours = workHours;
    	}	
    	
    	public void viewDetails()
    	{	
    		System.out.println(storeAssistantID);
    		System.out.println(totalWorkHours);
    		System.out.println(salary);
    	}
    	
    }
    This is the class StaffApp.

    Java Code:
    public class StaffApp 
    {
    	public static void main (String[] args) 
    	{
    		StoreAssistant claire = new StoreAssistant("Claire001", 36);
    		StoreSupervisor madison = new StoreSupervisor("Madison003", 26, 10);
    		claire.computeSalary(); //This is the part where it won't compile
    		madison.computeSalary(); //This is the part where it won't compile
    		claire.viewDetails();
    		System.out.println();
    		madison.viewDetails();
    		}
    	}
    The problem is that Eclispe tells me that these two statement "claire.computeSalary()" and "madison.computeSalary()" just won't compile.

    I got this error.

    The method computeSalary(double) in the type StoreSupervisor is not applicable for the arguments ()

    The method computeSalary(double) in the type StoreSupervisor is not applicable for the arguments ()

    Can someone help?

  2. #2
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default Re: Problem using methods from other class

    Within your main method for your StaffApp class you declare this:
    Java Code:
     
    StoreAssistant claire = new StoreAssistant("Claire001", 36);
    Afterwards you write:
    Java Code:
    claire.computeSalary();
    Except within the StoreAssistant class there isn't a method called "computeSalary". So i believe that is one mistake. Hope this helps.

    Regards Serb.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Problem using methods from other class

    Why do you even have the computeSalary method take a parameter? Why not just let it have an empty/default parameter?

  4. #4
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Problem using methods from other class

    If you are Extending the class Staff, you don't need to reestablish the fields/methods that Staff has.

  5. #5
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Problem using methods from other class

    Here's the code for a working version. If you have any questions don't hesitate to ask!!

    Staff:
    Java Code:
    public class Staff 
    {
        String staffID;
        public int totalWorkHours;
        public double salary;
         
    //    int getTotalWorkHours()
    //    {
    //        return totalWorkHours;
    //    }
         
        void setTotalWorkHours(int totalOfWorkHour)
        {
            this.totalWorkHours = totalOfWorkHour;
        }
         
        public double computeSalary()
        {  
            this.salary = totalWorkHours*15;
            return this.salary;
        }
        
    }
    StoreSupervisor:
    Java Code:
    public class StoreSupervisor extends Staff
    {
        int totalAdminHours;
        int totalHoursWorked;
         
        public StoreSupervisor(String supervisorID, int workHours, int adminHours)
        {
            staffID = supervisorID;
            totalWorkHours = workHours;
            totalAdminHours = adminHours;
        }
         
    //    public int getWorkHours ()
    //    {
    //        return totalWorkHours;
    //    }
         
        public void computeSuperSalary()
        {
            salary = (totalWorkHours * 15) + (totalAdminHours * 20);
        }
         
        void viewDetails()
        {
            totalHoursWorked = totalWorkHours + totalAdminHours;
            System.out.println(staffID);
            System.out.println(totalAdminHours);
            System.out.println(totalWorkHours);
            System.out.println(salary);
        }
         
    //    public static void main(String[] args)
    //    {
    //         
    //    }
         
    }
    StoreAssistant:
    Java Code:
    public class StoreAssistant extends Staff
    {     
        public StoreAssistant(String sID, int workHours)
        {
            staffID = sID;
            totalWorkHours = workHours;
        }   
         
        public void viewDetails()
        {   
            System.out.println(staffID);
            System.out.println(totalWorkHours);
            System.out.println(salary);
        }
         
    }
    StaffApp:
    Java Code:
    public class StaffApp 
    {
        public static void main (String[] args) 
        {
            StoreAssistant claire = new StoreAssistant("Claire001", 36);
            StoreSupervisor madison = new StoreSupervisor("Madison003", 26, 10);
            claire.computeSalary(); //This is the part where it won't compile
            madison.computeSuperSalary(); //This is the part where it won't compile
            claire.viewDetails();
            System.out.println();
            madison.viewDetails();
            }
        }
    Some of your code was not used...so I just commented it out. Cheers
    Last edited by kraigballa; 10-13-2011 at 08:48 AM.

  6. #6
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default Re: Problem using methods from other class

    @OP - Your class Staff has a method named computeSalary which ASK FOR a double value which you named "salary"
    Java Code:
    public class Staff
    {
        String staffID;
        int totalWorkHours;
    
        int getTotalWorkHours()
        {
            return totalWorkHours;
        }
    
        void setTotalWorkHours(int totalOfWorkHour)
        {
            totalWorkHours = totalOfWorkHour;
        }
        [b]
        public double computeSalary(double salary)[/b] //Your asking for a double salary
        {
            salary = totalWorkHours*15;
            return salary;
        }
    Then, in your StaffApp class you try to call computeSalary like this
    Java Code:
    public class StaffApp
    {
        public static void main (String[] args)
        {
            ...
            claire.computeSalary();
            madison.computeSalary();
            ...
         }
    }

Similar Threads

  1. Accessing methods in a different class
    By syle_q in forum New To Java
    Replies: 6
    Last Post: 03-30-2011, 07:39 PM
  2. Methods in Object class
    By Navatha in forum New To Java
    Replies: 1
    Last Post: 09-28-2010, 11:17 PM
  3. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 02:33 PM
  4. Concept about methods of Class
    By s021126 in forum New To Java
    Replies: 9
    Last Post: 08-25-2009, 04:23 AM
  5. Getting methods of a Class - Reflection
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-15-2007, 03:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •