Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Is there way to compare all three leaders, display the average amount of cookies sold, display how much money was made if each box cost

Is there way to compare all three leaders, display the average amount of cookies sold, display how much money was made if each box cost 1.25, and be able to tell which scouts sold above average by using methods? I understand using classes but cannot use them for this particular problem. Here 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");

numScout = inputValidScout();

scoutArray = fillArray(numScout);

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: numSC

// Return: sArray

//

public static int[] fillArray(int numSC)

{

int cntr;

int[] sArray;

sArray = new int[numSC];

for(cntr = 0; cntr < sArray.length; ++cntr)

{

System.out.println("For scout " + cntr);

sArray[cntr] = inputValidCookieBox();

}

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:

// Return: numBox

//

public static int inputValidCookieBox()

{

int numBox;

numBox = 0;

do

{

numBox = inputInteger("Please enter the amount of cookie boxes sold"

+ " by 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 inputValidScout()

{

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

}

//

// genRandom

//

// The purpose of this method is to generate a random number

// between the minimum and maximum values specified inclusively

//

// Input: min the minimum value in the range of numbers

// max the maximum value in the range of numbers

//

// Return: num the random number in the given range

//

public static int genRandom(int min, int max)

{

double rNum; // the original random number generated

double range; // the range of random numbers allowed

int num;

rNum = 0.0;

range = 0.0;

num = 0;

// validate the input

if ((min >=0) && (min <= max))

{

// the input arguments are actually valid so we can generate a random number

// generate a random number 0.0 <= rNum < 1.0

rNum = Math.random( );

// determine the range

range = (double)max - (double)min + 1.0;

// scale the number between min and max inclusively

rNum = rNum * range;

rNum = rNum + (double)min;

// convert back to integer value

num = (int)rNum;

}

else

{

System.out.println("Invalid input arguments of " + min + " and " + max);

System.out.println("Random number cannot be generate");

}

// return the integer

return(num);

}// end genRandom

} // end GirlScoutCookies

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

Database And Transaction Processing

Authors: Philip M. Lewis, Arthur Bernstein, Michael Kifer

1st Edition

0201708728, 978-0201708721

More Books

Students also viewed these Databases questions