Question

JAVA PROBLEM #1: The British have been shelling the Revolutionary forces with a large cannon from...

JAVA

PROBLEM #1:

The British have been shelling the Revolutionary forces with a large cannon from within their camp. You have been assigned a dangerous reconnaissance mission -- to infiltrate the enemy camp and determine the amount of ammunition available for that cannon.

Fortunately for you, the British (being relatively neat and orderly) have stacked the cannonballs into a single pyramid-shaped stack. At the top is a single cannonball resting on a square of 8 cannonballs, which is itself resting on a square of 27 cannonballs, which is itself resting on a square of 64 cannonballs, and so forth. Given the danger in your situation, you only have a chance to count the number of layers before you escape back to your own encampment.

Your assignment, upon your return, is to write a method which will return the number of cannonballs in the stack, given the number of layers. Good luck!

Requirements:

Come up with a recurrence relation (mathematical expression of the recursive function) for the method. Do this before you start coding.

Put the recurrence relation (including the base case) in the comments for this method.

Write a recursive static Java method to solve the problem

Write a main method which will prompt the spy to type in the number of layers, call the method and display the results.

Make sure the method is never called with a negative number.

Allow the user to enter several values (loop). Use a sentinel to stop the program.

No credit will be awarded for a non-recursive solution.

PROBLEM #2:

The spy also wants to tell General Washington how many troops to send. To protect the information, the spy encoded it. To decode the number, add all the individual digits. For example, the coded number, 1234, sums to the real number, 10.

Requirements:

Come up with a recurrence relation (mathematical expression of the recursive function) for the method. Do this before you start coding.

Put the recurrence relation (including the base case) in the comments for this method.

Write a recursive static Java method to solve the problem

Write a main method which will prompt the spy to type in the encoded number of troops, call the method and display the results.

Make sure the method is never called with a negative number.

Allow the user to enter several values (loop). Use a sentinel to stop the program.

No credit will be awarded for a non-recursive solution.

Grading

Requirements

(approximate points)

Possible Points

Problem #1 - Cannonballs

The method correctly calculates the number of cannonballs in a stack of N layers using recursion (13 pts)

A recurrence relation (including base case) is included in the comments (4 pts)

17 pts

Problem #2 – Sum digits

The method correctly calculates the sum of the digits in a number using recursion (13 pts)

A recurrence relation (including base case) is included in the comments (4 pts)

17 pts

main()

main() prompts for the number of layers the stack of cannonballs and calls the cannonball method

main() displays the number of cannonballs in the stack

main() does not permit negative numbers to be passed to the cannonballs method

main() includes a sentinel controlled loop which allows the user to call the cannonballs method repeatedly

main() prompts for the number of troops and calls the sum method

main() displays the sum of the digits

main() does not permit negative numbers to be passed to the reverse method

main() includes a sentinel controlled loop which allows the user to call the reverse method repeatedly

6 pts

  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

For the following problem, we need to have following consideration in mind.


Following is the program for problem 1:

  1. We need to make both the function recursive so the base cases and the recurrence relation for both the function are as follows.
    1. Base case: T(1) = 1

      Reccurrence: T(n) = T(n-1) + n*n*n

    2. Base case T(0) = 0

      Recurrence: T(n) = n%10 + T(n/10)

  2. After this we need to make the main function for both the problems, have a sentinel and let it stop when user presses the -1.

import java.util.Scanner;

class Balls {
   public static int cannonballs(int n) {
      // Base case: T(1) = 1
      // Reccurrence: T(n) = T(n-1) + n*n*n
      // applying the recurrence relation in the code
      if(n == 1) {
         return 1;
      }else {
         return n*n*n + cannonballs(n-1);
      }
   }
   
   public static void main(String args[]) {
   
      Scanner scan = new Scanner(System.in);
      System.out.println("Enter number of layers");
      // taking input the first no. of layers
      int layers = scan.nextInt();
      while(layers != -1) {
         // check for negative layers
         if(layers < 0) {
            System.out.println("Enter the positive value");
            layers = scan.nextInt();
            continue;
         }
         // finding the number of balls using recursive method
         int balls = cannonballs(layers);
         // printing and asking for next layer
         System.out.println("Cannolballs for " + String.valueOf(layers) + " layers are: " + String.valueOf(balls));
         System.out.println("Enter next layer or -1 to exit");
         // taking input for the next layer
         layers = scan.nextInt();
         
      }
      
      System.out.println("Stopped");
   }
}

Following is the output after filling it the random numbers.

terminatedsava Application] CProgram FilesJavaljre1.8.0 171binljavaw.exe (Dec7, 2018, 3:50:21 AM Enter number of layers Cannolballs for 3 layers are: 36 Enter next layer or -1 to exit 4 Cannolballs for 4 layers are: 100 Enter next layer or -1 to exit 8 Cannolballs for 8 layers are: 1296 Enter next layer or -1 to exit 9 Cannolballs for 9 layers are: 2025 Enter next layer or -1 to exit 1 stopped

Following is the program to the problem 2:


import java.util.Scanner;

class Number {
   public static int sumDigits(int n) {
      // Base case T(0) = 0
      // Recurrence: T(n) = n%10 + T(n/10)
      // applying the recurrence relation in the code
      if(n==0) {
         return 0;
      }else {
         return n%10 + sumDigits(n/10);
      }
   }
   public static void main(String args[]) {
      
      Scanner scan = new Scanner(System.in);
      System.out.println("Enter a positive number");
      // taking input the number
      int number = scan.nextInt();
      while(number != -1) {
         // checking if the number entered is negative
         if(number < 0) {
            System.out.println("Enter the positive value");
            number = scan.nextInt();
            continue;
         }
         // calling the recursive function
         int sum = sumDigits(number);
         // printing the returned values
         System.out.println("Sum of Digits of the number " + String.valueOf(number) + " is: " + String.valueOf(sum));
         System.out.println("Enter next positive number or -1 to exit");
         // taking input for the next number
         number = scan.nextInt();
      }
      System.out.println("Stopped");
   }
}

After running the code i got the following output.

If you have any problem regarding understanding of the solution, do let me know in the comment section.

Thanks.

Add a comment
Know the answer?
Add Answer to:
JAVA PROBLEM #1: The British have been shelling the Revolutionary forces with a large cannon from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5,...

    Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. The 2 is found by adding the two numbers before it (1+1) The 3 is found by adding the two numbers before it (1+2), And the 5 is (2+3), and so on!         Example: the next number in the sequence above is 21+34 = 55 Source:...

  • LAB5 #1. Method and loop Write a method integerPower(base, exponent) that returns the value of baseexponent...

    LAB5 #1. Method and loop Write a method integerPower(base, exponent) that returns the value of baseexponent (2 pts). For example, integerPower(3, 4) returns 81. Assume thatexponent is a positive nonzero integer, and base is an integer. Method integer should use for or while loop to control the calculation. Do not use any Math library methods. Incorporate this method into a program class and invoke this method with different combinations of input values at least 4 times. Please use printf() method...

  • PLEASE HELP!!!I need help with the following JAVA PROGRAMS. Please ready carefully. So this comes from...

    PLEASE HELP!!!I need help with the following JAVA PROGRAMS. Please ready carefully. So this comes from tony gaddis starting out with java book. I have included the screenshot of the problems for reference. I need HELP implementing these two problems TOGETHER. There should be TWO class files. One which has TWO methods (one to implement problem 8, and one to implement problem 9). The second class should consist of the MAIN program with the MAIN method which calls those methods....

  • *In JAVA please* Tasks This lab has two parts: Write a recursive method that converts a...

    *In JAVA please* Tasks This lab has two parts: Write a recursive method that converts a decimal number to a different base number system. Print out the results after testing your method on a few different inputs. Task 1 – Recursive Method Create a recursive method that returns a given number converted from base ten to a given other base number system ranging from two to thirty-six. A decimal number, or base ten number, can be expressed in any other...

  • Please do both parts (in Java); thanks! An Armstrong number of three digits is an integer...

    Please do both parts (in Java); thanks! An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3^3 + 7^3 + 1^3 = 371. Draw the flowchart and write a Java code to find ALL Armstrong number in the range of 0 and 999. You should write your code in two ways: (Yes as if you are...

  • CIT 149 JAVA 1 programming question? There will be two files for this problem. The first...

    CIT 149 JAVA 1 programming question? There will be two files for this problem. The first one, called FutureValue will hold the main method. The second file called FinancialUtils will hold the two methods that will display what the program will do, and perform the calculations. In the main method, which controls the logic of the program, you will first call the displayInstructions method which will display a statement of what the program will do. You will write this description...

  • Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

    Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT