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 (8th 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)
- Consider a packet of length L that begins at end system A and travels over three links to a destination end system. These three links are connected by two packet switches. Let di, si, and Ri denote the length, propagation speed, and the transmission rate of link i, for i = 1, 2, 3. The packet switch delays each packet by dproc. Assuming no queuing delays, in terms of di, si, Ri, (i = 1, 2, 3), and L, what is the total end-to-end delay for the packet? Suppose now the packet is 1,500 bytes, the propagation speed on all three links is 2.5 * 10^8 m/s, the transmission rates of all three links are 2.5 Mbps, the packet switch processing delay is 3 msec, the length of the first link is 5,000 km, the length of the second link is 4,000 km, and the length of the last link is 1,000 km. For these values, what is the end-to-end delay?arrow_forwardhow to know the weight to data and data to weight also weight by infomraion gain in rapid miner , between this flow diagram retrieve then selecte attrbuite then set role and split data and decision tree and apply model and peformance ,please show how the operators should be connected:arrow_forwardusing rapid miner how to creat decison trea for all attribute and another one with delete one or more of them also how i know the weight of each attribute and what that mean in impact the resultarrow_forward
- Q.1. Architecture performance [10 marks] Answer A certain microprocessor requires either 2, 4, or 6 machine cycles to perform various operations. ⚫ (40+g+f)% require 2 machine cycles, ⚫ (30-g) % require 4 machine cycles, and ⚫ (30-f)% require 6 machine cycles. (a) What is the average number of machine cycles per instruction for this microprocessor? Answer (b) What is the clock rate (machine cycles per second) required for this microprocessor to be a "1000 MIPS" processor? Answer (c) Suppose that 35% of the instructions require retrieving an operand from memory which needs an extra 8 machine cycles. What is the average number of machine cycles per instruction, including the instructions that fetch operands from memory?arrow_forwardQ.2. Architecture performance [25 marks] Consider two different implementations, M1 and M2, of the same instruction set. M1 has a clock rate of 2 GHz and M2 has a clock rate of 3.3 GHz. There are two classes of instructions with the following CPIs: Class A CPI for M1 CPI for M2 2.f 1.g B 5 3 C 6 4 Note that the dots in 2 fand 1.g indicate decimal points and not multiplication. a) What are the peak MIPS performances for both machines? b) Which implementation is faster, if half the instructions executed in a certain program are from class A, while the rest are divided equally among classes B and C. c) What speedup factor for the execution of class-A instructions would lead to 20% overall speedup? d) What is the maximum possible speedup that can be achieved by only improving the execution of class-A instructions? Explain why. e) What is the clock rate required for microprocessor M1 to be a "1000 MIPS" (not peak MIPS) processor?arrow_forwardPLEASE SOLVE STEP BY STEP WITHOUT ARTIFICIAL INTELLIGENCE OR CHATGPT I don't understand why you use chatgpt, if I wanted to I would do it myself, I need to learn from you, not from being a d amn robot. SOLVE STEP BY STEP I WANT THE DIAGRAM PERFECTLY IN SIMULINKarrow_forward
- I need to develop and run a program that prompts the user to enter a positive integer n, and then calculate the value of n factorial n! = multiplication of all integers between 1 and n, and print the value n! on the screen. This is for C*.arrow_forwardI need to develop and run a C* program to sum up integers from 1 to 100, and print out the sum value on the screen. Can someone help please?arrow_forwardGiven the schema below for the widgetshop, provide a schema diagram. Schema name Attributes Widget-schema Customer-schema (stocknum, manufacturer, description, weight, price, inventory) (custnum, name, address) Purchased-schema (custnum, stocknum, pdate) Requestedby-schema (stocknum, custnum) Newitem-schema (stocknum, manufacturer, description) Employee-schema (ssn, name, address, salary) You can remove the Newitem-schema (red).arrow_forward
- True or False: Given the sets F and G with F being an element of G, is it always ture that P(F) is an element of P(G)? (P(F) and P(G) mean power sets). Why?arrow_forwardCan you please simplify (the domain is not empty) ∃xF (x) → ¬∃x(F (x) ∨ ¬G(x)). Foarrow_forwardHistogramUse par(mfrow=c(2,2)) and output 4 plots with different argument settings.arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,Programming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage LearningNp Ms Office 365/Excel 2016 I NtermedComputer ScienceISBN:9781337508841Author:CareyPublisher:Cengage