Question
In java, how would I be able to find and display the troop leader that has the most cookies sold, the average of cookies sold,
In java, how would I be able to find and display the troop leader that has the most cookies sold, the average of cookies sold, and the scouts who were able to sell above average. This is what I have so far.
package homework4Package;
import java.util.Scanner;
public class GirlScoutCookies
{
public static void main(String[] args)
{
handleAllTroopLeaders();
} // end main
//
// handleAllTroopLeaders
//
// the purpose of this method is to handle all troop leaders
//
// Input: none
// Return: none
//
public static void handleAllTroopLeaders()
{
final int MAXLEADERS = 3;
int cntr;
for(cntr = 0; cntr < MAXLEADERS; ++cntr)
{
handleOneTroopLeader();
}
}
//
// handleOneTroopLeader
//
// the purpose of this method is to gather
// all information needed on each troop leader
//
// Input: none
// Return: none
//
public static int[] handleOneTroopLeader()
{
String firstName;
String lastName;
int numScout;
int numBoxes;
int randomBoxes;
double average;
int[] scoutArray;
firstName = "none yet";
lastName = "none yet";
numScout = 0;
average = 0.0;
scoutArray = new int[0];
firstName = inputString("Please enter first name\t");
lastName = inputString("Please enter last name\t");
scoutArray = returnArray();
printArray(scoutArray, firstName, lastName);
return(scoutArray);
}
// returnArray
//
// the purpose of this array is to input
// the amount of cookies sold by each scout
// and to return the array
//
// Input: none
// Return: sArray
//
public static int[] returnArray()
{
int numBox;
int numScout;
int cntr;
int[] sArray;
numBox = 0;
numScout = 0;
numScout = inputValidScouts();
sArray = new int[numScout];
for(cntr = 0; cntr < sArray.length; ++cntr)
{
sArray[cntr] = inputInteger("Please enter the amount "
+ "of cookies sold by scout " + cntr + "\t");
}
return(sArray);
}
//
// printArray
// the purpose of this method is to print out the values stored in the array.
//
// Input: array the array of integers
// Return: none
//
public static void printArray(int[] array, String fName, String lName)
{
int cntr;
System.out.println("Cookie Sale Results");
System.out.println("Boxes Sold Mentored By " + fName +" "+ lName);
System.out.println("Scout Number\t\tNumber of Boxes");
for(cntr = 0; cntr < array.length; ++cntr)
{
System.out.println(cntr + "\t\t\t" + array[cntr]);
}
}
//
// inputValidCookieBox
//
// the purpose of this method is to validate
// the number of cookie boxes entered at the kbd
//
// Input: none
// Return: numBox
//
public static int inputValidCookieBox()
{
int numBox;
numBox = 0;
do
{
numBox = inputInteger("Please enter in the amount of cookies"
+ " sold by the scout\t");
if(numBox < 0 || numBox > 100)
{
System.out.println(numBox + " is invalid");
}
}while(numBox < 0 || numBox > 100);
return(numBox);
}
//
// inputValidScout
//
// the purpose of this method is to input
// a valid int for the number of scouts in the troop
//
// Input: none
// Return: numScouts
//
public static int inputValidScouts()
{
int numScouts;
numScouts = 0;
do
{
numScouts = inputInteger("Please enter the number of scouts "
+ "in the troop\t");
if(numScouts < 0)
{
System.out.println(numScouts + " is invalid.");
}
}while(numScouts < 0);
return(numScouts);
}
//
// inputString
//
// the purpose of this method is to input an string
// from the keyboard
// Input: message prompt
// Return: inputStr
//
public static String inputString(String prompt)
{
Scanner input = new Scanner(System.in);
String inputStr;
System.out.print(prompt);
inputStr = input.nextLine();
return(inputStr);
}// end inputString
//
// inputInteger
//
// the purpose of this method is to input an integer
// from the keyboard
// Input: message prompt
// Return: num
//
public static int inputInteger(String prompt)
{
Scanner input = new Scanner(System.in);
int num;
String cleanUpStr;
num = 0;
cleanUpStr = "none yet";
System.out.print(prompt);
num = input.nextInt();
cleanUpStr = input.nextLine();
return(num);
}// end inputString
//
// inputDouble
//
// the purpose of this method is to input a double
// from the keyboard
// Input: message prompt
// Return: num
//
public static double inputDouble(String prompt)
{
Scanner input = new Scanner(System.in);
double num;
String cleanUpStr;
num = 0.0;
cleanUpStr = "none yet";
System.out.print(prompt);
num = input.nextDouble();
cleanUpStr = input.nextLine();
return(num);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started