Java: An Introduction to Problem Solving and Programming (8th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
Question
Book Icon
Chapter 12, Problem 2E
Program Plan Intro

Count number of poor family using “ArrayList”

Program Plan:

“Family.java”:

  • Declare required variables.
  • Define constructor for “Family” class.
    • Assign “income” to new variable “newIncome”.
    • Assign “size” to new variable “nSize”.
  • Define method “isPoor” with two arguments “housingCost” and “foodCost”.
    • This method returns “true” if the value of “housingCost + foodCost * size” is greater than half of income. Otherwise returns “false”.
  • Define method “toString” which is used to returns the given statement with “size” and “income”.

“CountPoor.java”:

  • Import required package.
  • Define “CountPoor” class.
    • Define main function.
      • Create object for scanner class.
      • Create array for “incomeValues” using “ArrayList”.
      • Display prompt statement for number of families.
      • Assign “additionalData” to “true”.
      • Set “index” to “0”.
      • Read income and size for each family using “while” loop.
        • Display prompt statement for each family.
        • If user input has next double, then
          • Read income and size of each family from user.
          • Add income values and size to “Family” class using “add” method.
        • Otherwise, assign “additionalData” to “false” and then finish reading data from user.
      • Display prompt statement for average housing cost and food cost.
      • Read the average house cost and food cost from user.
      • Initializes the count value to “0”.
      • Compute the number of poor family using “for” loop.
        • Check if the given family is poor by using “isPoor” function.
        • If it is, then increment the count value and display poor family details using “get” method.
      • Finally display the count of poor family.

Blurred answer
Students have asked these similar questions
Challenge exercise What are examples of other methods that the Arrays class provides?
What is the difference in the result of returning the words in a HashSet compared with returning them in an ArrayList?
Using JAVA, write a method that modifies an ArrayList<String>, moving all strings starting with an uppercase letter to the front, without otherwise changing the order of the elements. Write a test program to demonstrate the correct operation of the method.

Chapter 12 Solutions

Java: An Introduction to Problem Solving and Programming (8th Edition)

Ch. 12.1 - Prob. 12STQCh. 12.2 - Prob. 13STQCh. 12.2 - Prob. 14STQCh. 12.2 - Prob. 15STQCh. 12.2 - Prob. 16STQCh. 12.3 - Prob. 17STQCh. 12.3 - Prob. 18STQCh. 12.3 - Prob. 19STQCh. 12.3 - Write a definition of a method isEmpty for the...Ch. 12.3 - Prob. 21STQCh. 12.3 - Prob. 22STQCh. 12.3 - Prob. 23STQCh. 12.3 - Prob. 24STQCh. 12.3 - Redefine the method getDataAtCurrent in...Ch. 12.3 - Repeat Question 25 for the method...Ch. 12.3 - Repeat Question 25 for the method...Ch. 12.3 - Repeat Question 25 for the method...Ch. 12.4 - Revise the definition of the class ListNode in...Ch. 12.4 - Prob. 30STQCh. 12.5 - What is the purpose of the FXML file?Ch. 12.5 - Prob. 32STQCh. 12 - Repeat Exercise 2 in Chapter 7, but use an...Ch. 12 - Prob. 2ECh. 12 - Prob. 3ECh. 12 - Repeat Exercises 6 and 7 in Chapter 7, but use an...Ch. 12 - Write a static method removeDuplicates...Ch. 12 - Write a static method...Ch. 12 - Write a program that will read sentences from a...Ch. 12 - Repeat Exercise 12 in Chapter 7, but use an...Ch. 12 - Write a program that will read a text file that...Ch. 12 - Revise the class StringLinkedList in Listing 12.5...Ch. 12 - Prob. 12ECh. 12 - Write some code that will use an iterator to...Ch. 12 - Prob. 14ECh. 12 - Write some code that will use an iterator to...Ch. 12 - Prob. 17ECh. 12 - Revise the method selectionSort within the class...Ch. 12 - Repeat the previous practice program, but instead...Ch. 12 - Repeat Practice Program 1, but instead write a...Ch. 12 - Write a program that allows the user to enter an...Ch. 12 - Write a program that uses a HashMap to compute a...Ch. 12 - Write a program that creates Pet objects from data...Ch. 12 - Repeat the previous programming project, but sort...Ch. 12 - Repeat the previous programming project, but read...Ch. 12 - Prob. 9PPCh. 12 - Prob. 10PPCh. 12 - Prob. 11PPCh. 12 - Prob. 12PPCh. 12 - Prob. 13PPCh. 12 - Prob. 14PPCh. 12 - Prob. 15PP
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning