ihok rpo e will get ema fnding, and hones with ?will get more anent on in the st
ID: 3699251 • Letter: I
Question
ihok rpo e will get ema fnding, and hones with ?will get more anent on in the street. This The Da on y ancil has decided toprkritue its spending on street m i en noe according tothe ea softhe people who live in the policy is an oquitable one,unlike that in the bad-old-days of apartheid treet streets t In onder to implemens this policy acormputer peogram is needed. The fine part of the program has Store the houne each house, store who lives in the house. Oace the data has been satored some basic analysis is reqaired Report the totl age of the people in each house Repert the oal age of the people ia the sreet Here what a sample ran should look Eke(wi the keyboard input shown in itaia) ow many peopie live in umber 1 s total age the street ha a total age od 371Explanation / Answer
Here is your solution:
StreetHouse.java
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
/**
* @author Lokesh Kumar
*
*/
public class StreetHouse {
static Map<Integer, String> houseNumbers = new LinkedHashMap<Integer, String>();
static Map<String, List<Integer>> peopleAge = new LinkedHashMap<String, List<Integer>>();
/**
* This method used to sum all the ages from houses
* @param list
* @return
*/
public static int sum(List<Integer> list) {
int sum = 0;
for (int i : list) {
sum += i;
}
return sum;
}
/**
* This method used to get total hosues in the street
* @param scanner
* @return
*/
public static int totalStreets(Scanner scanner) {
System.out.println("How many houses in the street ? : ");
return scanner.nextInt();
}
/**
* This method used to get all house numbers
* @param noOfHouses
* @param scanner
*/
public static void houseNumbers(int noOfHouses, Scanner scanner) {
for (int i = 1; i <= noOfHouses; i++) {
System.out.println("What is the next house number? :");
houseNumbers.put(i, scanner.next());
}
}
/**
* This method used to get people ages from every house
* @param noOfHouses
* @param scanner
*/
public static void peopleAge(int noOfHouses, Scanner scanner) {
for (int i = 1; i <= noOfHouses; i++) {
System.out.println("How many people live in the number " + houseNumbers.get(i) + " :");
int noOfPeople = scanner.nextInt();
List<Integer> peopleAges = new ArrayList<Integer>();
for (int j = 1; j <= noOfPeople; j++) {
System.out.println("What is the age of person " + j + " :");
int ageOfPerson = scanner.nextInt();
peopleAges.add(ageOfPerson);
}
peopleAge.put(houseNumbers.get(i), peopleAges);
}
}
/**
* This method used to calculate total people's age
* @param noOfHouses
*/
public static void calculateIndividual(int noOfHouses) {
int sumOfAllAges = 0;
for (int i = 1; i <= noOfHouses; i++) {
List<Integer> totalAge = peopleAge.get(houseNumbers.get(i));
int total_age = StreetHouse.sum(totalAge);
System.out.println("House " + houseNumbers.get(i) + " has a total age of " + total_age);
sumOfAllAges = sumOfAllAges + total_age;
}
System.out.println(" The street has a total age of " + sumOfAllAges);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int noOfHouses = StreetHouse.totalStreets(scanner);
StreetHouse.houseNumbers(noOfHouses, scanner);
StreetHouse.peopleAge(noOfHouses, scanner);
StreetHouse.calculateIndividual(noOfHouses);
scanner.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.