Java:类问题

时间:2016-02-19 16:23:52

标签: java class object timestamp simulation

我正在尝试编写一个程序来执行以下操作

1.给下雨量,看看有多少树死亡,并且生长停滞

2.给下雨量,看看甲虫杀死了多少棵树

3.给出被火烧死的树木的百分比,杀死那些在课堂上的树木,然后每三个被火烧死的树木,再种下一棵树。

4.对于每个变成5的树,收获它,并种植一棵新树。

    public class Simulation2 {

    public static void main(String[] args) throws IOException {
        PrintWriter outpt;

        double beetle1[]={0.1,0.05,0.0};  //index 0=1-3 inches
        double beetle2[]={0.15,0.05,0.0}; //index 1= 4-10 inches
        double beetle3[]={0.30,0.1,0.02}; //index 2= 11 or greater
        double beetle4[]={0.30,0.1,0.02}; //beetles

        double tree1[]={0.10,0.70,0.05,0.01,0.02,0.02};     //index 0-1 = 1-3 inches rain
        double tree2[]={0.10,0.75,0.05,0.02,0.03,0.03};     //index 2-3 = 4-10 inches rain
        double tree3[]={0.30,0.60,0.05,0.02,0.03,0.03};     //index 4-5 = 11 or greater inches rain
        double tree4[]={0.35,0.65,0.05,0.01,0.04,0.04};     //first index = dying, next = not growing
                                                            // 1.00-first index-second index = percent growing

        tree onetree= new tree(400000.0,0.0,beetle1,tree1);
        tree twotree= new tree(300000.0,0.0,beetle2,tree2); //create the different year old trees and pass in the variables
        tree redtree= new tree(200000.0,0.0,beetle3,tree3);
        tree bluetree= new tree(100000.0,0.0,beetle4,tree4);
        int year; //what year it is
        int itype; //type of rain
        int choice = 2; //choice of which policie you'd like to run; 1-4
        for(year=1;year<=50;year++)
        {
            if (choice == 1 || choice == 4) //if choice is 1 or 4, get itype from seeded rain
                itype=seededRain();
            else
                itype=rain(); //gets the rain type --- 0 for drought, 1 for moderate, 2 for heavy

            onetree.rainCircumstances(itype);
            twotree.rainCircumstances(itype);
            redtree.rainCircumstances(itype); //pass the trees the rain type
            bluetree.rainCircumstances(itype);

            onetree.survBeetles(itype);
            twotree.survBeetles(itype);
            redtree.survBeetles(itype);    //pass the trees the rain type for beetles
            bluetree.survBeetles(itype);
                                                                        //Working on the assumption Four year olds
                                                                        //are cut when the growing season ends
                                                                        //when they turn 5
                                                                        //before the forest fires can get to them
            twotree.setRegTree(onetree.getRegTree()+twotree.getTreesNotGrowing());
            //set 2yr old tree by aging one year old and not growing the 2yr old trees affected by rain
            redtree.setRegTree(twotree.getRegTree()+redtree.getTreesNotGrowing());
            //set 3yr old tree by aging 2yr old and not growing 3yr old trees affected by rain
            redtree.setResistentTree(redtree.getResTreesNotGrowing());
            //do the same for 3yr old resistent trees
            bluetree.setRegTree(redtree.getRegTree()+bluetree.getTreesNotGrowing());
            //set 4yr old trees by aging 3yr old trees and not growing 3yr old trees affected by rain amount
            bluetree.setResistentTree(redtree.getResistentTree()+bluetree.getResTreesNotGrowing());
            //same here
            if (choice == 2)
            {
                redtree.sprayTrees(1); //if choice is 2 spray all three year old trees
            }

            if (choice == 3)
            {
                redtree.sprayTrees(0.50); //if choice is 3 spray 50% of both 3 and 4 yr old trees
                bluetree.sprayTrees(0.50);
            }

            if (choice == 4)
            {
                bluetree.sprayTrees(0.50); //if choice is 4 spray just 50% of 4 yr old trees
            }

            double x;
            if (choice == 1 || choice == 4)
                x=wildFireSeeding(); 
            else                        //gets percent of trees destroyed in wildfires
                x=wildFire();           //based on if clouds are seeded or not
            onetree.fireCasualities(x);
            twotree.fireCasualities(x); //sends that % to firecasualities function
            redtree.fireCasualities(x);
            bluetree.fireCasualities(x);


            double treesToPlant = bluetree.totalTrees()+onetree.getTreesKilled()+twotree.getTreesKilled()+redtree.getTreesKilled()+bluetree.getTreesKilled();
            //calculates how many trees to plant based on trees harvested, and trees killed by forest fire
            onetree.setRegTree(treesToPlant+onetree.getTreesNotGrowing());
            //sets onetree's tree amount by adding treestoplant along with the trees that didnt grow previous year

            onetree.calcstats(); //calculates the sum of trees and sum^2
            twotree.calcstats();
            redtree.calcstats();
            bluetree.calcstats();

//          if (year < 4)
//          {
//              System.out.println("Rain Type: "+itype+" WIld fire type: "+ x);
//              System.out.println("Trees to plant" + treesToPlant);
//              System.out.println("Four Year Olds Cut: " + fourYearOldsCut);
//              System.out.println(" ");
//              
//          }



        }
        //Prints out the mean in equivalent age 
        System.out.println("Mean in equivalent age in order of 1 thru 4: " + onetree.getAverage()+ " " + twotree.getAverage()+" "+redtree.getAverage()+" " + bluetree.getAverage());
        System.out.println("Variance in equivalent age categories in 1 thru 4: "+onetree.getVariance()+ " " + twotree.getVariance()+" "+redtree.getVariance()+" " + bluetree.getVariance());
    }
//rain and wildfire functions ommitted to save time
static class tree
{
    private double regTree; //regular trees 
    private double resistentTree; //resistenet trees
    private double stuntedGrowth; //dont believe I used this
    private double treesNotGrowing; //trees that didnt grow due to rain
    private double resTreesNotGrowing; //resistent trees that remained stagnent that year
    private double sumTree; //sum of the trees
    private double sumTree2; // sum of the trees squared
    private double beetleDamage[]={0.0,0.0,0.0};
    private double rain[]={0.0,0.0,0.0,0.0,0.0,0.0};
    private double treesKilled; //number of trees killed 

    //getters and setters ommitted
    public void calcstats()
    {
        sumTree+=regTree+resistentTree; //calculates sum of trees
        sumTree2+=(regTree+resistentTree)*(regTree+resistentTree);
    }

    public tree(double original, double resist, double beetle[], double rainIn[])
    {
        int i;

        regTree=original;
        resistentTree=resist;

        for (i=0;i<=2;i++)
        {
            beetleDamage[i]=beetle[i];          
        }

        for (i=0; i<=5;i++)
        {
            rain[i]=rainIn[i];
        }
        sumTree=sumTree2=0.0;
        stuntedGrowth=0.0;
        treesNotGrowing=0.0;
    }

    public void rainCircumstances(int wtype)
    {
        if (wtype == 0) //drout
        {
            regTree*=1.0-rain[0];  //calculates how many trees died due to rain
            resistentTree*=1.0-rain[0]; //calculates how many resistent trees died due to rain
            double percentGrowthStalled = rain[1]; //gets the percent of trees that will stagnate due to rain
            treesNotGrowing=percentGrowthStalled*regTree; //gets number of trees not growing
            resTreesNotGrowing=percentGrowthStalled*resistentTree; //gets number of resistent trees not growing
            regTree=regTree-treesNotGrowing; //subtracts trees not growing from regTree
            resistentTree= resistentTree-resTreesNotGrowing; //subtracts resistent trees not growing from resistenTree
        }

        if (wtype == 1)
        {
            regTree*=1.0-rain[2];                           //see what trees survive the rain/weather for the year
            resistentTree*=1.0-rain[2];                     //moderate
            double percentGrowthStalled = rain[3];
            treesNotGrowing=percentGrowthStalled*regTree;
            resTreesNotGrowing=percentGrowthStalled*resistentTree;
            regTree=regTree-treesNotGrowing;
            resistentTree= resistentTree-resTreesNotGrowing;
        }
        if (wtype == 2)
        {
            regTree*=1.0-rain[4];       //percent of trees dying due to rain
            resistentTree*=1.0-rain[4]; //percent of trees dying due to rain
            double percentGrowthStalled = rain[5];
            treesNotGrowing=percentGrowthStalled*regTree;
            resTreesNotGrowing=percentGrowthStalled*resistentTree;
            regTree=regTree-treesNotGrowing;
            resistentTree= resistentTree-resTreesNotGrowing;
        }
        return;
    }

    public void survBeetles(int wtype)
    {
        regTree*=1.0-beetleDamage[wtype]; //calculates how many trees are left after the beetle attack
    }


    public void fireCasualities(double percentKilled)
    {
        double treesAtStart=regTree+resistentTree; //calculates how many trees there are at start
        regTree*=(1.0-percentKilled);               //calculates trees left after fire casualties
        resistentTree*=(1.0-percentKilled);         //calculates resistent trees left after fire casualties
        treesKilled=treesAtStart-regTree-resistentTree; //gets total trees killed so we can plant them again
        return;
    }

    public double totalTrees()
    {
        double totalTrees=regTree+resistentTree; //adds regtree and resistenttree together to  see how many trees we have
        return totalTrees;
    }
    public void sprayTrees(double percentToSpray)
    {
        resistentTree+=regTree*percentToSpray; //takes a percent in decimal format, sprays reg trees creating resistent trees
        regTree-=regTree*percentToSpray;       //subtract the newly sprayed trees from regTree
    }

    public double getAverage()
    {
        double average;
        average=sumTree/50; //gets average
        return average;
    }

    public double getVariance()
    {
        double variance;
        variance=sumTree2/50-(sumTree/50)*(sumTree/50); //gets variance
        return variance;
    }


}




}

当我去抓树的统计数据时,我收到了一些疯狂的高数字,我相信问题出在我rainCircumstances课程的tree函数中,但看了几个小时之后我无法看到我出错的地方,并希望另一双眼睛可以帮助我。

我为添加如此多的代码而道歉,但是经过几个小时我已经想要给另一组眼睛充分了解我正在使用的内容。谢谢你的时间!!

0 个答案:

没有答案