Skip to content

Instantly share code, notes, and snippets.

@bytecodeman
Created November 22, 2019 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytecodeman/4565d94810ab390d4ab77a387d2d9453 to your computer and use it in GitHub Desktop.
Save bytecodeman/4565d94810ab390d4ab77a387d2d9453 to your computer and use it in GitHub Desktop.
CSC-111 Beane Machine Homework
/*
* Name: Prof. Antonio C. Silvestri
* Date: 11/22/2019
* Course Number: CSC-111
* Course Name: Intro to Java Programming
* Problem Number: 9
* Email: silvestri@stcc.edu
* Description: Bean Machine Homework
*/
import java.util.Scanner;
public class BeanMachine {
private static final String TITLE = "Bean Machine V1.0";
private static final String CONTINUE_PROMPT = "Do this again? [y/N] ";
//**********************************************
private static int maximum(int[] slots) {
int max = slots[0];
for (int i = 1; i < slots.length; i++)
if (slots[i] > max)
max = slots[i];
return max;
}
//**********************************************
private static void verticalHistogram(int[] slots) {
int max = maximum(slots);
for (int row = max; row >= 0; row--) {
for (int i = 0; i < slots.length; i++)
System.out.printf("%5s", slots[i] > row ? "O" : " ");
System.out.println();
}
System.out.print(" +");
for (int i = 1; i < slots.length; i++)
System.out.print("----+");
System.out.println();
System.out.print(" ");
for (int i = 0; i < slots.length; i++)
System.out.printf("%5s", "(" + slots[i] + ")");
System.out.println();
for (int i = 0; i < slots.length; i++)
System.out.printf("%5d", i);
System.out.println();
}
//**********************************************
private static void horizontalHistogram(int[] slots) {
for (int i = 0; i < slots.length; i++) {
System.out.printf("%3d: ", i);
for (int j = 0; j < slots[i]; j++)
System.out.print("O");
System.out.printf("%s(%d)\n", slots[i] > 0 ? " " : "", slots[i]);
}
}
//**********************************************
private static int[] generateSlotTotals(int total, int levels) {
int[] slots = new int[levels + 1];
for (int i = 0; i < total; i++) {
int rightCount = 0;
for (int j = 0; j < levels; j++)
if (Math.random() >= 0.5) {
rightCount++;
}
slots[rightCount]++;
}
return slots;
}
//**********************************************
private static void process(Scanner sc, String args[]) {
System.out.print("How many balls do you want to drop? ");
int total = sc.nextInt();
System.out.print("How many levels in the machine? ");
int levels = sc.nextInt();
sc.nextLine();
int slots[] = generateSlotTotals(total, levels);
horizontalHistogram(slots);
System.out.println();
verticalHistogram(slots);
}
//**********************************************
private static boolean doThisAgain(Scanner sc, String prompt) {
System.out.print(prompt);
String doOver = sc.nextLine();
return doOver.equalsIgnoreCase("Y");
}
//**********************************************
public static void main(String args[]) {
System.out.println("Welcome to " + TITLE);
Scanner sc = new Scanner(System.in);
do {
process(sc, args);
} while (doThisAgain(sc, CONTINUE_PROMPT));
sc.close();
System.out.println("Thank you for using " + TITLE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment