This is an InteliJ java You are a recycling truck driver You
This is an InteliJ java
You are a recycling truck driver. You’d like to collect some statistics on how much each of the houses on a certain street is recycling.
Each house has to use crates for their recycling. This street is a little unusual since it only has 8 houses, and the city planner must have been a computer programmer, since the house numbers are 0, 1, 2, 3, 4, 5, 6, and 7.
(Hint – the house numbers are the same as array element indexes.)
Write a program that asks for the number of recycling crates set out by each house. You should store this data in an array.
How many recycling crates, in total, from all the houses on the street?
What is the largest number of crates set out by any house?
What is the smallest number of crates set out by any house?
Which house had the most recycling? Display that house number.
I have these codes, and I got all others except the bolded one.
Solution
Solution:
As per given, the house number and the array element indexes are same. Hence for getting the house number with most recycling i.e. maximum number of crates we have to modify the calculateMax logic and keep a track of the array index with the maximum value.
PFA the complete class with the full implementation in the method:
import java.util.Scanner;
public class Recycling_Crats {
static Scanner numberScanner = new Scanner(System.in);
public static void main(String[] args) {
int numberOfHouses = 8;
int[] cratesPerHouse = getRecyclingPerHouse(numberOfHouses);
int totalCrates = calculateTotal(cratesPerHouse);
int maxCrates = calculateMax(cratesPerHouse);
int minCrates = calculateMin(cratesPerHouse);
int houseWithMostRecycling = calculateHouseWithMostRecycling(cratesPerHouse);
System.out.println(\"Total crates from all houses = \" + totalCrates);
System.out.println(\"Max crates at any house = \" + maxCrates);
System.out.println(\"Min crates at any house = \" + minCrates);
System.out.println(\"House with most recycling = \" + houseWithMostRecycling);
numberScanner.close();
}
// Ask user for number of crates for each house. Store in array and return this array.
public static int[] getRecyclingPerHouse(int houses) {
int input = 0;
int numHouses[] = new int[houses];
for (int i = 0; i < houses; i++) {
System.out.println(\"Enter crats here : \");
input = numberScanner.nextInt();
numHouses[i] = input;
}
return numHouses;
}
//Add up all of the numbers in the array and return that
public static int calculateTotal(int[] cratesPerHouse) {
int total = 0;
for (int i = 0; i < cratesPerHouse.length; i++) {
total += cratesPerHouse[i];
}
return total;
}
//Which is the largest number in the array?
public static int calculateMax(int[] cratesPerHouse) {
int max = 0;
for (int i = 0; i < cratesPerHouse.length; i++) {
if (cratesPerHouse[i] > max) {
max = cratesPerHouse[i];
}
}
return max;
}
//Which is the smallest number in the array?
private static int calculateMin(int[] cratesPerHouse) {
//int numbers [] = new int[8];
int min = cratesPerHouse[0];
for (int i = 1; i < 8; i++) {
if (cratesPerHouse[i] < min) min = cratesPerHouse[i];
}
return min;
}
//Use the array to figure out which house number - or array element number - has the most recycling
public static int calculateHouseWithMostRecycling(int[] cratesPerHouse) {
//Please use here
int houseNumber = 0, maxNumberOfCrates = 0;
for(int i = 0; i < cratesPerHouse.length; i++){
if(cratesPerHouse[i] > maxNumberOfCrates){
maxNumberOfCrates = cratesPerHouse[i];
houseNumber = i;
}
}
return houseNumber;
}
}


