[英]Can I write this code without using serialization?
For my project I was wondering whether there is a way I can do this assignment without using serialization.对于我的项目,我想知道是否有一种方法可以在不使用序列化的情况下完成此任务。 Here are the guidelines to the project and the code I already have together:
以下是该项目的指导方针和我已经拥有的代码:
The Canadian Forest Service wants to do a simple simulation of the growth and pruning of forests.加拿大林务局希望对森林的生长和修剪进行简单的模拟。 Each forest has a name and exactly 10 trees.
每个森林都有一个名字,正好有 10 棵树。 The trees are planted when they are 1' to 5' tall, and each tree has a individual growth rate of 50%-100% per year.
树木长到 1 英尺到 5 英尺时种植,每棵树每年都有 50%-100% 的个体增长率。 For the simulation new trees are constructed randomly within these bounds.
为了模拟,在这些边界内随机构造新树。 A forest is reaped (by lumberjacks) on demand - all trees above a specifed height are cut down and replaced with new trees.
森林(由伐木工人)按需收割——所有高于指定高度的树木都被砍伐并替换为新的树木。
The user interface to the simulation must allow the user to:模拟的用户界面必须允许用户:
Display the current forest (with tree heights to 2 decimal places) Discard the current forest and create a new forest Simulate a year's growth in the current forest Reap the current forest of trees over a user specified height, replacing the reaped trees with random new trees.显示当前森林(树木高度保留 2 个小数位) 丢弃当前森林并创建一个新森林 模拟当前森林中一年的增长 在用户指定的高度上收获当前森林,用随机的新树木替换收获的树木. Save the information about the current forest to file (named after the forest) Discard the current forest and load the information about a forest from a file.
将当前林的信息保存到文件(以林命名) 丢弃当前林,从文件中加载林的信息。
Class1 1级
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 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))));
}
}
If by serialization you understand standard java serialization with ObjectXXXStream
, then yes, you can avoid it.如果通过序列化您了解
ObjectXXXStream
标准 java 序列化,那么是的,您可以避免它。
If you mean serialization in more broad way, then no.如果您的意思是更广泛的序列化,那么不。 Files cant directly store java objects you have to convert them to bytes (which is serialization by definition).
文件不能直接存储 java 对象,您必须将它们转换为字节(根据定义,这是序列化)。
PS: If you actually ask "How?" PS:如果你真的问“怎么样?” you should include it in your question.
你应该把它包括在你的问题中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.