Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java biggners Problem definition: You will define number of chip monks and number of trees. There will be a random number of acorns on each

java biggners

Problem definition:

You will define number of chip monks and number of trees. There will be a random number of acorns on each tree. You will pick one chip monk (say chip monk 0 in array) and make him your chip monk.

On each loop of the game you will select which tree you will go to. All other chip monks will go to a random selected tree. While at a tree you will first try to give every chip monk 4 acorns. If there are not enough acorns for every chip monk to get 4 you will find the largest integer that all chip monks can get that many acorns. If there are fewer acorns than chip monks you start with the lowest number chip monk and give him one acorn and proceed until all the acorns are gone.

The game is over when all the acorns from all the trees are gone.

You will print each chip monk and the number of acorns they got and see if you have more acorns than the other chip monks.

Specifically:

The game will start where you will define the number of chip monks and the number of trees that have acorns on them.

You will also define one chip monk giving it a name that will be your chip monk

The game will run until there are no more acorns available.

The game will assign a random number of acorns per tree between zero and 40.

The game will ask you which tree you want your chip monk to go to

The game will select at random which tree each other chip monk will go to.

The maximum number of acorns a chip monk can carry is 4.

If there are multiple chip monks at the same tree and not enough acorns for every chip monk to carry 4, then each chip monk will get the same number of acorns.

If there are multiple chip monks and not enough acorns for each chip monk to get one then give one acorn to as many chip monks as you can starting with lowest number chip monk.

The output will show each chip monk and how many acorns they picked up. It will compare that to your chip monk and let you know if you picked up more acorns than the other chip monks and who the winner was.

HINT: pretend you have 3 chip monks and 3 trees and walk through a couple loops of their getting acorns. Draw out on piece paper the chip monks and trees with acorns on trees.

The following is one possible logical approach to the problem:

1. Read in number of chip monks and number of trees

2. Create one dimensional array for trees where index is tree number and value is number of acorns at tree

3. Create a one dimensional array for chip monks where the index is chip monk number and value is number of acorns chip monk has.

a. Assume all values zero or create do loop to initialize

4. For each tree randomly assign number acorns per tree putting value in array between 0 and 40

int Math.random()*40

a. Need know total acorns to know when to stop so add number acorns each tree to variable. Then during game subtract as each chip monk gets acorns.

5. Create a loop to let chip monks get acorns as long as there are acorns left

a. Ask which tree you want your chip monk to go to

b. For each chip monk randomly figure out which tree they are going to

c. Save the tree number in another array with index being which number chip monk

6. To figure out acorns :

a. For each tree go through array and count number of chip monks that are going to that tree

b. Integer divide the number of chip monks into the number of acorns left

1. If the number (integer) is greater than 3 go through array and find each chip monks giving them 4 acorns and add number chip monks times 4 to number of acorns passed out and subtract from acorns on that tree

2. If the number is greater zero but less than 4 go through each chip monk and give them that number acorns and add that number to acorns passed out and subtract from acorns on tree

3. If the number is zero which means not all chip monks get acorns

If number acorns at tree is zero skip over no acorns to assign

Give lowest number chip monk one acorn subtract from number and if zero done else go to next lowest number chip monk and give him an acorn.

c. Continue looping until all acorns are gone

7. Print out each chip monk and number acorns they collected. Also print out if your chip monk collected more acorns the other chip monks

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

Automating Access Databases With Macros

Authors: Fish Davis

1st Edition

1797816349, 978-1797816340

Students also viewed these Databases questions

Question

List out some inventory management techniques.

Answered: 1 week ago