Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please answer: 1, 2, and 4. Code is already on number 3. (JAVA) Analysis: describe the Input: Output: (from #3 code) Design: draw a flow

Please answer: 1, 2, and 4. Code is already on number 3. (JAVA)

  1. Analysis: describe the Input: Output: (from #3 code)
  1. Design: draw a flow chart of the method . (from #3 code)

  1. Coding: copy your Java source program here

import java.util.Scanner;

public class HW07A {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter the number of balls to drop: ");

int numberOfBalls = input.nextInt();

String[] ballPaths = new String[numberOfBalls];

System.out.print("Enter the number of slots in the bean machine: ");

int numberOfSlots = input.nextInt();

int[] slots = new int[numberOfSlots];

// display ball paths

for (int i = 0; i < numberOfBalls; i++) {

ballPaths[i] = dropBall(slots);

System.out.printf("%10s ", ballPaths[i]);

}

// display game

System.out.println("");

printArray(slots, numberOfBalls);

}

public static String dropBall(int[] slot) {

StringBuilder ballPath = new StringBuilder();

for (int i = 0; i < slot.length - 1; i++) {

int random = (int)(Math.random() * 10) % 2;

if (random > 0) ballPath.append("R");

else ballPath.append("L");

}

int position = getBallPosition(ballPath.toString(), 'R');

slot[position]++;

return ballPath.toString();

}

public static int getBallPosition(String str, char a) {

int count = 0;

for (int i = 0; i < str.length(); i++) {

if (str.charAt(i) == a) count++;

}

return count;

}

public static void printArray(int[] slots, int numberOfBalls) {

while (!isEmpty(slots)) {

if (isRowEmpty(slots, numberOfBalls)) {

numberOfBalls--;

continue;

}

for (int i = 0; i < slots.length; i++) {

if (slots[i] >= numberOfBalls) {

System.out.printf("%2c", 'O');

slots[i]--;

}

else System.out.printf("%2c", ' ');

}

numberOfBalls--;

System.out.println("");

}

}

public static boolean isEmpty(int[] slots) {

for (int slot : slots) {

if (slot != 0) {

return false;

}

}

return true;

}

public static boolean isRowEmpty(int[] slots, int rowNum) {

for (int slot : slots) {

if (slot == rowNum) {

return false;

}

}

return true;

}

}

  1. Testing: (Describe how you test this program, you should use your own input data to test.) Use as much space as needed.

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

Creating A Database In Filemaker Pro Visual QuickProject Guide

Authors: Steven A. Schwartz

1st Edition

0321321219, 978-0321321213

More Books

Students also viewed these Databases questions

Question

What were your most important educational experiences?

Answered: 1 week ago

Question

Which personal relationships influenced you the most?

Answered: 1 week ago