我可以在不使用序列化的情况下编写此代码吗?

时间:2019-04-30 03:54:11

标签: java

对于我的项目,我想知道是否有一种方法可以在不使用序列化的情况下进行此分配。以下是该项目的准则以及我已经拥有的代码:

加拿大森林服务局希望对森林的生长和修剪进行简单的模拟。每个森林都有一个名称和正好10棵树。当树木高1'至5'时就进行种植,每棵树的年增长率为50%-100%。为了模拟,在这些范围内随机构建新树。按需砍伐森林(伐木工人)-超过指定高度的所有树木都将被砍伐并换成新树木。

模拟的用户界面必须允许用户:

显示当前森林(树高到小数点后2位) 丢弃当前目录林并创建一个新目录林 模拟当前森林中一年的增长 收获超过用户指定高度的当前树木林,用随机的新树替换收割的树。 将有关当前林的信息保存到文件(以林命名) 丢弃当前目录林并从文件中加载有关目录林的信息。

Class1

import java.io.*;
import java.util.*;
public class Forest{

//constants
    private static final int MAX_NUM_TREES = 10;

//variables
    int index;
    private String name;
    private Tree[] arrayOfTrees;
    public Forest(String forestName){
//Constructor class that takes a name and creates an array of trees().
        index = 0;
        name = forestName;
        arrayOfTrees = new Tree[MAX_NUM_TREES];

        for(index = 0; index < arrayOfTrees.length; index++){

            arrayOfTrees[index] = new Tree();

        }
    }

    public void display(){
// displays the array of trees and the index
        index = 0;

        if(name != null){

            System.out.println(name);
            for(index = 0; index < arrayOfTrees.length; index ++){
                System.out.printf("%2d   :   %s\n", (index + 1), arrayOfTrees[index]);
            }
        }else{
            System.out.println("No forest.");
        }

    }
   public void yearGrowth(){
//grows each tree in the array
        index = 0;

        for(index = 0; index < arrayOfTrees.length ; index ++){

            arrayOfTrees[index].grow();
        }

    }
   public void reap(int reapHeight){
        //reaps the trees and prints out the old and new information
        index = 0;


        for(index = 0; index < arrayOfTrees.length; index++){

            if(arrayOfTrees[index].getHeight() >= reapHeight){

                System.out.println("Cut " + (index+1) + " : " + arrayOfTrees[index] );
                arrayOfTrees[index] = new Tree();
                System.out.println("New " + (index+1) + " : " + arrayOfTrees[index] );

            }
        }

    }
public static void saveForest(Forest forest) throws IOException {
//saves the forest
        String name = forest.getName();
        ObjectOutputStream toStream;

        toStream = new ObjectOutputStream(new FileOutputStream(name));
        toStream.writeObject(forest);
        toStream.close();
    }

   public static Forest loadForest(String fileName) throws IOException {
//loads the forest
        ObjectInputStream fromStream = null;
        Forest local;

        fromStream = new ObjectInputStream(new FileInputStream(fileName));
        try {
            local = (Forest)fromStream.readObject();
        }catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
            return(null);
        }finally{
            try {
                if (fromStream != null) {
                    fromStream.close();
                }
            } catch (IOException e) {
                System.out.println(e.getMessage());
                return(null);
            }
        }
        return(local);
    }
    public String getName(){

        return (name);
    }
}

Class2

import java.util.Random;
import java.util.*;
import java.io.*;

public class Tree{

//creates the variables as the
    private double height;
    private double growthRate;
    private static Random rand = new Random();
    final double MIN_HEIGHT = 1;
    final double MIN_GROWTH_RATE = 0.5;
    final double MAX_HEIGHT = 5;
    final double MAX_GROWTH_RATE = 1.0;

    public Tree() {
//creates tree with a height and a growth rate
        Random rand = new Random();

        height = (MIN_HEIGHT + ((Math.random() * (MAX_HEIGHT - MIN_HEIGHT))));
        growthRate = (MIN_GROWTH_RATE + (Math.random() * (MAX_GROWTH_RATE - MIN_GROWTH_RATE)));


    }

    public double grow(){
//tree grows and returns height

        height = height * (1 + growthRate);
        return height;


    }

    public double getHeight(){

        return (height);

    }

    public double getGrowthRate(){

        return (growthRate);

    }

    public String toString(){
//toString formats the output with height and growthrate

        return (String.format("%7.2f (%2d%% pa)", height, ((int)(growthRate * 100))));

    }
}

1 个答案:

答案 0 :(得分:0)

如果通过序列化了解了ObjectXXXStream的标准Java序列化,那么可以,可以避免。

如果您是说以更广泛的方式进行序列化,则不会。文件不能直接存储Java对象,您必须将其转换为字节(按定义序列化)。

PS:如果您实际上问“如何?”您应该将其添加到您的问题中。