Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to determine the average amount households spend on housing (ren

ID: 3709259 • Letter: W

Question

Write a program to determine the average amount households spend on housing (rent or mortgage) based on their income. The U.S. Department of Housing and Urban Development's (HUD) Housing Affordability Data System (HADS) provides data samples of housing and income from across the country. Your instructor has provided you with select information from HADS in the file HADS.txt with about 59,000 lines of data. Each line in the file contains: Age Region int Built Value int Tenure int Utility Cost Income int Age of the head of household Census region Year the house was built Value of the house Rent or mortgage indicator Utility costs Monthly rent or mortgage Annual household income int int double double m only needs the Cost and Income values, but your program must Your progra in the file. The first line of the file contains header information. Your progranm readLine ) once in the beginning to get past the header read all data values should call You need to group the data by income. Each group represents a salary range of $10,000. You need to separate the households that have an income less than $10,000 from the many lines of data that show zero income. You also need to group the few households with large incomes into the t group. Your program can create an index for each group b if (income0) ) else if (income200000) )else( y: index0 index = 20; index-income10000 1;

Explanation / Answer

Code


import java.io.File;
import java.io.FileNotFoundException;
import java.text.NumberFormat;
import java.util.Scanner;

public class HouseholdsSepends {
    public static void main(String args[]) {
        int[] totalExpense = new int[21];
int[] totalCount = new int[21];
//Scanner to read file
Scanner scan = new Scanner(new File("HADS.txt"));
boolean skipHeader = true;
while(scan.hasNextLine()){
    // Read one line
String hadsLine = scan.nextLine();
if(skipHeader){
skipHeader = false;
continue;
}else{
    //Split input
String[] hadsArray = hadsLine.split(" ");
int age = Integer.parseInt(hadsArray[0]);
int region = Integer.parseInt(hadsArray[1]);
int built = Integer.parseInt(hadsArray[2]);
int value = Integer.parseInt(hadsArray[3]);
int tenure = Integer.parseInt(hadsArray[4]);
double utility = Double.parseDouble(hadsArray[5]);
double cost = Double.parseDouble(hadsArray[6]);
int income = Integer.parseInt(hadsArray[7]);
int index = 0;
// Find index to store
if(income==0){
index = 0;
totalExpense[index]+=cost;
totalCount[index]+=1;
}
else if(income>=200000){
index = 20;
totalExpense[index]+=cost;
totalCount[index]+=1;
}
else{
index = income / 10000 + 1;
totalExpense[index]+=cost;
totalCount[index]+=1;
}

}

}
//Print output
String header = "Income Group Average Housing";
System.out.println(header);
int income = 0;
for(int i=0;i<=20;i++){
double avg = totalExpense[i] / (double) totalCount[i];

String output = String.format("%d %.2f", income,avg);
System.out.println(output);
income+=10000;
}

    }
}

Output

Income Group Average Housing

0 1218.64

10000 1042.10

20000 1010.08

30000 1095.62

40000 1122.87

50000 1250.84

60000 1357.67

70000 1403.40

80000 1545.25

90000 1618.72

100000 1729.15

110000 1982.16

120000 1984.70

130000 2123.80

140000 2113.61

150000 2341.43

160000 2517.02

170000 2387.49

180000 2565.17

190000 2649.04

200000 3643.42

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote