Question: Summary In this lab, you complete a Java program that uses an array to store data for the village of Marengo. The program is described

Summary In this lab, you complete a Java program that uses an array to store data for the village of Marengo. The program is described in Chapter 8, Exercise 5, in Programming Logic and Design. The program should allow the user to enter each household size and determine the mean and median household size in Marengo. The program should output the mean and median household size in Marengo. The file provided for this lab contains the necessary variable declarations and input statements. You need to write the code that sorts the household sizes in ascending order using a bubble sort and then prints the mean and median household size in Marengo. Comments in the code tell you where to write your statements. Instructions Ensure the file HouseholdSize.java is open. Write the bubble sort. Output the mean and median household size in Marengo. Execute the program by clicking the Run button and the bottom of the screen. Enter the following input, and ensure the output is correct. Household sizes: 4, 1, 2, 4, 3, 3, 2, 2, 2, 4, 5, 6 followed by 999 to exit the program.

I'm not sure if I'm doing the mean and median right and if it's sorting I keep getting errors.

// HouseholdSize.java - This program uses a bubble sort to arrange up to 300 household sizes in

// descending order and then prints the mean and median household size.

// Input: Interactive.

// Output: Mean and median household size.

import java.util.Scanner;

public class HouseholdSize

{

public static void main(String args[])

{

Scanner s = new Scanner(System.in);

// Declare variables.

final int SIZE = 300; // Maximum number of household sizes.

int householdSizes[] = new int[SIZE]; // Array used to store up to 300 household sizes.

int x;

int limit = SIZE;

int householdSize = 0;

String householdSizeString;

int pairsToCompare;

boolean switchOccurred;

int temp;

double sum = 0;

double mean = 0;

double median = 0;

// Input household size

System.out.println("Enter household size or 999 to quit: ");

householdSizeString = s.nextLine();

householdSize = Integer.parseInt(householdSizeString);

// This is the work done in the fillArray() method

x = 0;

while(x < limit && householdSize != 999)

{

// Place value in array.

householdSizes[x] = householdSize;

x++; // Get ready for next input item.

System.out.println("Enter household size or 999 to quit: ");

householdSizeString = s.nextLine();

householdSize = Integer.parseInt(householdSizeString);

} // End of input loop.

// Find the mean

for (i=0; i

sum = sum + householdSize[i];

}

mean = sum/(double)temp;

// This is the work done in the sortArray() method

sortArray(householdSize, temp);

// This is the work done in the displayArray() method

display(householdSize, temp);

// Print the mean

System.out.println("The mean for household size in Marengo is: " + mean);

// Calculate the median

sortArray(householdSize);

if(size % 2 == 0) {

return housholdSize[size/2-1];

}

else {

return householdSize[size/2];

}

// Print the median

System.out.println("The median household size in Marengo is: " +median);

System.exit(0);

} // End of main() method.

} // End of HouseholdSize class.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!