-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;
}

好的,我刚刚编辑了这个并修复了areaf和areag问题,但是由于某种原因,当我得到f曲线下的面积时,我仍然得到0。为什么它不从Integration类中获取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 回答