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

HouseholdSize.java Lab 8-2: Using a Bubble Sort In this lab, you will complete a

ID: 3827169 • Letter: H

Question

HouseholdSize.java

Lab 8-2: Using a Bubble Sort

In this lab, you will complete a Java program that uses an array to store data for the village of Marengo. The village of Marengo conducted a census and collected records that contain household data, including the number of occupants in each household. The exact number of household records has not yet been determined, but you know that Marengo has fewer than 300 households. 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.

Sample output:

// 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 javax.swing.*;

public class HouseholdSize
{
   public static void main(String args[])
   {
       // 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;
       int median = 0;

       // Input household size
       householdSizeString = JOptionPane.showInputDialog("Enter household size or 999 to quit: ");
       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;
          
// Calculate total of household sizes here


          
           x++; // Get ready for next input item.
           householdSizeString = JOptionPane.showInputDialog("Enter household size or 999 to quit: ");
           householdSize = Integer.parseInt(householdSizeString);
       } // End of input loop.
      
      
       // Find the mean
      
       // This is the work done in the sortArray() method
      
      
       // This is the work done in the displayArray() method
       // Print the mean

       // Calculate the median

// Print the median

       System.exit(0);
   } // End of main() method.
} // End of HouseholdSize class.

nput Enter household size or 999 to quit: 999 OK Cancel

Explanation / Answer

HouseholdSize.java

import javax.swing.*;
public class HouseholdSize
{
public static void main(String args[])
{
// 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=0;
double sum = 0;
double mean = 0;
int median = 0;
// Input household size
householdSizeString = JOptionPane.showInputDialog("Enter household size or 999 to quit: ");
householdSize = Integer.parseInt(householdSizeString);
// This is the work done in the fillArray() method
x = 0;
while(x < limit && householdSize != 999)
{
   temp++;
// Place value in array.
householdSizes[x] = householdSize;
// Calculate total of household sizes here

x++; // Get ready for next input item.
householdSizeString = JOptionPane.showInputDialog("Enter household size or 999 to quit: ");
householdSize = Integer.parseInt(householdSizeString);
} // End of input loop.
  
  
// Find the mean
for(int i=0; i<temp ;i++){
   sum = sum + householdSizes[i];
}
mean = sum/(double)temp;
// This is the work done in the sortArray() method
sortArray(householdSizes, temp);
  
// This is the work done in the displayArray() method
displayArray(householdSizes, temp);
// Print the mean
System.out.println("Mean household size in Marengo is: "+mean);
// Calculate the median
System.out.println("Median household size in Marengo is: "+getMedian(householdSizes, temp));
// Print the median
System.exit(0);
} // End of main() method.
public static int getMedian(int[] householdSizes, int size) {
   if(size % 2==0){
           return householdSizes[size/2 -1 ];
       }
       else {
           return householdSizes[size/2 ];
          
       }
}
public static void displayArray(int[] householdSizes,int size) {
   for(int i=0; i<size ;i++){
       System.out.print(householdSizes[i]+ " ");
   }
   System.out.println();
}
public static void sortArray(int[] householdSizes, int size) {


  
int n = size;
int temp = 0;
  
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
  
if(householdSizes[j-1] > householdSizes[j]){
//swap the elements!
temp = householdSizes[j-1];
householdSizes[j-1] = householdSizes[j];
householdSizes[j] = temp;
}
  
}
}

}
} // End of HouseholdSize class.

Output:

1 2 3 4 5 6 7 8 9
Mean household size in Marengo is: 5.0
Median household size in Marengo is: 5