A. Create a class named Household that includes data fields for the number of oc
ID: 3647374 • Letter: A
Question
A. Create a class named Household that includes data fields for the number of occupants and the annual income, as well as methods to get and set each field. In addition, create a default constructor that automatically sets the occupants field to 1 and the income field to 0. Then create an application named TestHousehold that demonstrates each method works correctly.B. Create an additional overloaded constructor for the Household class you created in part A. This constructor receives an integer parameter and assigns the value to the occupants field. Add any needed statements to TestHousehold to ensure that the overloaded constructor works correctly.
C. Create a third overloaded constructor for the Household class you created in parts A and B. This constructor receives two parameters, the values of which are assigned to the occupants and income fields, respectively. Alter the TestHousehold application to demonstrate that each version of the constructor works properly.
Explanation / Answer
import java.text.DecimalFormat; public class Household { private int occ,occ1,occ3,occ4; private double income,income1,income2; DecimalFormat money = new DecimalFormat("0.00"); Household() { occ = 1; occ1 =2; occ3=7; occ4=3; income = 0.00; income1=55000.00; income2 =80000.00; System.out.println("The house before the set methods has "+ occ + " occupants and an income of $" + (money.format(income))); } public void methodThatUsesInstanceVariables(){ System.out.print("The revised house has " + occ4); System.out.println(" occupants and an income of $" + (money.format(income1))); System.out.print("The second house has " + occ1); System.out.println(" occupants and an income of $" + (money.format(income))); System.out.print("The third house has " + occ3); System.out.println(" occupants and an income of $" + (money.format(income2))); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.