Sorry I want to find out how to use a bubble array with some existing code. I wa
ID: 3741072 • Letter: S
Question
Sorry I want to find out how to use a bubble array with some existing code. I want to know how to add the fill array, sort array, swap array and use display array to sort in ascendign order. Current example sorts in decendign order and only uses the sort array. It works but is not explained.
I need to modify existing code:
// 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.
Explanation / Answer
import javax.swing.*;
public class HouseholdSize
{
public static void sortArray(int[] arr)
{
int size=arr.length;
for(int i=0;i<size-1;i++)
{
for(int j=0;j<size-i-1;j++)
{
if(arr[j]>arr[j+1])//swapping is done here
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
public static void displayArray(int arr[])
{
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
System.out.println();
}
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);
if(householdSize!=999)
mean+=householdSize;
} // End of input loop.
// Find the mean
mean/=x;
// This is the work done in the sortArray() method
sortArray(householdSizes);
// This is the work done in the displayArray() method
displayArray(householdSizes);
// Print the mean
System.out.println(mean);
// Calculate the median
if(x%2==0)
median=(householdSizes[x/2]+householdSizes[(x-1)/2])/2;
else
median=householdSizes[x/2];
// Print the median
System.out.println(median);
System.exit(0);
} // End of main() method.
} // End of HouseholdSize class.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.