首页 > 解决方案 > 我可以在不使用序列化的情况下编写此代码吗?

问题描述

对于我的项目,我想知道是否有一种方法可以在不使用序列化的情况下完成这项任务。以下是项目指南和我已经拥有的代码:

加拿大林务局想要对森林的生长和修剪做一个简单的模拟。每个森林都有一个名字,正好有 10 棵树。树木在1'至5'高时种植,每棵树每年的个体增长率为50%-100%。对于模拟,新树是在这些范围内随机构建的。(伐木工人)按需收割森林——所有高于指定高度的树木都被砍伐并用新树代替。

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

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

第一类

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

2 类

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

    }
}

标签: java

解决方案


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

如果您的意思是更广泛的序列化,那么没有。文件不能直接存储 java 对象,您必须将它们转换为字节(根据定义这是序列化)。

PS:如果你真的问“如何?” 你应该把它包括在你的问题中。


推荐阅读