The following Java program returns compile time error. Identify all the errors and correct them

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
The following Java program returns compile time error. Identify all the errors and correct them.
1 import java.io. HashMap;
3 public class FindError{
4
public HashMap<Integer, Long cache new HashMap<Integer, Long> ();
public static long calculate(int n) {
if (n == 0) return 1;
10
11
long result = ;
12
for (int 1 0; 1< n; 1++)
result += calculate(i) . calculate(n -i- 1);
13
14
15
16
return result;
17
18
19
public static calculateMem(int n) {
20
if(cache.containskey (n))
return cache.get(n);
21
22
23
24
if (n == 6) return 1;
25
26
long result = 0;
27
for (int 1 = 0; 1 < n; 1++)
result += calculateMem (i) * calculateMem (n - i - 1);
28
29
30
31
cache. add(n, result);
32
33
return result;
34
35
36
37
public static void main(String args[]){
38
39
for (int n = 18; n <=28; n++) {
40
int startTime = System.currentTimeNillis();
System.out.print("\noutput forn =" +n +": " + calculate(n));
System.out.println(" Execution time (normal): ". (System.currentTimeMillis() - startTime) +" ms");
41
42
43
44
startTime - System.currentTimeMillis();
System.out.print ("Output for n =" +n +": " + calculateMem (n));
System.out.println(" Execution time (memoized): ". (System.currentTimeMillis() - startTime) " ms");
45
46
47
48
49
50 }
Do not write the entire program. You are not allowed to add or remove any line from the given program to produce the expected
output. Your task is to mention the line numbers which are incorrect and then write the required changes in those lines to get the
following output:
Output for n =18: 477638700 Execution time (normal): 675 ms
Output for n =18: 477638700 Execution time (memoized): 0 ms
Output for n =19: 176726319O Execution time (normal): 1958 ms
Output for n =19: 176726319O Execution time (memoized): 0 ms
Output for n =20: 6564120420 Execution time (normal): 6101 ms
Output for n =20: 6564120420 Execution time (memoized): 0 ms
Note that the execution time might differ depending on the machine where we are executing the program.
Transcribed Image Text:The following Java program returns compile time error. Identify all the errors and correct them. 1 import java.io. HashMap; 3 public class FindError{ 4 public HashMap<Integer, Long cache new HashMap<Integer, Long> (); public static long calculate(int n) { if (n == 0) return 1; 10 11 long result = ; 12 for (int 1 0; 1< n; 1++) result += calculate(i) . calculate(n -i- 1); 13 14 15 16 return result; 17 18 19 public static calculateMem(int n) { 20 if(cache.containskey (n)) return cache.get(n); 21 22 23 24 if (n == 6) return 1; 25 26 long result = 0; 27 for (int 1 = 0; 1 < n; 1++) result += calculateMem (i) * calculateMem (n - i - 1); 28 29 30 31 cache. add(n, result); 32 33 return result; 34 35 36 37 public static void main(String args[]){ 38 39 for (int n = 18; n <=28; n++) { 40 int startTime = System.currentTimeNillis(); System.out.print("\noutput forn =" +n +": " + calculate(n)); System.out.println(" Execution time (normal): ". (System.currentTimeMillis() - startTime) +" ms"); 41 42 43 44 startTime - System.currentTimeMillis(); System.out.print ("Output for n =" +n +": " + calculateMem (n)); System.out.println(" Execution time (memoized): ". (System.currentTimeMillis() - startTime) " ms"); 45 46 47 48 49 50 } Do not write the entire program. You are not allowed to add or remove any line from the given program to produce the expected output. Your task is to mention the line numbers which are incorrect and then write the required changes in those lines to get the following output: Output for n =18: 477638700 Execution time (normal): 675 ms Output for n =18: 477638700 Execution time (memoized): 0 ms Output for n =19: 176726319O Execution time (normal): 1958 ms Output for n =19: 176726319O Execution time (memoized): 0 ms Output for n =20: 6564120420 Execution time (normal): 6101 ms Output for n =20: 6564120420 Execution time (memoized): 0 ms Note that the execution time might differ depending on the machine where we are executing the program.
Expert Solution
steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Top down approach design
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education