Results 1 to 6 of 6
  1. #1
    mackavelirip is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default HW assignment help

    Here's the HW assignment

    Create a Faculty subclass of Employee which has the additional data of rank. Rank could be one of Assistant Professor, Associate Professor, or Professor. Create a Staff subclass of Employee which has the additional data of Category (Full or Part time). Provide suitable constructors. Each of these sub classes also have a change_status() method that changes (for example by promoting) the rank or category respectively. Add a change_status() method to the Employee base class that simply outputs a message “Cannot change this Employee’s status”.

    Demonstrate dynamic binding in your main program. Declare an array of Employees (compile time type) but assign different subclasses to each array element (run time type). Show that get_info() and change_status() uses the methods determined by their run time types and displays all the relevant data.
    The first part of the homework assignment was to create an object employee that has a name and salary and change in salary. I did that now the assignment above is the second part.

    Here is the code Ive done so far.

    Java Code:
    import java.util.*;
    
    public class Employee
    {
    	String name;
    	double salary;
    	
    	static Scanner console = new Scanner(System.in);
    	
    	//constructor
    	Employee(String n, double sal)
    	{
    		name = n;
    		salary = sal;
    	}
    	
    	class Faculty extends Employee
    	{
    	
    		int rank;
    	
    		Faculty(int num)
    		{
    			rank = num;
    		}
    		
    		void change_status()
    		{
    			
    			System.out.println("1. Professor");
    			System.out.println("2. Assistant Professor");
    			System.out.println("3. Associate Professor");
    			System.out.println("Enter the number that corresponds with your employees new job title.");
    			rank = console.nextInt();
    			
    			switch(rank)
    			{
    				case 1:
    				
    				System.out.println("Your employee is now a Professor");
    				break;
    				
    				case 2:
    
    				System.out.println("Your employee is now an Assistant Professor.");
    				break;
    				
    				case 3:
    				
    				System.out.println("Your employee is now an Associate Professor.");
    				break;
    				
    				default:
    				System.out.println("Invalid choice, please re-enter");
    				rank = console.nextInt();
    
    			}
    		}
    			
    	}
    	
    	class Staff extends Employee
    	{
    	
    		int category;
    		
    		Staff(int num1)
    		{
    			category = num1;
    		}
    		
    		void change_status()
    		{
    			
    			System.out.println("1. Full-time");
    			System.out.println("2. Part-time");
    			System.out.println("Enter your employees category with the corresponding number.");
    			category = console.nextInt();
    			
    			switch(category)
    			{
    				case 1:
    				
    				System.out.println("Your employee is full-time");
    				break;
    				
    				case 2:
    				
    				System.out.println("Your employee is part-time");
    				break;
    				
    				default:
    				System.out.println("Invalid choice, please re-enter");
    				category = console.nextInt();
    				
    			}
    		}
    			
    	}
    	
    	
    	void salary_change(double amount)
    	{ 
    		char choice;
    		System.out.println("Is your employee getting a raise[Y/N]?");
    		choice = console.next().charAt(0);
    		
    		//if its a raise then add it to the salary
    		if(choice == 'Y' || choice == 'y')
    		{
    			salary = salary + amount;
    			
    		}
    		
    		//if it's a decrease then subtract it from the salary
    		else if(choice == 'N' || choice == 'n')
    		{
    
    			salary = salary - amount;
    		}
    		else
    		{
    			System.out.println("Error, please re-enter.");
    			choice = console.next().charAt(0);
    		}
    		
    		
    	}
    	
    	public double get_info()
    	{
    		salary = console.nextDouble();
    		return salary;
    	}
    	
    	void quit()
    	{
    		salary = 0;
    	}
    	
    	void change_status()
    	{
    		System.out.println("Cannot change this employee's status.");
    	}	
    	
    	public static void main(String[] args)
    	{
    		Employee[] empAry = new Employee[5];
    		Employee emp = new Employee("Fred", 15000.00);
    		Faculty fact = new Faculty(0);
    		Staff staff = new Staff(0);
    		
    		
    		
    		char choice1, choice2;
    		double num;
    		
    		for(int count = 0; count < 5; count++)
    		{
    	
    		//gets employee name
    		System.out.println("What is the name of your employee?");
    		empAry[count].name = console.nextLine();
    		//gets employee salary
    		System.out.println("What is their salary?");
    		empAry[count].salary = empAry[count].get_info();
    		
    		
    		//gets the ammount of the raise/paycut
    		System.out.println("How much is the change in salary?");
    		num = console.nextDouble();
    		empAry[count].salary_change(num);
    		
    		//prints new salary
    		System.out.println("Name: " + empAry[count].name);
    		System.out.println("There new salary is " + empAry[count].salary);
    		System.out.println("Change in salary: " + num);
    		
    		empAry[count].change_status();
    		
    	
    		
    		
    		
    		//resets salary to 0
    		empAry[count].quit();
    		
    		}
    	
    	
    		
    		
    		
    	}
    }
    and im getting these errors.

    Employee.java:27: cannot find symbol
    symbol : constructor Employee()
    location: class Employee
    {
    ^
    Employee.java:72: cannot find symbol
    symbol : constructor Employee()
    location: class Employee
    {
    ^
    Employee.java:154: non-static variable this cannot be referenced from a static context
    Faculty fact = new Faculty(0);
    ^
    Employee.java:155: non-static variable this cannot be referenced from a static context
    Staff staff = new Staff(0);
    ^
    4 errors


    Im not sure what im doing wrong i've read through the book and everything. Im also confused on what dynamic binding is and how it differs from polymorphism. Does my code demonstrate dynamic binding or am I getting it confused with polymorphism. Any help would be appriciated thanks.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    The code you posted doesn't correspond to the errors. When seeking help, make sure that you post both the latest iteration of your code and the errors you see from the very same code. Also, remove any excess vertical white space -- it's annoying to have to scroll through and makes it more difficult to read the code.

    When writing a class, if you don't provide a constructor the compiler creates a default, no-argument constructor. Note that if you do provide a constructor this no-argument constructor is not created.

    In every constructor, the first line may be a explicit call to another constructor of the same class (using the this keyword) or of the super class (using the super keyword). If the first line is not an explicit call to another constructor, the compiler inserts a call to the super class's default (no-argument) constructor.

    You have an Employee class that doesn't have a default constructor, since you defined a constructor that takes two parameters. In the two classes that extend Employee, you have not however provided any constructor so the compiler inserts a default constructor that attempts to call the default constructor of the super class -- Employee -- but can't find one.

    db

  3. #3
    mackavelirip is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    Thanks for your reply if I understood what you said i made the changes but now get new errors. This is what I have so far.

    Java Code:
    import java.util.*;
    
    public class Employee
    {
    	String name;
    	double salary;
    	
    	static Scanner console = new Scanner(System.in);
    	
    	//constructor
    	Employee(String n, double sal)
    	{
    		name = n;
    		salary = sal;
    	}
    	
    	static class Faculty extends Employee
    	{
    	
    		int rank;
    	
    		Faculty(int num)
    		{
    			rank = num;
    		}
    		
    		void change_status()
    		{
    			int choice;
    			
    			System.out.println("1. Professor");
    			System.out.println("2. Assistant Professor");
    			System.out.println("3. Associate Professor");
    			System.out.println("Enter the number that corresponds with your employees new job title.");
    			choice = console.nextInt();
    			
    			switch(choice)
    			{
    				case 1:
    				
    				System.out.println("Your employee is now a Professor");
    				break;
    				
    				case 2:
    
    				System.out.println("Your employee is now an Assistant Professor.");
    				break;
    				
    				case 3:
    				
    				System.out.println("Your employee is now an Associate Professor.");
    				break;
    				
    				default:
    				System.out.println("Invalid choice, please re-enter");
    				choice = console.nextInt();
    			}
    		}	
    	}
    	
    	static class Staff extends Employee
    	{
    	
    		int category;
    		
    		Staff(int num1)
    		{
    			category = num1;
    		}
    		
    		void change_status()
    		{
    			int choice;
    			
    			System.out.println("1. Full-time");
    			System.out.println("2. Part-time");
    			System.out.println("Enter your employees category with the corresponding number.");
    			choice = console.nextInt();
    			
    			switch(choice)
    			{
    				case 1:
    				
    				System.out.println("Your employee is full-time");
    				break;
    				
    				case 2:
    				
    				System.out.println("Your employee is part-time");
    				break;
    				
    				default:
    				System.out.println("Invalid choice, please re-enter");
    				choice = console.nextInt();
    			}
    		}	
    	}
    	
    	void salary_change(double amount)
    	{ 
    		char choice;
    		System.out.println("Is your employee getting a raise[Y/N]?");
    		choice = console.next().charAt(0);
    		
    		//if its a raise then add it to the salary
    		if(choice == 'Y' || choice == 'y')
    		{
    			salary = salary + amount;
    			
    		}
    		
    		//if it's a decrease then subtract it from the salary
    		else if(choice == 'N' || choice == 'n')
    		{
    
    			salary = salary - amount;
    		}
    		else
    		{
    			System.out.println("Error, please re-enter.");
    			choice = console.next().charAt(0);
    		}
    		
    	}
    	
    	public double get_info()
    	{
    		salary = console.nextDouble();
    		return salary;
    	}
    	
    	void quit()
    	{
    		salary = 0;
    	}
    	
    	void change_status()
    	{
    		System.out.println("Cannot change this employee's status.");
    	}	
    	
    	public static void main(String[] args)
    	{
    		Employee[] empAry = new Employee[3];
    		Employee emp = new Employee("Fred", 1000.00);
    		Faculty fact = new Faculty("fred", 1200.00, 0);
    		Staff staff = new Staff("Fred", 12000.00, 0);
    		
    		double num;
    		
    		empAry[0] = emp;
    		empAry[1] = fact;
    		empAry[2] = staff;
    		
    		
    		//gets employee name
    		System.out.println("What is the name of your employee?");
    		emp.name = console.nextLine();
    		//gets employee salary
    		System.out.println("What is their salary?");
    		emp.salary = emp.get_info();
    		
    		
    		//gets the ammount of the raise/paycut
    		System.out.println("How much is the change in salary?");
    		num = console.nextDouble();
    		emp.salary_change(num);
    		
    		//prints new salary
    		System.out.println("Name: " + emp.name);
    		System.out.println("There new salary is " + emp.salary);
    		System.out.println("Change in salary: " + num);
    		
    		for(int count = 0; count < 3; count++)
    		{
    			empAry[count].change_status();
    		}
    		
    		//resets salary to 0
    		emp.quit();
    	
    	}		
    }
    Now these are the errors I get.


    Employee.java:27: cannot find symbol
    symbol : constructor Employee()
    location: class Employee
    {
    ^
    Employee.java:73: cannot find symbol
    symbol : constructor Employee()
    location: class Employee
    {
    ^
    Employee.java:156: cannot find symbol
    symbol : constructor Faculty(java.lang.String,double,int)
    location: class Employee.Faculty
    Faculty fact = new Faculty("fred", 1200.00, 0);
    ^
    Employee.java:157: cannot find symbol
    symbol : constructor Staff(java.lang.String,double,int)
    location: class Employee.Staff
    Staff staff = new Staff("Fred", 12000.00, 0);
    ^
    4 errors

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    when you get an error like that, please follow the line number to spot the error and if then you don't see what it is and can't correct it, all we really need is that line & probably the method that line falls in to spot the error - not your whole code as most people have lives and wont read it anyway.

    furthermore, you say that you understand what darryl told you but clearly you havent.
    Note: that the compiler error says cannot find symbol Employee()
    NOT cannot find symbol Employee(java.lang.String, double)

    In every constructor, the first line may be a explicit call to another constructor of the same class (using the this keyword) or of the super class (using the super keyword). If the first line is not an explicit call to another constructor, the compiler inserts a call to the super class's default (no-argument) constructor.

    You have an Employee class that doesn't have a default constructor, since you defined a constructor that takes two parameters. In the two classes that extend Employee, you have not however provided any constructor so the compiler inserts a default constructor that attempts to call the default constructor of the super class -- Employee -- but can't find one.
    Inheritance (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
    Last edited by ozzyman; 03-30-2011 at 11:42 PM.

  5. #5
    mackavelirip is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    Alright I looked up how to use the super method and got it to run thank you very much. I have one more question. When I run the program, for the first employee in the array everything works fine. I ask for the name, salary and change in salary and it works. Then when it goes to the next person in the array, it says whats the name of your employee but instead of letting you type something it skips it and goes straight to the salary. I was wondering why that is.

    Heres my updated code.

    Heres what the output looks like. I bolded the part where it skips the name.

    What is the name of your employee?
    Fred
    What is their salary?
    15000
    How much is the change in salary?
    5000
    Is your employee getting a raise[Y/N]?
    y
    Name: Fred
    There new salary is 20000.0
    Change in salary: 5000.0
    Cannot change this employee's status.
    What is the name of your employee? //this is where it skips
    What is their salary?
    15000
    How much is the change in salary?
    1000
    Is your employee getting a raise[Y/N]?
    n
    Name:
    There new salary is 14000.0
    Change in salary: 1000.0
    1. Professor
    2. Assistant Professor
    3. Associate Professor
    Enter the number that corresponds with your employees new job title.
    1
    Your employee is now a Professor
    What is the name of your employee? //this is where it skips
    What is their salary?
    20000
    How much is the change in salary?
    2000
    Is your employee getting a raise[Y/N]?
    y
    Name:
    There new salary is 22000.0
    Change in salary: 2000.0
    1. Full-time
    2. Part-time
    Enter your employees category with the corresponding number.
    2
    Your employee is part-time
    Here is my updated code.

    Java Code:
    import java.util.*;
    
    public class Employee
    {
        String name;
        double salary;
        
        static Scanner console = new Scanner(System.in);
        
        //constructor
        Employee(String n, double sal)
        {
            name = n;
            salary = sal;
        }
        
        static class Faculty extends Employee
        {
        
            int rank;
        
            Faculty(int num, String n, double sal)
            {
    		  		super(n, sal);
                rank = num;
            }
            
            void change_status()
            {
                int choice;
                
                System.out.println("1. Professor");
                System.out.println("2. Assistant Professor");
                System.out.println("3. Associate Professor");
                System.out.println("Enter the number that corresponds with your employees new job title.");
                choice = console.nextInt();
                
                switch(choice)
                {
                    case 1:
                    
                    System.out.println("Your employee is now a Professor");
                    break;
                    
                    case 2:
    
                    System.out.println("Your employee is now an Assistant Professor.");
                    break;
                    
                    case 3:
                    
                    System.out.println("Your employee is now an Associate Professor.");
                    break;
                    
                    default:
                    System.out.println("Invalid choice, please re-enter");
                    choice = console.nextInt();
    
                }
            }
                
        }
        
        static class Staff extends Employee
        {
        
            int category;
            
            Staff(int num1, String n, double sal)
            {
    		  		super(n,sal);
                category = num1;
            }
            
            void change_status()
            {
                int choice;
                
                System.out.println("1. Full-time");
                System.out.println("2. Part-time");
                System.out.println("Enter your employees category with the corresponding number.");
                choice = console.nextInt();
                
                switch(choice)
                {
                    case 1:
                    
                    System.out.println("Your employee is full-time");
                    break;
                    
                    case 2:
                    
                    System.out.println("Your employee is part-time");
                    break;
                    
                    default:
                    System.out.println("Invalid choice, please re-enter");
                    choice = console.nextInt();
                    
                }
            }
                
        }
        
        
        void salary_change(double amount)
        {
            char choice;
            System.out.println("Is your employee getting a raise[Y/N]?");
            choice = console.next().charAt(0);
            
            //if its a raise then add it to the salary
            if(choice == 'Y' || choice == 'y')
            {
                salary = salary + amount;
                
            }
            
            //if it's a decrease then subtract it from the salary
            else if(choice == 'N' || choice == 'n')
            {
    
                salary = salary - amount;
            }
            else
            {
                System.out.println("Error, please re-enter.");
                choice = console.next().charAt(0);
            }
            
            
        }
        
        public double get_info()
        {
    	 	salary = console.nextDouble();
          return salary;
        }
        
        void quit()
        {
            salary = 0;
        }
        
        void change_status()
        {
            System.out.println("Cannot change this employee's status.");
        }    
        
        public static void main(String[] args)
        {
            Employee[] empAry = new Employee[3];
            Employee emp = new Employee("Fred", 1000.00);
            Faculty fact = new Faculty(10, "Fred", 10000.00);
            Staff staff = new Staff(10, "Fred", 10000.00);
            
            
            double num;
            
            empAry[0] = emp;
            empAry[1] = fact;
            empAry[2] = staff;
            
            for(int count = 0; count < 3; count++)
    		  {
            //gets employee name
            System.out.println("What is the name of your employee?");
            empAry[count].name = console.nextLine();
            //gets employee salary
            System.out.println("What is their salary?");
            empAry[count].salary = empAry[count].get_info();
            
            
            //gets the ammount of the raise/paycut
            System.out.println("How much is the change in salary?");
            num = console.nextDouble();
            empAry[count].salary_change(num);
            
            //prints new salary
            System.out.println("Name: " + empAry[count].name);
            System.out.println("There new salary is " + empAry[count].salary);
            System.out.println("Change in salary: " + num);
            
            empAry[count].change_status();
    		 
            //resets salary to 0
            empAry[count].quit();
        	}
    	 	 
        }        
    
    }


    And I would like to say thanks for your guys help this is my first programming class, although its fun its very challenging to me and I appreciate you guys taking your time to help me, sorry if I am a nuisance.
    Last edited by mackavelirip; 03-31-2011 at 01:45 AM.

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

    Default

    I think your problem is that your Scanner object, console doesn't always handle the end of line token. I suggest that wherever you have console.next(), if possible change it to console.nextLine(), and wherever you have console.nextInt() or console.nextDouble(), follow this with a call to console.nextLine() just to handle and swallow the end of line token.

    For instance, change this:
    Java Code:
    num = console.nextDouble();

    to this:
    Java Code:
    num = console.nextDouble();
    console.nextLine();   // add this line

Similar Threads

  1. assignment help
    By xyknight in forum New To Java
    Replies: 6
    Last Post: 03-13-2011, 07:19 PM
  2. Need help with assignment! please
    By runawaykinms in forum Java Applets
    Replies: 2
    Last Post: 10-06-2010, 09:58 AM
  3. Need help with a assignment
    By helpMe.Java in forum New To Java
    Replies: 7
    Last Post: 06-06-2010, 04:49 PM
  4. Can somebody help me in my assignment
    By coolstruxx in forum NetBeans
    Replies: 0
    Last Post: 03-24-2009, 01:27 AM
  5. I am looking for help with an assignment
    By nanoo51969 in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 09:41 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
  •