2012-12-04 21 views
-3

嘿這裏大家是我的代碼:集成類

​​

這裏是我的集成類:

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.爲什麼它沒有采取積分類的sumf

+0

我在回答中寫了你該怎麼做,不是嗎?您的更改不正確。如果你不明確地調用這些方法,你的總和將總是'0'。 – bellum

+0

您的權利我沒有在您的評論中看到Integration.Areaf部分。謝謝 – CraZieR

+0

隨着我上面得到的代碼,什麼是調用方法的最佳方式,我只是有點困惑 – CraZieR

回答

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子句或以正確的方式進行調整。

+0

sum = =無關我可以把它命名爲spongebob = area1 - area2。 – CraZieR

+0

@CraZieR在編程方面並不好。 – bellum