Hello, I could really use some expertise on fixing my code. My CountFamilies cod
ID: 3692851 • Letter: H
Question
Hello, I could really use some expertise on fixing my code. My CountFamilies code does not give a result. Instead it keeps giving me a zero amount on the families that have less than 10% of maximum income. Please help on the code to produce an amount of families making less than 10% of the maximum income. Here is the requirement; Write a program in a class CountFamiles that counts the number of familes whose income is below a certain value. Read an integer k from the keyboard and then create an array of double values of size k. Read k values representing family income from the keyboard and place them into the array. Find the maximum income among these values. Then count the families that make less than 10 percent of this maximum income. Display this count and the incomes of these families. Here is my code; package countfamiles; import java.util.Scanner; public class CountFamiles { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Please enter the number of families: "); int k = keyboard.nextInt(); double[] income = new double[k]; double maxIncome = 0; for (int i=0; i maxIncome) maxIncome = income[i]; } int lowIncomeCount=0; for (int i=0; i
Explanation / Answer
CountFamiles.java
import java.util.Scanner;
public class CountFamiles {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the number of families: ");
int k = keyboard.nextInt();
double[] income = new double[k];
double maxIncome = 0;
System.out.println("Please enter all families incomes");
for (int i=0; i < k; i++)
income[i] = keyboard.nextDouble();
maxIncome = findMaxIncome(income);
System.out.println("The maximum income of these families "+maxIncome);
double tenPercent = (maxIncome * 10)/100;
System.out.println("Ten percentage of Max Income is "+tenPercent);
int lowIncomeCount = 0;
System.out.println("Less than Ten percentage of Max Income families are : ");
for(int i=0; i<income.length; i++){
if(income[i] < tenPercent){
System.out.println(income[i]);
lowIncomeCount++;
}
}
System.out.println("The number of families whose income less than 10 Percentage of Max Income is :"+lowIncomeCount);
}
public static double findMaxIncome(double income[]){
double max = income[0];
for(int i=0; i<income.length; i++){
if(max < income[i])
max = income[i];
}
return max;
}
}
Output:
Please enter the number of families: 5
Please enter all families incomes
100
4
6
34
67
The maximum income of these families 100.0
Ten percentage of Max Income is 10.0
Less than Ten percentage of Max Income families are :
4.0
6.0
The number of families whose income less than 10 Percentage of Max Income is :2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.