Concept explainers
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.
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.
- Define main function.
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);
}
}
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?
Chapter 12 Solutions
Java: An Introduction to Problem Solving and Programming (7th Edition)
Additional Engineering Textbook Solutions
Problem Solving with C++ (10th Edition)
Starting Out With Visual Basic (8th Edition)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
SURVEY OF OPERATING SYSTEMS
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
- 4. def modify_data(x, my_list): X = X + 1 my_list.append(x) print(f"Inside the function: x = {x}, my_list = {my_list}") num = 5 numbers = [1, 2, 3] modify_data(num, numbers) print(f"Outside the function: num = {num}, my_list = {numbers}") Classe Classe that lin Thus, A pro is ref inter Ever dict The The output: Inside the function:? Outside the function:?arrow_forwardpython Tasks 5 • Task 1: Building a Library Management system. Write a Book class and a function to filter books by publication year. • Task 2: Create a Person class with name and age attributes, and calculate the average age of a list of people Task 3: Building a Movie Collection system. Each movie has a title, a genre, and a rating. Write a function to filter movies based on a minimum rating. ⚫ Task 4: Find Young Animals. Create an Animal class with name, species, and age attributes, and track the animals' ages to know which ones are still young. • Task 5(homework): In a store's inventory system, you want to apply discounts to products and filter those with prices above a specified amount. 27/04/1446arrow_forwardOf the five primary components of an information system (hardware, software, data, people, process), which do you think is the most important to the success of a business organization? Part A - Define each primary component of the information system. Part B - Include your perspective on why your selection is most important. Part C - Provide an example from your personal experience to support your answer.arrow_forward
- Management Information Systemsarrow_forwardQ2/find the transfer function C/R for the system shown in the figure Re དarrow_forwardPlease original work select a topic related to architectures or infrastructures (Data Lakehouse Architecture). Discussing how you would implement your chosen topic in a data warehouse project Please cite in text references and add weblinksarrow_forward
- Please original work What topic would be related to architectures or infrastructures. How you would implement your chosen topic in a data warehouse project. Please cite in text references and add weblinksarrow_forwardWhat is cloud computing and why do we use it? Give one of your friends with your answer.arrow_forwardWhat are triggers and how do you invoke them on demand? Give one reference with your answer.arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT