Alexandros Stefanidis wrote:How can I get in my output the following:
Betty Tront is 31 years old, gets a 980.5 Euros salary and is married with 3 children.
What a strange requirement, combining the salaries. What happens if you add a second spouse? Do you divorce the first one?
Alexandros Stefanidis wrote:. . . Another question is to add a method setSalary in class MarriedPerson that will take as argument a MarriedPerson object named spouse and will do the following:
a) check spouse sex
b)If it is not the same with variable sex then in variable salary will be added the relevant variable of object spouse
c)if it the same nothing happens.
. . .
and in line 11 of class Person I have removed the private access to salary.
. . .
That is because you are not accessing the salary field correctly. You should be using getXXX and setXXX methods. Those will permit the changes without removing private access. You can also validate the input in a setXXX methodYou can also write an incrementSalary method which is rather similar.
Alexandros Stefanidis wrote:If I keep the salary variable as private then I get the following error:
error: salary has private access in Person
this.salary += spouse.getSalary(); //line 44 in class MarriedPerson
Campbell Ritchie wrote:Another reason for using toString is that it is the “Java® way” to do things. The toString() method is inherited from the Object class, and is therefore in every object you create. Also method calls like System.out.println() assume that toString has been overridden in the objects pass to them. You can write System.out.println(myObject) and get a useful printout if you override toString.