# Inheritance and Constructor Problem

• 01-19-2011, 03:03 PM
iratus
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).

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         }```
• 01-19-2011, 03:14 PM
KevinWorkman
"It doesn't work" is as useful a question as "then fix it" is a useful answer.
• 01-19-2011, 03:17 PM
iratus
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 !!!
• 01-19-2011, 03:26 PM
Petr
Hi.
You need change code as follows
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; }```
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.
• 01-19-2011, 03:32 PM
KevinWorkman
Hooray spoonfeeding :-/
• 01-19-2011, 05:55 PM
iratus
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 !

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                                                                 }                 }         //}```
• 01-19-2011, 05:57 PM
KevinWorkman
What do you mean by, "But not it asks me for an exception"? Be specific.
• 01-19-2011, 08:09 PM
aspire1
How are you running the program: java Employ or java Inher?
• 01-19-2011, 11:18 PM
Junky
Quote:

Originally Posted by iratus
Implement a class Employee and the classes derived of it Worker, Employee and Manager.

Yeah, that'll work.
• 05-10-2011, 07:35 AM
JosAH
edit: Sorry, I goofed; I shouldn't touch a keyboard before I had my espresso.

Jos