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!! SmellAlmostbest.java package org.example;
import java.util.HashMap;
import java.util.Map;
This code will return the st argument to the power of all subsequent arguments
This one caches our results for later reuse
public class SmellAlmostBest
private static Mapcache new HashMap;
public static void mainString args
if argslength
System.out.printlnUsage: java sersmells.SmellAlmostBest ;
System.exit;
int num Integer.parseIntargs;
long timer System.currentTimeMillis;
for int j ; j args.length; j
System.out.println
"The argsjth power of num is toPowernum Integer.parseIntargsj;
System.out.printlnCompleted args iterations in SystemcurrentTimeMillis timerms;
What is the inefficiency here?
public static int toPowerint n int pow
Map entry cache.getn;
Integer res null;
if entry null
res entry.getpow;
if res null
res calcPowern pow;
entry.putpow res;
else
res calcPowern pow;
entry new HashMap;
entry.putpow res;
cache.putn entry;
return res;
private static int calcPowerint n int pow
if pow
return ;
int res ;
for int i ; i pow; res n i
;
;
;
return res;
IPower.java package org.example;
import java.util.HashMap;
import java.util.Map;
public interface IPower
int toPowerint n int pow;
class PowerSimple implements IPower
public int toPowerint n int pow
if pow
return ;
int res ;
for int i ; i pow; res n i
;
;
;
return res;
class PowerCached implements IPower
private static Mapcache new HashMap;
resolves the inefficiency in AlmostBest
public int toPowerint n int pow
Map entry PowerCached.cache.getn;
if entry null
entry new HashMap;
return toCachedPowerentry n pow;
The contract here is we know there is a cache entry so no check required
DBC
private int toCachedPowerMap e int n int pow
Integer res null;
if pow
res ;
else
res egetpow;
if res null
return res;
else
res n toCachedPowere n pow ;
eputpow res; saves each intermediate result in the cache
return res;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
