11.2.1: Calling a recursive method.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) { char startingLetter; startingLetter = ‘z’; /* Your solution goes here */ }}11.5.1: Recursive method: Writing the base case.Write code to complete doublePennies()’s base case. Sample output for below program:Note: These activities may test code with different test values. This activity will perform three tests, with startingPennies = 1 and userDays = 10, then with startingPennies = 1 and userDays = 40, then with startingPennies = 1 and userDays = 1. See “How to Use zyBooks”. Also note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds, and report “Program end never reached.” The system doesn’t print the test case that caused the reported message. public class CalculatePennies {// Returns number of pennies if pennies are doubled numDays times public static long doublePennies(long numPennies, int numDays) { long totalPennies; /* Your solution goes here */ else { totalPennies = doublePennies((numPennies * 2), numDays – 1); } return totalPennies; }// Program computes pennies if you have 1 penny today,// 2 pennies after one day, 4 after two days, and so on public static void main (String [] args) { long startingPennies; int userDays; startingPennies = 1; userDays = 10; System.out.println(“Number of pennies after ” + userDays + ” days: ” + doublePennies(startingPennies, userDays)); }}11.6.1: Writing a recursive math method.Write code to complete raiseToPower(). Sample output if userBase is 4 and userExponent is 2 is shown below. Note: This example is for practicing recursion; a non-recursive method, or using the built-in method pow(), would be more common.public class ExponentMethod { public static int raiseToPower(int baseVal, int exponentVal) { int resultVal; if (exponentVal == 0) { resultVal = 1; } else { resultVal = baseVal * /* Your solution goes here */; } return resultVal; } public static void main (String [] args) { int userBase; int userExponent; userBase = 4; userExponent = 2; System.out.println(userBase + “^” + userExponent + ” = ” + raiseToPower(userBase, userExponent)); }}12.3.2: Output formatting: Printing a maximum number of decimals.Write single statement that prints outsideTemperature with 2 decimals. End with newline. Sample output:public class OutsideTemperatureFormatting { public static void main (String [] args) { double outsideTemperature; outsideTemperature = 103.46432; /* Your solution goes here */ }}12.4.2: Reading from a string.Write code that uses the input string stream inSS to read input data from string userInput, and updates variables userMonth, userDate, and userYear. Sample output if userinput is “Jan 12 1992”:import java.util.Scanner;public class StringInputStream { public static void main (String [] args) { Scanner inSS = null; String userInput = “Jan 12 1992”; inSS = new Scanner(userInput); String userMonth; int userDate; int userYear; /* Your solution goes here */ System.out.println(“Month: ” + userMonth); System.out.println(“Date: ” + userDate); System.out.println(“Year: ” + userYear); }}12.4.3: Output using string stream.Write code that inserts userItems into the output string stream itemsOSS until the user enters “Exit”. Each item should be followed by a space. Sample output if user input is “red purple yellow Exit”:import java.util.Scanner;import java.io.PrintWriter;import java.io.StringWriter;public class StringStreamOutput { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); String userItem; StringWriter itemCharStream = new StringWriter(); PrintWriter itemsOSS = new PrintWriter(itemCharStream); System.out.println(“Enter items (type Exit to quit):”); userItem = scnr.next(); while (!userItem.equals(“Exit”)) { /* Your solution goes here */ userItem = scnr.next(); } userItem = itemCharStream.toString(); System.out.println(userItem); }}
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here