Problem solving using recursion There are two skeleton programs, flesh them out following the suggestions given. Recursive algorithm for calculating xn (n ≥0): if n = 0, return 1.0 else return x * xn-1                      (slow technique) Alternate (faster) algorithm: if n = 0, return 1.0 else if n is odd return x * xn-1 else { y = xn/2;  return y * y;} (fast technique). Run program with several input values and compare results from fast power, slow power, and Math.pow in this table: Show all digits displayed by your program. You could add a loop to your program and paste the screen shot for all input values mentioned here instead. Show all digits. Base    Exponent    slow power    fast power    Math.pow 3.5    64             5    15             1.25    31             2    17

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Problem solving using recursion

There are two skeleton programs, flesh them out following the suggestions given.

Recursive algorithm for calculating xn (n ≥0): if n = 0, return 1.0 else return x * xn-1                      (slow technique)
Alternate (faster) algorithm: if n = 0, return 1.0 else if n is odd return x * xn-1 else { y = xn/2;  return y * y;} (fast technique).

Run program with several input values and compare results from fast power, slow power, and Math.pow in this table:
Show all digits displayed by your program.

You could add a loop to your program and paste the screen shot for all input values mentioned here instead. Show all digits.

Base    Exponent    slow power    fast power    Math.pow
3.5    64            
5    15            
1.25    31            
2    17            


String matching
Finding needle in a haystack:
If needle is longer than the haystack then failure
else if needle matches the initial portion of haystack (use .startsWith() method) then success
else {
    create a shorter haystack (haystack.substring(1))
    return result from recursive call on the shorter haystack
}

You are allowed to use only the following methods from the Java String class: .length(), .startsWith(), .subString().
You are not allowed to use any other method from the String class. [The String class already has a find method. We are trying to implement that using simpler tools].

Fill results from power calculation in this page, copy paste source code and screen shots from the two finished programs in the following pages. Save the file with name yourUserid_ICA06.docx and submit through Isidore Assignment. 

You could add a loop to your programs so you can show results from several inputs in one screen shot. Be sure to cover all possible situations for String Matching.

Submit this Word document filled in with source codes and screen shots in appropriate pages through Isidore assignments.

 

 final static Scanner cin = new Scanner(System.in); /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here out.print("CPS 151 ICA 04 Power calculation by YOUR NAME\n\n"); out.print("Enter base (double): "); double base = cin.nextDouble(); out.print("Enter the power to raise it to (positive int): "); int expo = cin.nextInt(); double result1 = slowPower(base, expo); double result2 = fastPower(base, expo); out.println("Result from slow power = " + result1); out.println("Result from fast power = " + result2); out.println("Result from math.pow = " + Math.pow(base, expo)); } // end main private static double slowPower(double base, int expo) { if (expo == 0) return 1.0; else return 1.0; // stub, replace with correct recursion pattern } // end method private static double fastPower(double base, int expo) { if (expo == 0) return 1.0; else return 1.0; // stub, replace with correct recursion pattern } // end method

 

Second code:

 // define a scanner attached to keyboard available to all functions final static Scanner cin = new Scanner(System.in); /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here out.print("CPS 151 ICA 04 String matching by YOUR NAME\n\n"); out.print("Enter the text (one word): "); String hayStack = cin.next(); out.print("Enter the pattern (one word): "); String needle = cin.next(); if (find(needle, hayStack)) out.println(needle + " was found in " + hayStack); else out.println(needle + " was not found in " + hayStack); } // end main private static boolean find(String needle, String hayStack) { int patLen = needle.length(); int textLen = hayStack.length(); // base case 1 if (patLen > textLen) { // failure } else if (needle.startsWith(needle)) { // base case 2 MODIFY!! // SUCCESS } else { // recurse } return false; // dummy to keep NetBeans happy } // end method

 

 

CPS 151 ICA 04 Sample output
Power calculation
I used a loop to try out all sample data in one run. Also, I used println (not printf) so java would show all
the calculated digits. Make your output like this and you do not need to submit the table.
03
Output - ICA04_Power Key (run) - Editor
Output - ICA04_Power Key (run) x
run:
CPS 151 ICA 04 Power calculation by KEY
Enter base (double) or 0.0 to quit loop: 3.5
Enter the power to raise it to (positive int): 64
Result from slow power = 6.61233485303376E34
Result from fast power = 6.612334853033757E34
Result from math.pow = 6.612334853033757E34
Enter base (double) or 0.0 to quit loop: 5
Enter the power to raise it to (positive int): 15
Result from slow power = 3.0517578125E10
Result from fast power = 3.0517578125E10
Result from math.pow= 2.0517578125E10
Enter base (double) or 0.0 to quit loop: 1.25
Enter the power to raise it to (positive int): 31
Result from slow power = 1009.741958682895
Result from fast power = 1009.7419586828951
Result from math.pow= 1009.7419586828951
Enter base (double) or 0.0 to quit loop: 2
Enter the power to raise it to (positive int): 17
Result from slow power = 131072.0
Result from fas power = 131072.0
Result from math.pow= 131072.0
Enter base (double) or 0.0 to quit loop: 0
BUILD SUCCESSFUL (total time: 58 seconds)
0
X
Transcribed Image Text:CPS 151 ICA 04 Sample output Power calculation I used a loop to try out all sample data in one run. Also, I used println (not printf) so java would show all the calculated digits. Make your output like this and you do not need to submit the table. 03 Output - ICA04_Power Key (run) - Editor Output - ICA04_Power Key (run) x run: CPS 151 ICA 04 Power calculation by KEY Enter base (double) or 0.0 to quit loop: 3.5 Enter the power to raise it to (positive int): 64 Result from slow power = 6.61233485303376E34 Result from fast power = 6.612334853033757E34 Result from math.pow = 6.612334853033757E34 Enter base (double) or 0.0 to quit loop: 5 Enter the power to raise it to (positive int): 15 Result from slow power = 3.0517578125E10 Result from fast power = 3.0517578125E10 Result from math.pow= 2.0517578125E10 Enter base (double) or 0.0 to quit loop: 1.25 Enter the power to raise it to (positive int): 31 Result from slow power = 1009.741958682895 Result from fast power = 1009.7419586828951 Result from math.pow= 1009.7419586828951 Enter base (double) or 0.0 to quit loop: 2 Enter the power to raise it to (positive int): 17 Result from slow power = 131072.0 Result from fas power = 131072.0 Result from math.pow= 131072.0 Enter base (double) or 0.0 to quit loop: 0 BUILD SUCCESSFUL (total time: 58 seconds) 0 X
String matching
Here also I used a loop to try out several matching problems in one run
DD
Output - ICA04_String Match_Key (run) - Editor
Output - ICA04_StringMatch_Key (run) X
run:
CPS 151 ICA 04 String matching by KEY
Enter the text (one word) or QUIT: abaracadabra
Enter the pattern (one word): dabra
dabra was found in abaracadabra
Enter the text (one word) or QUIT: abracadabra
Enter the pattern (one word): racad
racad was found in abracadabra
Enter the text (one word) or QUIT: abracadabra
Enter the pattern (one word): cadaver
cadaver was not found in abracadabra
Enter the text (one word) or QUIT: abracadabra
Enter the pattern (one word): abracadabra
abracadabra was found in abracadabra
Enter the text (one word) or QUIT: QUIT
BUILD SUCCESSFUL (total time: 1 minute 25 seconds)
n
X
<
Transcribed Image Text:String matching Here also I used a loop to try out several matching problems in one run DD Output - ICA04_String Match_Key (run) - Editor Output - ICA04_StringMatch_Key (run) X run: CPS 151 ICA 04 String matching by KEY Enter the text (one word) or QUIT: abaracadabra Enter the pattern (one word): dabra dabra was found in abaracadabra Enter the text (one word) or QUIT: abracadabra Enter the pattern (one word): racad racad was found in abracadabra Enter the text (one word) or QUIT: abracadabra Enter the pattern (one word): cadaver cadaver was not found in abracadabra Enter the text (one word) or QUIT: abracadabra Enter the pattern (one word): abracadabra abracadabra was found in abracadabra Enter the text (one word) or QUIT: QUIT BUILD SUCCESSFUL (total time: 1 minute 25 seconds) n X <
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 5 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY