How can I express the following situation in Java programming?

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter5: Control Structures Ii (repetition)
Section: Chapter Questions
Problem 14PE
icon
Related questions
Question
100%

How can I express the following situation in Java programming?

 

Orcs, Hobbits, and Tom Bombadil
 We have n-1 Orcs and n Hobbits who want to cross a river. We also have Tom Bombadil who wants to cross as well.
Only Hobbits and Tom Bombadil can row the boat (don’t ask me why!)
At all times, the number of Hobbits on either side of the river must be greater or equal the number of Orcs on that side (unless there are no Hobbits on that side), or the Hobbits will be killed by the Orcs!
 If the boat holds 2, with n=4 or more, the problem has no solution.
 If the boat can hold 3, then up to n=5 can be solved.
 If the boat can hold 4, any number (n) Hobbits can cross.
 So -- if n = 1,2 or 3 - use a boat of size 2.
 If n = 4 or 5 - use a boat of size 3.
 If n > 5 use a boat of size 4.
 If n <0 invalid input
 if n = 0 no output.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Hiring Problem
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning