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

Dr. Croly\'s beverage cellar needs to be organized. He wants a program that can

ID: 3779641 • Letter: D

Question


Dr. Croly's beverage cellar needs to be organized. He wants a program that can sort his beverages in order of quantity from largest to smallest. public static void main (String() args) (String[] data - (*The Briery - Smokey and the Bois:3*. *Browser; * De Silly - Scotch Silly, Burgundy Bbl. Aged:6*, *Boueri Van. Sternberg - de Garre:4*. Jolly Pumpkin Artisan Ales - Oro de Calabaza: 1*. *Collective Brewing Project - American Sour Red:2*. Boor. Brewery - 2012 Gauze Marriage Farfait:5*); order(data);} Write a function order String [] data) that processes the list of beverages from main. Each string in data contains a beverage and a quantity in the following format: beverage_name:quantity The function should print the beverages in order of quantity from largest to smallest like this: Brasserie De Silly - Scotch Silly, Burgundy Bbl. Aged: 6 Boon Brewery - 2012 Gauze Marriage Parfait: 5 Boueri Van Steinberger - de Gare: 4 The Briery - Smokey and the Bios: 3 Collective Brewing Project - American Sour Red: 2 Jolly Pumpkin Artisan Ales - Oro de Calabaza: 1 A few notes You cannot make any assumptions about the number of items in data.

Explanation / Answer

public class SortingApp {

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// TODO code application logic here
String[] data = {
"The Bruery - The Smokey and the Boise: 3",
"Brasserie De Silly - Scotch Silly, Burgundy Bbl. Aged: 6",
"Brouwerij Van Steenberge - de Garre: 4",
"Jolly Pumpkin Artisan Ales - Oro de Calabaza: 1",
"Collective Brewing Project - Americal Sour Red: 2",
"Boon Brewery -2012 Geuze Mariage Parfait: 5"
};
String[] sortedData = orderData(data);
  
for (String sortedData1 : sortedData) {
System.out.println(sortedData1);
}
}
  
public static String[] orderData(String[] inData)
{
//Split the string into two and obtain the quantity & position
String[][] tempData = new String[inData.length][2];
int[][] processData = new int[inData.length][2];
for(int i = 0; i < inData.length; i++){
tempData[i] = inData[i].split(":");
tempData[i][1] = tempData[i][1].trim();
//System.out.println("tempData = " + tempData[i][0] + " -> " + tempData[i][1]);
processData[i][0] = Integer.parseInt(tempData[i][1]);
processData[i][1] = i;
}
  
//Sort quantity
int k;
for(int m = inData.length; m >= 0; m--){
for (int i = 0; i < inData.length - 1; i++){
k = i+1;
if(processData[i][0] < processData[k][0]){
int temp[] = new int[2];
temp[0] = processData[i][0];
temp[1] = processData[i][1];
processData[i][0] = processData[k][0];
processData[i][1] = processData[k][1];
processData[k][0] = temp[0];
processData[k][1] = temp[1];
}
}
}
  
//Store the Data in temp string array
String[] tempString = new String[inData.length];
System.arraycopy(inData, 0, tempString, 0, inData.length);
  
//Get the sorted Data
for(int i = 0; i < tempString.length; i++){
int position = processData[i][1];
inData[i] = tempString[position];
}
  
return inData;
}
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote