Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

JAVA "to the power of"

Name: Anonymous 2011-12-10 17:01

Sup /prog/?

I know this is a retarded question, but I need some help with a method I need to write. The method parameters are a double and an int (a and b, respectively)

I need to write a for loop in the method that raises a (the double) to the power of b (the int)

How the fuck do I do this?? Normally this wouldn't be a problem, but I'm burnt out from finals

Name: Anonymous 2011-12-12 4:02

None of this code is ENTERPRISE QUALITY, /prog/!


import java.util.HashMap;

public class PowerFactory
{
  private static PowerFactory powerFactoryFactory = null;

  public static PowerFactory getPowerFactory()
  {
    if (powerFactoryFactory = null)
      powerFactoryFactory = new PowerFactory();
    return powerFactoryFactory;
  }

  private HashMap<Double, HashMap<Integer, Double>> pows;

  private PowerFactory()
  {
    pows = new HashMap<Double, HashMap<Integer, Double>>();
  }

  public double power(int a, double b)
  {
    double ans = 0;
    HashMap<Integer, Double> ins
    if (pows.containsKey(b))
    {
      ins = pows.get(b);
      if (ins.containsKey(a))
        ans = ins.get(a);
      else
      {
        ans = raise(a, b);
        ins.put(a, ans);
      }
    }
    else
    {
      ins = new HashMap<Integer, Double>();
      ans = raise(a, b);
      ins.put(a, ans);
      pows.put(b, ins);
    }
    return ans;
  }

  private static raise(int a, double b)
  {
    double res = b;
    for (int i = 1; i < a; i++)
      res *= b;
    return res;
  }
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List