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
bartleby

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 12, Problem 1E

Repeat Exercise 2 in Chapter 7, but use an instance of ArrayList instead of an array. Do not read the number of values, but continue to read values until the user enters a negative value.

Expert Solution & Answer
Check Mark

Explanation of Solution

Program Plan:

  • Import required package.
  • Define “CountFamiles”class.
    • Define main function.
      • Create object for scanner class.
      • Create array “incomeArr” by using “ArrayList”.
      • Display the prompt statement.
      • Assign “additionalValues” to “true”.
      • Initializes required variable.
      • Read input of income from user up to the value of “additionalValues” is “false”.
        • Display prompt statement for each family.
        • Read each family income from user and store it to “incomeValues”.
        • If “incomeValues” is less than “0”, then set “additionalValues” to “false”.
        • Otherwise, add the “incomeValues” to array “incomeArr”.
        • Increment the index.
      • Compute maximum income using “for” loop.
        • If “incomeArr.get(i)” is greater than maximum income, then assign maximum income to “incomeArr.get(i)”.
      • Display maximum income.
      • Display “10” percent of maximum income by “0.1 * maximumIncome”.
      • Initializes the count value to “0”.
      • Compute the count of families with incomes less than “10” percentage of maximum income using “for” loop.
        • Check condition. If value of “incomeArr.get(i)” is less than “0.1 * maximumIncome”, then increment count value and display the family income with less than “10” percentage of income by family wise.
      • Finally display the count of families.

Program:

The below java program is used to counts the number of families with less than “10” percent of maximum income using array.

“CountFamiles.java”

//Import required package

import java.util.*;

//Define "CountFamiles" class

public class CountFamiles

{

    //Define main function

    public static void main(String[] args)

    {

        //Create object for scanner class

        Scanner input = new Scanner(System.in);

/* Create array "incomeArr" by using "ArrayList" */

ArrayList<Double> incomeArr = new ArrayList<Double>();

        //Display the prompt statement

System.out.println("Enter the income for each family. Use a negative value to indicate the end ");

        //Assign "additionalValues" to "true"

        boolean additionalValues = true;

        //Initializes required variable

        int i = 0;

/* Read input of income from user upto the value of "additionalValues" is "false" */

        while(additionalValues)

        {

//Display prompt statement for each family

System.out.print("Enter Income for family " + (i +1) + ": ");

/* Read each family income from user and store it to "incomeValues" */

double incomeValues = input.nextDouble();

/* If "incomeValues" is less than "0", then */

            if(incomeValues < 0)

                /* Set "additionalValues" to "false" */

                additionalValues = false;

            /* Otherwise */

            else

/* Add the "incomeValues" to array "incomeArr" */

                incomeArr.add(incomeValues);

            //Increment the index

            i++;

        }

        //Compute maximum income using "for" loop

        double maximumIncome = incomeArr.get(0);

        for(i  = 0; i < incomeArr.size(); i ++)

        {

/* If incomeArr.get[i] is greater than maximum income, then */

            if(incomeArr.get(i) > maximumIncome)

//Assign maximum income to "incomeArr.get(i)"

                maximumIncome = incomeArr.get(i);

        }

        //Display maximum income

System.out.println("The maximum income is: " + maximumIncome);

        //Display 10 percent of maximum income

System.out.println("10% of maximum income is: " + (0.1 * maximumIncome));

        //Display prompt statement

System.out.println("\nDisplaying all families with incomes less than 10% of the maximum income income");

        //Initializes the count value to "0"

        int countValue = 0;

//Compute the count of families with incomes less than 10% of maximum income

        for( i=0; i<incomeArr.size(); i++)

        {

            //Check condition

if(incomeArr.get(i) < (0.1 * maximumIncome))

            {

                //Increment count value

                countValue++;

/* Display the family income with less than 10 percentage of income by family wise */

System.out.println("Family " + (i+1) + " had income " + incomeArr.get(i));

            }

        }

        //Display count of families

System.out.println("Count of families with 10% of maximum income is: " + countValue);

    }

}

Sample Output

Output:

Enter the income for each family. Use a negative value to indicate the end

Enter Income for family 1: 10000

Enter Income for family 2: 48000

Enter Income for family 3: 20000

Enter Income for family 4: 12000

Enter Income for family 5: 300000

Enter Income for family 6: 40000

Enter Income for family 7: -1

The maximum income is: 300000.0

10% of maximum income is: 30000.0

Displaying all families with incomes less than 10% of the maximum income

Family 1 had income 10000.0

Family 3 had income 20000.0

Family 4 had income 12000.0

Count of families with 10% of maximum income is: 3

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
(1 point) By dragging statements from the left column to the right column below, give a proof by induction of the following statement: an = = 9" - 1 is a solution to the recurrence relation an = 9an-18 with ao = : 0. The correct proof will use 8 of the statements below. Statements to choose from: Note that a₁ = 9a0 + 8. Now assume that P(n) is true for all n ≥ 0. Your Proof: Put chosen statements in order in this column and press the Submit Answers button. Let P(n) be the predicate, "a = 9″ – 1". απ = 90 − 1 = Note that Let P(n) be the predicate, "an 9" - 1 is a solution to the recurrence relation an = 9an-1 +8 with ao = 0." - Now assume that P(k + 1) is true. Thus P(k) is true for all k. Thus P(k+1) is true. Then ak+1 = 9ak +8, so P(k + 1) is true. = 1 − 1 = 0, as required. Then = 9k — 1. ak Now assume that P(k) is true for an arbitrary integer k ≥ 1. By the recurrence relation, we have ak+1 = ak+1 = = 9ak + 8 = 9(9k − 1) + 8 This simplifies to 9k+19+8 = 9k+1 − 1 Then 9k+1 − 1 = 9(9*…
Given the following request to an AI chatbot, which response is more accurate? (You should use Google to verify that the factual claims in each response are accurate.)   Prompt Is this true? The first recorded use of the phrase "when pigs fly" in print is in a 1616 play by Ben Jonson called "The Devil is an Ass," in which a character says, "When pigs fly, good-night!!" Response   Response A Response B Yes, that's true! The phrase "when pigs fly" indeed appears in Ben Jonson's 1616 play The Devil is an Ass and was said during the second act in a conversation between Fitzdottrel and Pug. The phrase was first used to express skepticism or disbelief, suggesting that something is as unlikely to happen as the fanciful idea of pigs flying. This usage is often cited as one of the earliest instances of the phrase in print.   The claim that the phrase "when pigs fly" was first recorded in Ben Jonson's 1616 play "The Devil is an Ass" is not accurate. There is no evidence from the…
This is for my Computer Organization & Assembly Language Class

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

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
What is an object?

Starting Out With Visual Basic (8th Edition)

Write code that tests the variable x to determine whether it is greater than 0. If x is greater than 0, the cod...

Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)

Suppose a manufacturer produces a computer chip and later discovers a flaw in its design. Suppose further that ...

Computer Science: An Overview (13th Edition) (What's New in Computer Science)

With this type of binding, the Java Virtual Machine determines at runtime which method to call, depending on th...

Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:9781337508841
Author:Carey
Publisher:Cengage
Java Math Library; Author: Alex Lee;https://www.youtube.com/watch?v=ufegX5o8uc4;License: Standard YouTube License, CC-BY