Java: How can I make this calculus method on limits more efficient?

how can I initialize all of my variables more easier?

is there a calculus package?

is there a more efficient solution overall?

import java.util.Scanner;

public class limits {

public static void main(String[] args)
{
    String introMessage = "          ***Calculus: Limits***"                 + "\n"
            +             "This application uses the method of exhaustion"   + "\n"
            +             "to test limits. You enter the number that x"      + "\n"
            +             "approches and this program will give you three"   + "\n"
            +             "numbers on either side of the limit showing"      + "\n"
            +             "closer approximations of the limit.";
    System.out.println(introMessage);
    System.out.println();
    String polynomialMessage = "Our function: "                 + "\n"
                            +  "  lim   f(x) = x^2 + x + 1 = L" + "\n"
                            +  "x -> a";
    System.out.println(polynomialMessage);
    System.out.println();
    System.out.println("As x approaches a, what will our limit L be?");
    System.out.println();

can I initialize all of these at once?

    // initialize variables
    double belowAOne = 0.0;
    double belowATwo = 0.0;
    double belowAThree = 0.0;
    double belowAFour = 0.0;
    double aboveAOne = 0.0;
    double aboveATwo = 0.0;
    double aboveAThree = 0.0;
    double aboveAFour = 0.0;

    double totalBAOne = 0.0;
    double totalBATwo = 0.0;
    double totalBAThree = 0.0;
    double totalBAFour = 0.0;
    double totalAAOne = 0.0;
    double totalAATwo = 0.0;
    double totalAAThree = 0.0;
    double totalAAFour = 0.0;

    double L = 0;

    // create a Scanner object named sc
    Scanner sc = new Scanner(System.in);

    // perform invoice calculations until choice isn't equal to "y" or "Y"
    String choice = "y";
    while (!choice.equalsIgnoreCase("n"))
    {
      System.out.println("Please enter a whole number between 1 and 10 for a: ");
      double a = sc.nextDouble();

      if (a > 0 && a <=10 )
      {
      // calculate L
      L = (a*a) + a + 1;

      // create values that approaches a
      belowAOne = a - .5;
      belowATwo = a - .1;
      belowAThree = a - .01;
      belowAFour = a - .001;
      aboveAOne = a + .5;
      aboveATwo = a + .1;
      aboveAThree = a + .01;
      aboveAFour = a + .001;

      totalBAOne = (belowAOne * belowAOne) + belowAOne + 1;
      totalBATwo = (belowATwo * belowATwo) + belowATwo + 1;
      totalBAThree = (belowAThree * belowAThree) + belowAThree + 1;
      totalBAFour = (belowAFour * belowAFour) + belowAFour + 1;
      totalAAOne = (aboveAOne * aboveAOne) + aboveAOne + 1;
      totalAATwo = (aboveATwo * aboveATwo) + aboveATwo + 1;
      totalAAThree = (aboveAThree * aboveAThree) + aboveAThree + 1;
      totalAAFour = (aboveAFour * aboveAFour) + aboveAFour + 1;

      String chart = "      x     " +   "x^2 + x + 1"    + "\n"
              +         "---------+--------------"       + "\n"
              + "     " + belowAOne +   "   :   " + totalBAOne   + "\n"
              + "     " + belowATwo +   "   :   " + totalBATwo   + "\n"
              + "     " + belowAThree + "  :   " + totalBAThree  + "\n"
              + "     " + belowAFour  + " :   " + totalBAFour    + "\n"
              + "     " + " a " +       "   :   " + "L"          + "\n"
              + "     " + aboveAFour +  " :   " + totalAAFour    + "\n"
              + "     " + aboveAThree + "  :   " + totalAAThree  + "\n"
              + "     " + aboveATwo   + "   :   " + totalAATwo   + "\n"
              + "     " + aboveAOne  +  "   :   " + totalAAOne   + "\n";
      System.out.println();
      System.out.println();
      System.out.println();
      System.out.println(chart);
      System.out.println();
      System.out.println("As X approaches " + a + ", our guess "
              +          "for L is: " + L);
      System.out.println();

      // end the program
      choice = "n";
      }

      else
      {
       System.out.println();
       System.out.println("***Invalid Entry***");
       System.out.println();
      }
    }
}

}

Is there a better way to write this program?


Download java.how.can.i.make.this.calculus.method.on.limits.more.efficient.zip
Direct Link


Download


Download java.how.can.i.make.this.calculus.method.on.limits.more.efficient.zip
Mediafire


Download


Download java.how.can.i.make.this.calculus.method.on.limits.more.efficient.zip
GigaPeta


Download


Download java.how.can.i.make.this.calculus.method.on.limits.more.efficient.zip
Fileparadox


Download


Download java.how.can.i.make.this.calculus.method.on.limits.more.efficient.zip
Anafile


Download

Both comments and pings are currently closed.

Comments are closed.