Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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. Grad

// 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)

{

temp++;

// 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 (int i=0; 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("The mean of household size in Marengo is: " + mean);

// Calculate the median

if (temp % 2 == 0) {

median = householdSizes[temp/2-1];

}

else {

median = householdSizes[temp/2];

}

// Print the median

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

System.exit(0);

} // End of main() method.

public static void displayArray(int[] householdSizes, int size){

for (int i=0; i

}

System.out.println(householdSize[i] + " ");

}

public static void sortArray(int[] householdSizes, int size) {

int n = size;

int temp = 0;

for (int i=0; i

for (int j=0; j<(n-i); j++){

if(householdSizes[i-1] > householdSizes[i]) {

temp = householdSizes[i-1];

householdSizes[i-1] = householdSizes[i];

householdSizes[i] = temp;

}

}

}

}

} // End of HouseholdSize class.

I really don't understand what I'm doing wrong. Help please.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Navigating The Supply Chain Maze A Comprehensive Guide To Optimize Operations And Drive Success

Authors: Michael E Kirshteyn Ph D

1st Edition

B0CPQ2RBYC, 979-8870727585

More Books

Students also viewed these Databases questions

Question

Write the difference between sexual and asexual reproduction.

Answered: 1 week ago

Question

What your favourite topic in mathematics?

Answered: 1 week ago

Question

Briefly describe vegetative reproduction in plants.

Answered: 1 week ago

Question

My opinions/suggestions are valued.

Answered: 1 week ago