Write a class Family that has the attributes Income: a double value that is the
ID: 3806086 • Letter: W
Question
Write a class Family that has the attributes Income: a double value that is the income for the family Size: the number of people in the family and the following methods: Family(income, size): a constructor that sets the attributes isPoor(housingCost, foodCost): a method that returns true if housingCost + foodCost * size is greater than half the family income (foodCost is the average food cost for an individual, while housingCost is for the family) toString: a method that returns a string containing the information about the family
2. Write a program in a class CountPoor that counts the number of families that are considered poor. The program should read an integer k from the keyboard and then create an array of size k whose base type is Family. It should then create k objects of type Family and put them in the array, reading the income and size for each family from the keyboard. After reading an average housing cost and average food cost from the keyboard, it should display the families that are poor.
Sample run: How many families are there?
3
Enter the income and size for each of the families
Income and size for family 1
9000.0
2
Income and size for family 2
3000.0
2 Income and size for family 3
3500.0
6
Enter the average cost for housing and food
1500.0
1500.0 Displaying all families that evaluate as poor
Family 2 was considered poor: 2 people with income 3000.0
Family 3 was considered poor: 6 people with income 3500.0
The count is: 2
Explanation / Answer
Family.java
public class Family {
//Declaring instance variables
private double income;
private int size;
//Parameterized constructor
public Family(double income, int size) {
this.income = income;
this.size = size;
}
//This method will check whether the family is poor or not
public boolean isPoor(double housingCost, double foodCost) {
return ((housingCost + foodCost * size) > (income / 2));
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return size+" people with income $"+income;
}
}
____________________
CountPoor.java
import java.util.Scanner;
public class CountPoor {
public static void main(String[] args) {
//Declaring variables
int tot_families,poor_families=0,familySize = 0;
double familyIncome;
double avgHousingCost,avgFoodCost;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//getting the number of families entered by the user
System.out.print("How many families are there? ");
tot_families=sc.nextInt();
//Creating Arrays
Family fams[]=new Family[tot_families];
Family poorfams[]=new Family[tot_families];
//This for loop will get prompt the user to enter the family income and family size
System.out.println("Enter the income and size for each of the families");
for(int i=0;i<tot_families;i++)
{
System.out.print("Income and size for family "+(i+1)+":");
familyIncome=sc.nextDouble();
familySize=sc.nextInt();
/* Creating an family Object by passing the user
* inputs as arguments and populate that object into array
* */
fams[i]=new Family(familyIncome, familySize);
}
//getting the average housing and food cost
System.out.print("Enter the average cost for housing and food :");
avgHousingCost=sc.nextDouble();
avgFoodCost=sc.nextDouble();
//finding the poor families and populating them into an array
for(int i=0;i<tot_families;i++)
{
if (fams[i].isPoor(avgHousingCost, avgFoodCost)) {
poorfams[i] = fams[i];
poor_families++;
}
}
//Displaying the poor families info
System.out.println("::Displaying all families that evaluate as poor ::");
for(int i=0;i<tot_families;i++)
{
if(poorfams[i]!=null)
{
System.out.println("Family "+(i+1)+" was considered poor: "+poorfams[i].toString());
}
}
System.out.println("The count is: "+poor_families);
}
}
__________________
output:
How many families are there? 3
Enter the income and size for each of the families
Income and size for family 1:9000.0 2
Income and size for family 2:3000.0 2
Income and size for family 3:3500.0 6
Enter the average cost for housing and food :1500.0 1500.0
::Displaying all families that evaluate as poor ::
Family 2 was considered poor: 2 people with income $3000.0
Family 3 was considered poor: 6 people with income $3500.0
The count is: 2
____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.