Repeat Exercise 3 in Chapter 7, but use an instance of ArrayList instead of an array. Do

Question:

Repeat Exercise 3 in Chapter 7, but use an instance of ArrayList instead of an array. Do not read the number of families, but read data for families until the user enters the word done.


Exercise 3 in Chapter 7

Write a program in a class CountPoor that counts the number of families that are considered poor. Write and use 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

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.

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Question Posted: