48.0k views
3 votes
Write a statement that calls the recursive method backwardsAlphabet() with parameter startingLetter.

import java.util.Scanner; public class RecursiveCalls { public static void backwardsAlphabet(char currLetter) { if (currLetter == 'a') { System.out.println(currLetter); } else { System.out.print(currLetter + " "); backwardsAlphabet((char)(currLetter - 1)); } } public static void main (String [] args) { Scanner scnr = new Scanner(System.in); char startingLetter; startingLetter = scnr.next().charAt(0); /* Your solution goes here */ } }

User Subha
by
4.1k points

1 Answer

1 vote

Answer:

Following are the code to method calling

backwardsAlphabet(startingLetter); //calling method backwardsAlphabet

Output:

please find the attachment.

Step-by-step explanation:

Working of program:

  • In the given java code, a class "RecursiveCalls" is declared, inside the class, a method that is "backwardsAlphabet" is defined, this method accepts a char parameter that is "currLetter".
  • In this method a conditional statement is used, if the block it will check input parameter value is 'a', then it will print value, otherwise, it will go to else section in this block it will use the recursive function that prints it's before value.
  • In the main method, first, we create the scanner class object then defined a char variable "startingLetter", in this we input from the user and pass its value into the method that is "backwardsAlphabet".
Write a statement that calls the recursive method backwardsAlphabet() with parameter-example-1
User Mohsen Emami
by
4.4k points