Results 1 to 10 of 10
  1. #1
    iratus is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default Inheritance and Constructor Problem

    Hello

    I have given an exercise to write a small program as follow. I have wrote all the code but for some reason it doesn't work. Can anybody help me please ???




    Exercise
    In the accounting of a company the employee's data are saved in an array. Common data of all employees are ID, name and entry year. The workers receive wages which consist of hourly wage and overtime wage. The employees receive a salary. This is the sum from basic salary and weighting allowance. In the end, the manager receives a basic salary and a commission.
    Implement a class Employee and the classes derived of it Worker, Employee and Manager. Display a list of all data according to schema below. Use the super methods with the constructors as well as with the output methods (with super).


    Java Code:
    	class Employ {
    		String Surename;
    		String ID;
    		String Year;
      		Employ (String name, String id, String year) {
    			this.Surename = name;
    			this.ID = id;
    			this.Year = year;
    		}
    		void print() {
    			System.out.println("Name: "+ this.Surename);
    			System.out.println("ID: "+ this.ID);
    			System.out.println("Year: "+ this.Year);
    		}
    	
    
    	//2nd
    	
    	class Worker extends Employ {
    		int Payment; //Additional attribute 
    		Worker (String name, String id, String year,int payment) {
    			super(name,id,year);
    			this.Payment = payment;
    		}
    		// Overwrites the method print() in Person
    		void print() {
    			super.print();
    			System.out.println("Payment: "+ this.Payment);
    			
    			
    		}
    	} // class
    	
    	//3d
    	
    	class OfficeWorker extends Employ {
    		int Payment; //Additional attribute 
    		OfficeWorker (String name, String id, String year,int payment) {
    			super(name,id,year);
    			this.Payment = payment;
    		}
    		// Overwrites the method print() in Person
    		void print() {
    			super.print();
    			System.out.println("Payment: "+ this.Payment);
    		}
    	}
    	
    	//4d
    	
    	class Manager extends Employ {
    		int Payment; //Additional attribute 
    		Manager (String name, String id, String year,int payment) {
    			super(name,id,year);
    			this.Payment = payment;
    		}
    		// Overwrites the method print() in Person
    		void print() {
    			super.print();
    			System.out.println("Payment: "+ this.Payment);
    		}
    	} // class
    	
    	// ipologismos misthon
    	
    
    	public class pay {
    		private double b,c,e,f,h ;//Instance variables  
    		// The constructor method
    		public pay(double hourlywage,double  overtimewage,double  salary,double basicsalary,double weightingallowance,double comission) {
    			
    			
    			this.b = hourlywage;
    			this.c = overtimewage;
    			this.e = basicsalary;
    			this.f = weightingallowance;
    			this.h = comission;
    		}
    		public double wages(){
    			return b+c; 
    		}
    		public double salary(){
    		 return e+f;
    		}
    
    		public double managersalary(){
    			 return e+h;
    			}
    
    	
    	//teliko
    	
    	 public class Inher {
    			public static void main (String args[ ]) {
    				
    				pay c1 = new pay(1000,1000,0,0,0);
    				pay c2 = new pay(0,0,1500,1500,0);
    				pay c3 = new pay(0,0,1500,1500,0);
    				pay c4 = new pay(0,0,1500,1500,0);
    				pay c5 = new pay(0,0,1500,0,2000);
    
    				Employ personal[] = new Employ [5];
    				personal [0] = new Worker ("1", "Muller", "1996" + c1.wages());
    				personal [1] = new Worker ("2", "„Maier", "1993" + c2.wages());
    				personal [2] = new OfiiceWorker ("3", "Schulze","2003"+ c3.salary());
    				personal [3] = new OfiiceWorker ("5", "Tiedke", "2002"+ c4.salary());
    				personal [4] = new Manager ("4", "Huber", "2004"+ c5.managersalary());
    				
    			
    				
    				for (int i =0; i< personal.length; i++)
    					personal[i].print(); // Polymorphism
    				
    			} // main
    	 }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,941
    Rep Power
    8

    Default

    "It doesn't work" is as useful a question as "then fix it" is a useful answer.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    iratus is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Ok I am not asking for a solution and I working hours on this one !!!

    Just give me a hint what can it be wrong !!! I don't have a clue what is the problem with this one !!!

  4. #4
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Hi.
    You need change code as follows
    Java Code:
    public pay(double hourlywage,double  overtimewage,double basicsalary,double weightingallowance,double comission) {
            this.b = hourlywage;
            this.c = overtimewage;
            this.e = basicsalary;
            this.f = weightingallowance;
            this.h = comission;
    }
    Java Code:
    personal [0] = new Worker("1", "Muller", "1996",(int) c1.wages());
    personal [1] = new Worker("2", "„Maier", "1993",(int) c2.wages());
    personal [2] = new OfficeWorker("3", "Schulze","2003",(int) c3.salary());
    personal [3] = new OfficeWorker("5", "Tiedke", "2002",(int) c4.salary());
    personal [4] = new Manager("4", "Huber", "2004",(int) c5.managersalary());
    and it will works fine ). I advise you to use any IDE for example JetBrain IDE.
    Skype: petrarsentev
    http://TrackStudio.com

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,941
    Rep Power
    8

    Default

    Hooray spoonfeeding :-/
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    iratus is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Ok I did some work on it according to your advices. But not it asks me for an exception. The problem is that I haven't rearned allready about exeption !!!

    Can i make any modification to change this problem ??? (Ecllipse doesn't give me any mistake !!! )

    Thanks again !

    Java Code:
    class Employ {
    		String Surename;
    		String ID;
    		String Year;
      		Employ (String name, String id, String year) {
    			this.Surename = name;
    			this.ID = id;
    			this.Year = year;
    		}
    		void print() {
    			System.out.println("Name: "+ this.Surename);
    			System.out.println("ID: "+ this.ID);
    			System.out.println("Year: "+ this.Year);
    		}
    	
    }
    	//2nd
    	
    	class Worker extends Employ {
    		int Payment; //Additional attribute 
    		Worker (String name, String id, String year,int payment) {
    			super(name,id,year);
    			this.Payment = payment;
    		}
    		// Overwrites the method print() in Person
    		void print() {
    			super.print();
    			System.out.println("Payment: "+ this.Payment);
    			
    			
    		}
    	} // class
    	
    	//3d
    	
    	class OfficeWorker extends Employ {
    		int Payment; //Additional attribute 
    		OfficeWorker (String name, String id, String year,int payment) {
    			super(name,id,year);
    			this.Payment = payment;
    		}
    		// Overwrites the method print() in Person
    		void print() {
    			super.print();
    			System.out.println("Payment: "+ this.Payment);
    		}
    	}
    	
    	//4d
    	
    	class Manager extends Employ {
    		int Payment; //Additional attribute 
    		Manager (String name, String id, String year,int payment) {
    			super(name,id,year);
    			this.Payment = payment;
    		}
    		// Overwrites the method print() in Person
    		void print() {
    			super.print();
    			System.out.println("Payment: "+ this.Payment);
    		}
    	} // class
    	
    	// ipologismos misthon
    	
    
    	class pay {
    		private double b,c,e,f,h ;//Instance variables  
    		
    		public pay(double hourlywage,double  overtimewage,double basicsalary,double weightingallowance,double comission) {
    			
    			
    			this.b = hourlywage;
    			this.c = overtimewage;
    			this.e = basicsalary;
    			this.f = weightingallowance;
    			this.h = comission;
    		}
    		public double wages(){
    			return b+c; 
    		}
    		public double salary(){
    		 return e+f;
    		}
    
    		public double managersalary(){
    			 return e+h;
    			}
    
    	}
    	//teliko
    		
    
    	 
    			class Inher {
    				public static void main (String args[ ]) {
    
    				
    					pay c1 = new pay(1000,1000,0,0,0);
    					pay c2 = new pay(0,0,1500,1500,0);
    					pay c3 = new pay(0,0,1500,1500,0);
    					pay c4 = new pay(0,0,1500,1500,0);
    					pay c5 = new pay(0,0,1500,0,2000);
    		
    					Employ personal[] = new Employ [5];
    					personal [0] = new Worker ("1", "Muller", "1996",(int) c1.wages());
    					personal [1] = new Worker ("2", "„Maier", "1993", (int) c2.wages());
    					personal [2] = new OfficeWorker ("3", "Schulze","2003", (int) c3.salary());
    					personal [3] = new OfficeWorker ("5", "Tiedke", "2002", (int) c4.salary());
    					personal [4] = new Manager ("4", "Huber", "2004", (int) c5.managersalary());
    					
    				
    					
    					for (int i =0; i< personal.length; i++)
    						personal[i].print(); // Polymorphism
    					
    			} 
    		 }
    
    	//}

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,941
    Rep Power
    8

    Default

    What do you mean by, "But not it asks me for an exception"? Be specific.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    aspire1 is offline Member
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Default

    How are you running the program: java Employ or java Inher?

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Quote Originally Posted by iratus View Post
    Implement a class Employee and the classes derived of it Worker, Employee and Manager.
    Yeah, that'll work.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    edit: Sorry, I goofed; I shouldn't touch a keyboard before I had my espresso.

    Jos
    Last edited by JosAH; 05-10-2011 at 07:38 AM.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Inheritance Problem
    By kazumahits in forum New To Java
    Replies: 5
    Last Post: 01-11-2011, 03:46 PM
  2. Inheritance problem
    By ZuperZombie in forum Advanced Java
    Replies: 0
    Last Post: 04-02-2010, 03:55 PM
  3. Inheritance Problem
    By g2beastie in forum New To Java
    Replies: 4
    Last Post: 03-25-2010, 08:23 PM
  4. inheritance problem
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 03-10-2010, 06:01 PM
  5. Problem with Inheritance
    By KronikAlkoholik in forum New To Java
    Replies: 4
    Last Post: 08-25-2009, 12:13 AM

Posting Permissions

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