Question

In: Computer Science

Write a method called recursiveDownAndUp() that takes one non-negative integer parameter, recursively starts at one thousand...

Write a method called recursiveDownAndUp() that takes one non-negative integer parameter, recursively starts at one thousand and prints all the integers from one thousand to the parameter (that is, prints 1000, 999, etc. all the way down to the parameter), then recursively starts at the integer parameter and prints all the integers from the parameter up to one thousand (that is, prints the parameter, the parameter + 1, the parameter + 2, etc. all the way up to 1000).

Hint: Here's a recursive method named recursiveUpAndDown() that performs the opposite counting being asked for in recursiveDownAndUp() -- it prints from the integer parameter recursively up to 1000, then prints from 1000 recursively down to the integer parameter. The method happens to be in a class called myCounter which has a main() method that invokes recursiveUpAndDown():

class myCounter{

static void recursiveUpAndDown(int i)

{

    if (i < 1)       // base case

        return;

            if (i > 1000)    // base case

        return;

    else

    {

        System.out.println(i);

        recursiveUpAndDown(i + 1); // recursive call

        System.out.println(i);

        return;

    }

}

  

public static void  main(String[] args)

{

    int i = 1;

    recursiveUpAndDown(i);

}

}

Notice recursiveUpAndDown()'s if statements about the value of i in the base case, and also notice recursiveUpAndDown()'s addition in the recursive call -- these are the heart of the logic which cause the counting up to occur first and the counting down to occur second

Solutions

Expert Solution

JAVA PROGRAM

/**
* class: MyCounter
*
*/
public class MyCounter {
   private static final int limit = 1000; // limit constant is set here
  
   private static int max= limit; //this is the static instance variable ; set to limit initially
  
   /**
   * method recursiveDownAndUp
   * It will downCount starting from limit till param
   * and then it will count up from param till limit
   * @param param
   */
   private static void recursiveDownAndUp(int param){
       if(param >limit){//base condition: when param is greater than limit then recursion stops
           return;
       }else{
           System.out.println(max--);//print max and decrement it
           recursiveDownAndUp(param+1); //recursive call with (param+1)
           System.out.println(++max);//increment max and print it
       }
      
   }
  
   //main method
   public static void main(String args[]){
       recursiveDownAndUp(953); //count down from 1000 to 953 and then from 953 to 1000
   }

}

===============================

OUTPUT

===============================

1000
999
998
997
996
995
994
993
992
991
990
989
988
987
986
985
984
983
982
981
980
979
978
977
976
975
974
973
972
971
970
969
968
967
966
965
964
963
962
961
960
959
958
957
956
955
954
953
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000


Related Solutions

Write a method that takes an integer array as its parameter and sorts the contents of...
Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm. Call this method after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed. CODE SO FAR: import java.util.*; public class ArrayInteger { public static void getRandomNumber(int A[],int n){ Random...
Write a static method called "evaluate" that takes a string as a parameter
In Java language  Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
.. Write a method called findNums that takes a two-dimension array of integers as a parameter...
.. Write a method called findNums that takes a two-dimension array of integers as a parameter and returns the number of times a two-digit number appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 The value returned would be 5 (there are 5 two-digit numbers in the array) public class Question2 {    public static void main(String args[]){      int arr[][] = {{10, 45,...
Write a function convert_date that takes an integer as a parameter and returns three integer values...
Write a function convert_date that takes an integer as a parameter and returns three integer values representing the input converted into days, month and year (see the function docstring). Write a program named t03.py that tests the function by asking the user to enter a number and displaying the output day, month and year. Save the function in a PyDev library module named functions.py A sample run for t03.py: Enter a date in the format MMDDYYYY: 05272017 The output will...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns the number of 1's in the binary representation of n. Use the fact that this is equal to the number of 1's in the representation of n//2 (integer division) plus 1 if n is odd. >>>numOnes(0) 0 >>>numOnes(1) 1 >>>numOnes(14) 3
Write in Racket language. Write a non-recursive Racket function called "keep-short-norec" that takes an integer and...
Write in Racket language. Write a non-recursive Racket function called "keep-short-norec" that takes an integer and a list of strings as parameters and evaluates to a list of strings. The resulting list should be all strings on the original list, maintaining their relative order, whose string length is less than the integer parameter. For example, (keep-short-rec 3 '('abc' 'ab' 'a')) should evaluate to '('ab''a') because these are the only strings shorter than 3.
Write a RECURSIVE method that receives as a parameter an integer named n. The method will...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will output n # of lines of stars. For example, the first line will have one star, the second line will have two stars, and so on. The line number n will have "n" number of ****** (stars) so if n is 3 it would print * ** *** The method must not have any loops!
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and...
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and returns a double. When the variable argument phone contains the character a or A print the word Apple and return 1099.99 When phone contains anything else return a 0.0
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the...
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the number of binary strings of length n that do not have two consecutive 0’s. For example, for n = 4, the number of binary strings of length 4 that do not contain two consecutive 0’s is 8: 1111, 1110, 1101, 1011, 1010, 0111, 0110, 0101. For this problem, your method needs to return only the number of such strings, not the strings themselves. You...
Code in Golang: Write a boolean function called share with one integer parameter which determines whether...
Code in Golang: Write a boolean function called share with one integer parameter which determines whether the sum of the digits in the inputted integer and the inputted integer share a factor other than one. If so, return true, otherwise return false. Examples: 121 sum is 4, so the function returns false 242 sum is 8, so the function returns true
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT