Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

HW HELP Java! The following program requires a recursive function. There is a game called Candy, where you give up pieces of candy based on

image text in transcribed

HW HELP Java!

The following program requires a recursive function. There is a game called Candy, where you give up pieces of candy based on certain rules of the amounts of candy you have when you are given some candy. You can then give back some candy, but you must follosw these rules (where n is the number of candy pieces that you have): l. Ifn is even, then you may give back exactly n/2 pieces of candy 2. If n is divisible by 3 or 4, then you may multiply the last two digits of n and give back this many pieces of candy. (Hint: the last digit of n is n%10, and the next-to-last digit is ((n%100)/10) 3. If n is divisible by S, then you may give back exactly 42 pieces of candy. The goal of the game is to end up with EXACTLY 42 pieces of candy. For example, suppose that you start with 250 pieces of candy. Then you could make these moves: Start with 250 candies. -Since 250 is divisible by' 5, you may return 42 of the candies, leaving you with 208 candies. -Since 208 is even, you may return half of the candies, leaving you with 104 candies. -Since 104 is even, you may return half of the candies, leaving you with 52 candies. -Since 52 is divisible by 4, you may multiply the last two digits (resulting in 10) and return these 10 candies This leaves you with 42 candies. You have reached the goal and won the game! Write a recursive method to meet this specification: public static boolean candy (int n) // Postcondition: A true return value means that it is poasible to win // the bear game by starting with n pieces of candy // A false return value means that it is not possible to win the candy // game by starting with n pieces of candy // Examples // candy (250) is true (as shown above) // candy (42) is true // candy (84) is true // candy (53) is false // candy (41) is talse

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 101

Authors: Guy Kawasaki

1st Edition

0938151525, 978-0938151524

More Books

Students also viewed these Databases questions