Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Urgent Please! Please read it carefully. It carries 50 marks. Please don't forget to submit java files with proper comments and explaning the code operation.

Urgent Please! Please read it carefully. It carries 50 marks. Please don't forget to submit java files with proper comments and explaning the code operation. Thank you so much!!!

Write a function, the function prints the output to the command line. The output consists of the String prefix followed by "section numbers" of the form 1.1., 1.2., 1.3., and so on. The levels argument

determines how may levels the section numbers have. For example, if levels is 2, then the section numbers have the form x.y. If levels is 3, then section numbers have the form x.y.z. The digits permitted in each level are always '1' through '3'. As an example, if prefix is the string "THERBLIG" and levels is 2, then the function would start by printing:

THERBLIG1.1. THERBLIG1.2. THERBLIG1.3. and end by printing: THERBLIG3.1. THERBLIG3.2. THERBLIG3.3. The stopping case occurs when levels reaches zero (in which case the prefix is printed once by itself followed by nothing else). Hint: sections(String prefix, int levels){ //the base vase is level == 0 if(levels == 0){ //print the prefix once stop the function }else{ //loop over through 1 to 3 //Create a new prefix = prefix + level+"." // recursive calls of the section with the new_prefix and levels-1 } }

Problem2 A Teddy Bear Picnic This question involves a game with teddy bears. The game starts when I give you some bears. You can then give back some bears, but you must follow these rules (where n is the number of bears that you have): 1. If n is divisible by 5, then you may give back exactly 42 bears.

2. If n is even, then you may give back exactly n/2 bears. 3. If n is divisible by 3 or 4, then you may multiply the last two digits of n and give back this many bears. (By the way, the last digit of n is n%10, and the next-to-last digit is ((n%100)/10). Don't change the sequence of the above three conditions. The goal of the game is to end up with EXACTLY 42 bears. For example, suppose that you start with 250 bears. Then you could make these moves: --Start with 250 bears. --Since 250 is divisible by 5, you may return 42 of the bears, leaving you with 208 bears. --Since 208 is even, you may return half of the bears, leaving you with 104 bears. --Since 104 is even, you may return half of the bears, leaving you with 52 bears. --Since 52 is divisible by 4, you may multiply the last two digits (resulting in 10) and return these 10 bears. This leaves you with 42 bears. --You have reached the goal! Write a recursive function to meet this specification: bool bears(int n) // Postcondition: A true return value means that it is possible to win // the bear game by starting with n bears. A false return value means that // it is not possible to win the bear game by starting with n bears. // Examples: // bear(250) is true (as shown above) // bear(42) is true // bear(84) is true // bear(53) is false // bear(41) is false Hint: To test whether n is even, use the expression ((n % 2) == 0). If you want to check if n is divisible by another number, you can check ((n % number) == 0). public static boolean bears(int n){ if (n < 42){ //return false; } if (n == 42){ //return true }else{ 1. if n is divisible by 5 //n = n-42 //recursively call of the bear function //bears(n) //return true; 2. If n is even //then you may give back exactly n/2 bears n = n/2.

// bears(n) //return true 3. If n is divisible by 3 or 4 //then you may multiply the last two digits of n and give back this many bears n =n-(multiply the last two digits of n) //recursive call bears(n) return true //return false; } } What to turn in: You can reasonably implement the entire game in a single file. Submit that file on Canvas. Remember to include comments at the beginning of the file providing your name and a brief description of what the program does. If you do any extra work and want to get credit for it then briefly describe your extra work in the comments at the beginning of your program so that I know what I'm looking for when I grade it.

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions