-3

みなさん、ここに私のコードがあります:

package Chapter5;

import java.util.Scanner;


public class MainMenu extends Integration
{

static Scanner s = new Scanner(System.in);
static int c;
static double lower, upper;
static double Curves;
static double sum = 0;


public static void main(String[] args) 
{

    while(true)
    {
        System.out.println("Please choose one of the options");
        System.out.println("1: Calculate the area between two curves");
        System.out.println("2: Calculate the volume when two curves are revolved about a disk");
        System.out.println("3: Calculate the volume when a single curve is revolved about a shell");
        System.out.println("4: Quit");
        c = s.nextInt();
        if(c == 1)
        {
            System.out.println("Area between two curves");
            System.out.println("Please enter the lower limit");
            lower = s.nextInt();
            System.out.println("Please enter the upper limit");
            upper = s.nextInt();
            System.out.print("Lower limit: " + lower);
            System.out.println("  Upper limit: " + upper);
            System.out.println("The area under the f curve: " + sumf);
            System.out.println("The area under the g curve: " + sumg);
            sum = sumf - sumg;
            System.out.println("Area between the two curves = " + sum);
        }
        if(c == 2)
        {
            System.out.println("Working");
        }
        if(c == 3)
        {
            System.out.println("Working");
        }
        if(c == 4)
        {
            break;

        }
    }
}

ここに私の統合クラスがあります:

   package Chapter5;

   /*Author: Cory Zander
   * Purpose: Find the area and volume between two curves
   */
   public class Integration 
   {

static double x;
static double dx;
static double f, g;
static double sumg = 0;
static double sumf = 0;
public static double f(double x) 
{
    f = x * Math.exp(x);
    return x;


}
public static double g(double x) 
{
    g = Math.pow(x, 2);
    return x;

}

public static double Areaf(double lower, double upper)
{
    int i;
    x = lower;
    dx = (upper - lower)/2000;
    for(i = 0; i <= 2000; i++)
    {
        if(i == 0 || i == 2000)
        {
            sumf += f;
        }
        else
        {
            sumf += 2 * f;
        }
        x += dx;


    }
    sumf *= dx/2;
    return sumf;


}
public static double Areag(double lower, double upper)
{
    int i;
    x = lower;
    dx = (upper - lower)/2000;
    for(i = 0; i <= 2000; i++)
    {
        if(i == 0 || i == 2000)
        {
            sumg += g;
        }
        else
        {
            sumg += 2 * g;
        }
        x += dx;


    }
    sumg *= dx/2;
    return sumg;
}

さて、これを編集して、面積と面積の問題を修正しましたが、何らかの理由で、f 曲線の下の面積を取得すると、まだ 0 になります。統合クラスから sumf を取得しないのはなぜですか

4

1 に答える 1

0
public static void main(String[] args){
    if (c == 1){
        //...
        double area1 = Integration.Areaf(lower, upper);
        double area2 = Integration.Areag(lower, upper);
        sum = area1 - area2;//sum and minus used?
        System.out.println("Area between the two curves = " + sum);
   }
}

また、継承を誤って使用したため、基本クラス ( Integration) 内のすべてのメソッドがstatic. extendsサブクラスから句を削除するか、正しい方法で調整するだけです。

于 2012-12-04T23:10:49.557 に答える