0

I don't understand why I keep getting errors!

public class Household { private int income; private int occupants; public void Household() { income = 41500; occupants = 2; } public void Household2(int ic, int occ) { income = ic; occupants = occ; } public void setIncome(int i) { income = i; } public void setOccupants(int o) { occupants = o; } public int getIncome() { return income; } public int getOccupants() { return occupants; } } public class TestHousehold { public static void main(String[] args) { Household house = new Household(); house.setIncome(41500); house.setOccupants(2); System.out.println("The " + house.getOccupants() + " occupants make an annual salary of " + house.getIncome() + "."); Household house1 = new Household(10000, 10); house1.setIncome(10000); house.setOccupants(10); System.out.println(house1.getOccupants() + "occupants make an annual salary of " + house1.getIncome()); } }

14th Sep 2018, 12:32 PM
Fleet
Fleet - avatar
2 odpowiedzi
+ 3
It seems like the error caused in constructor - wrong syntax You should remove return type from constructor as in public Household() and public Household(int ic,int occ) - Note that there is no void or any other return type. And the constructors nane dhould equal to the name of class. So Household2 should be changed to Household.
17th Sep 2018, 4:26 PM
Seniru
Seniru - avatar
+ 1
I think you mean something like this class Household{ private int income; private int occupants; public void setIncome(int i) { income = i; } public void setOccupants(int o) { occupants = o; } public int getIncome() { return income; } public int getOccupants() { return occupants; } } public class TestHousehold{ public static void main(String[] args) { Household house = new Household(); house.setIncome(41500); house.setOccupants(2); System.out.println("The " + house.getOccupants() + " occupants make an annual salary of " + house.getIncome() + "."); Household house1 = new Household(); house1.setIncome(10000); house1.setOccupants(10); System.out.println(house1.getOccupants() + " occupants make an annual salary of " + house1.getIncome()); } }
14th Sep 2018, 3:39 PM
JavaBobbo
JavaBobbo - avatar