Question

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

Homework Answers

Answer #1

class myCounter{

static void recursiveDownAndUp(int i){
   if(i < 1)
       return;
   else{
       System.out.println(i);

recursiveDownAndUp(i - 1); // recursive call

return;
   }
  
}
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


return;

}

}

  

public static void main(String[] args)

{

int i = 1, j = 1000;
   System.out.println("RecursiveDownAndUp");
   recursiveDownAndUp(j);
   System.out.println("RecursiveUpAndDown");
recursiveUpAndDown(i);
}

}

/* PLEASE UPVOTE */

Know the answer?
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
Similar Questions
write a recursive racket function "sum-alternate" that takes a positive integer x as a parameter. The...
write a recursive racket function "sum-alternate" that takes a positive integer x as a parameter. The function should return the sum of all the integers x, x-2, x-4, x-6, etc. as long as the numbers are positive. For example, [sum-alternate 5] should evaluate to 5 + 3 + 1, and [sum-alternate 6] should evaluate to 6+4+2.
Write a method that returns the sum of all the elements in a specified column in...
Write a method that returns the sum of all the elements in a specified column in a 3 x 4 matrix using the following header: public static double sumColumn(double[][] m, int columnIndex) The program should be broken down into methods, menu-driven, and check for proper input, etc. The problem I'm having is I'm trying to get my menu to execute the runProgram method. I'm not sure what should be in the parentheses to direct choice "1" to the method. I'm...
java 1) Create a mutator method called setPosition that accepts one integer parameter. Update the position...
java 1) Create a mutator method called setPosition that accepts one integer parameter. Update the position variable by adding the position to the parameter variable. 2)debug the code public class food { public static void main(String[] args) { Fruits apple = new Fruits(20); // Write the statement to call the method that will increase the instance variable position by 6. Fruits.setPosition(6); apple.getPosition(); } }
Write a static method called averageScores that takes as a parameter a Scanner containing a series...
Write a static method called averageScores that takes as a parameter a Scanner containing a series of student records and that prints a summary of each student record. A student record will begin with a name followed by a sequence of integer homework scores. The name is guaranteed to be one word composed of letters. You may assume that each student has at least one homework score. Your method should produce one lines of output for each student showing the...
Q1: Thefollowing code is supposed to return n!, for positive n. An analysis of the code...
Q1: Thefollowing code is supposed to return n!, for positive n. An analysis of the code using our "Three Question" approach reveals that: int factorial(int n){ if (n == 0)     return 1;   else     return (n * factorial(n – 1)); } Answer Choices : it fails the smaller-caller question.     it passes on all three questions and is a valid algorithm.     it fails the base-case question.     it fails the general-case question. Q2: Given that values is of...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
You will be traversing through an integer tree to print the data. Given main(), write the...
You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 - 85 -...
Write a method named changeStack that takes in a Stack of Integer objects named stackIn as...
Write a method named changeStack that takes in a Stack of Integer objects named stackIn as a parameter and returns another Stack of Integer objects. The returned Stack contains contents that is the result of switching the top half and the bottom half of the stackIn. But the ordering of integers in each half is NOT changed. In the case of odd-size stackIn, the middle element remains in the same position before and after the switch. This method is OUTSIDE...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two...
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two overloaded generic static search method to find the index locations of a specified value. One of the search methods applies to the array type while the other (overloaded) search method applies to the collection type. Implement the following generic linear search method and write a client program to display results: (Here is the header) public static <E extends Comparable<E>> int search(E[] list, E key)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT