Question: help me solve this and for test cases I need all Possible ones URGENT!! Smell 1 Almostbest.java - package org.example; import java.util.HashMap; import java.util.Map; /

help me solve this and for test cases I need all Possible ones URGENT!! Smell1Almostbest.java- package org.example;
import java.util.HashMap;
import java.util.Map;
// This code will return the 1st argument to the power of all subsequent arguments
// This one caches our results for later reuse
public class Smell1AlmostBest {
private static Map>__cache = new HashMap>();
public static void main(String[] args){
if (args.length 2){
System.out.println("Usage: java ser515.smells.Smell1AlmostBest +");
System.exit(-1);
}
int num = Integer.parseInt(args[0]);
long timer = System.currentTimeMillis();
for (int j =1; j args.length; j++){
System.out.println(
"The "+ args[j]+"th power of "+ num +" is "+ toPower(num, Integer.parseInt(args[j])));
}
System.out.println("Completed "+ args[1]+" iterations in "+(System.currentTimeMillis()- timer)+"ms");
}
// What is the inefficiency here?
public static int toPower(int n, int pow){
Map entry =__cache.get(n);
Integer res = null;
if (entry != null){
res = entry.get(pow);
if (res == null){
res = calcPower(n, pow);
entry.put(pow, res);
}
} else {
res = calcPower(n, pow);
entry = new HashMap();
entry.put(pow, res);
__cache.put(n, entry);
}
return res;
}
private static int calcPower(int n, int pow){
if (pow ==0)
return 1;
int res =1;
for (int i =0; i pow; res *= n, i++)
;
;
;
return res;
}
}.
IPower.java- package org.example;
import java.util.HashMap;
import java.util.Map;
public interface IPower {
int toPower(int n, int pow);
}
class PowerSimple implements IPower {
public int toPower(int n, int pow){
if (pow ==0)
return 1;
int res =1;
for (int i =0; i pow; res *= n, i++)
;
;
;
return res;
}
}
class PowerCached implements IPower {
private static Map>__cache = new HashMap>();
// resolves the inefficiency in AlmostBest
public int toPower(int n, int pow){
Map entry = PowerCached.__cache.get(n);
if (entry == null){
entry = new HashMap();
}
return toCachedPower(entry, n, pow);
}
// The contract here is we know there is a cache entry so no check required
//(DBC)
private int toCachedPower(Map e, int n, int pow){
Integer res = null;
if (pow ==0){
res =1;
} else {
res = e.get(pow);
if (res != null){
return res;
} else {
res = n * toCachedPower(e, n, pow -1);
e.put(pow, res); // saves each intermediate result in the cache
}
}
return res;
}
}
help me solve this and for test cases I need all

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!