Advanced Applied Programming

User Generated

norre25

Programming

Description

I uploaded a file contains the question and all the information you need

please I need Source Code and Output also a screenshot of the program

Unformatted Attachment Preview

Programming assignment #3 There are 2 parts to this assignments. 6. Binary Numbers Binary Numbers Computers do all their calculations using the binary (base-2) number system. (Humans use decimal (that is, base-10) numbers.) To understand how binary numbers work, recall how numbers are represented in our own base-10 system. For example, the number 382 can be expanded as 382 = 3 * 10 2 + 8 * 10 1 + 2 * 10 0 . That is, every decimal number can be written in terms of the powers of 10, and the number 382 is simply a listing of the coe¢cients of those powers from the highest power to the lowest. To represent numbers in the binary number system, …first expand the number in powers of 2. For example, consider the decimal number 234. This number can be expanded in powers of 2 as 234 = 1 * 2 7 + 1 * 2 6 + 1 * 2 5 + 0 * 2 4 + 1 * 2 3 + 0 * 2 2 + 1 * 2 1 + 0 * 2 0 . Therefore, the representation of the decimal number 234 in binary is 11101010. Notice that the coe¢cients of the powers of 2 can only be 0 and 1. Therefore, a binary number contains only 0s and 1s. Table 1 presents more examples. Write a recursive method that prompts the user to input a positive integer n and then converts it to binary. A sample header for such a method is given below. public static void convertToBinary(int n) { ……… } Here, the integer n is the (decimal) number to be converted. The method convertToBinary must call itself. In other words, it must be recursive. How can we do the needed conversion? Here is a way to do the conversion. Let us suppose that we want to convert the number 234 to a binary number. Consider the following sequence of computations. The number 11101010 is the binary representation of the decimal number 234. The sequence of remainders (read bottom to top) constitutes the binary number! Notice that the quotient in the …first line, 117, is used as the dividend in the second line and so on. Obviously, these computations can be done recursively. b. Teddy Bear Game This method involves a game with Teddy bears. The game starts when you are given n Teddy bears. You can then give back some bears, but you must follow these rules where n is the number of bears that you have. ➢ If n is even, then you may give back exactly n /2 bears. ➢ 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. ➢ If n is divisible by 5, then you may give back exactly 42 bears. 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 the following 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! The situation is more complicated that it looks! For example, suppose n is divisible by 60 (for example, n = 180). In this case, n is even, n is divisible by both 3 and 4, and n is divisible by 5. Therefore, you can take one of three actions: give back exactly n=2 bears, give back k bears where k is the product of the last two digits of n, or give back exactly 42 bears! Write a recursive method to decide whether it is possible to win the Teddy bear game if you start with n bears. You do not have to figure out the exact sequence of decisions. Just determine whether it is possible or not. public static boolean teddyBearGame(int n) { ……. } As an example, your method should return • • • • • true for n = 250, true for n = 42, false for n = 53, false for n = 41, etc.
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

The solution is attache...


Anonymous
Just the thing I needed, saved me a lot of time.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags