import java.util.*; public class CountFamilies { public static Scanner kbd; publ
ID: 3739269 • Letter: I
Question
import java.util.*;
public class CountFamilies {
public static Scanner kbd;
public static void main(String[] args) {
kbd = new Scanner(System.in);
kbd.close();
}
/**
* Finds the maximum integer in an array of integers
* @param values The array containing the list of integers to search
* @return The largest integer found in the array
*/
public static int getMaximum(int [] values) {
return 0;
}
}
(Here is the starter code)
Write a program that counts the number of families whose income is below a certain level.
Begin your program by asking the user to enter the number of families. Store this value in a variable callednumOfFamilies.
Next, create an array whose size is numOfFamilies. After the array has been created, readnumOfFamiliesvalues, representing family incomes, from the keyboard and place them into the array.
Next, find the maximum income of all the values entered, and print this value to the screen.
Finally, count the families that make less than 10% of the maximum income. Display the income of each of these families, then display the count. Make sure you use correct grammar (i.e. 1 family, 2 families).
The following is an example of what your MIGHT see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs. The user's values are shown below in italics:
Please enter the number of families: 5
Enter an income: 8500
Enter an income: 109000
Enter an income: 49000
Enter an income: 9000
Enter an income: 67000
The maximum income is: 109000
The incomes of families making less than 10% of
the maximum are:
8500
9000
for a total of 2 families
(Another example is)
Program Requirements
You must create and use a method named getMaximum. This method should receive one parameter, which is an array of integers. This method should search the array of integers to find and return the largest number in the list.
Explanation / Answer
CountFamilies.java
import java.util.*;
public class CountFamilies {
public static Scanner kbd;
public static void main(String[] args) {
kbd = new Scanner(System.in);
System.out.println("Please enter the number of families: ");
int n = kbd.nextInt();
int values[] = new int[n];
for(int i=0;i<values.length;i++){
System.out.println("Enter an income:");
values[i]= kbd.nextInt();
}
int max = getMaximum(values);
System.out.println("The maximum income is: "+max);
System.out.println("The incomes of families making less than 10% of the maximum are:");
double lessThan10 = (max * 10)/100;
for(int i=0;i<values.length;i++){
if(lessThan10>values[i]) {
System.out.println(values[i]);
}
}
kbd.close();
}
/**
* Finds the maximum integer in an array of integers
* @param values The array containing the list of integers to search
* @return The largest integer found in the array
*/
public static int getMaximum(int [] values) {
int max = values[0];
for(int i=0;i<values.length;i++){
if(max<values[i])
max = values[i];
}
return max;
}
}
Output:
Please enter the number of families:
5
Enter an income:
8500
Enter an income:
109000
Enter an income:
49000
Enter an income:
9000
Enter an income:
67000
The maximum income is: 109000
The incomes of families making less than 10% of the maximum are:
8500
9000
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.