Write a program that models a snail ranch, as follows.
+REPRODUCTION_RATE : int = 100 {readOnly}
+BASE_POPULATION : int = 200 {readOnly}
-population : int
+SnailRanch(population : int)
+getPopulation() : int
+breed() : void
+harvest() : int
getPopulation() returns the current population, breed() causes the snails to breed
harvest() harvests the snails, returning the yield, the number of snails that could be harvested,
reducing the population to no lower than BASE POPULATION
REPRODUCTION RATE is the number of children per pair of snails
BASE POPULATION is the number of snails to be left in the ranch after a harvest, population is the current population of the ranch
SnailRanch(int) is a constructor that establishes a ranch with an initial population
Implement a Main class that reads a file of events and prints the events, the current
population after each event, and after all events have occurred, prints the total yield from the ranch.The name of the file of events will be supplied as a command line argument.
if the file txt contains these events:
stock 100
breed
breed
harvest
breed
harvest
then a run of the program would look like this:
$ java Main log.txt
stock 100
breed 5100
breed 260100
harvest 200
breed 10200
harvest 200
total yield = 269900
$
The first event will always be "stock" with the initial population of the ranch.
import java.io.*;
public class Main {
public static void main(String[] argv) {
if (argv.length < 1) {
System.out.println("No input args");
}
BufferedReader reader = null;
try {
FileReader eventsFile = new FileReader(argv[0]);
reader = new BufferedReader(eventsFile);
String line = reader.readLine();
SnailRanch ranch = null;
int totalYield = 0;
while(line != null) {
if (line.contains("stock")) {
String[] parts = line.split(" ");
String snailPopulation = parts[parts.length - 1];
int population = Integer.parseInt(snailPopulation);
System.out.println(line);
ranch = new SnailRanch(population);
} else if (ranch != null) {
switch (line) {
case "breed":
ranch.breed();
break;
case "harvest":
totalYield += ranch.harvest();
break;
default:
line = reader.readLine();
continue;
}
System.out.println(line + " " + ranch.getPopulation());
}
line = reader.readLine();
}
System.out.println("total yield = " + totalYield);
}
catch (IOException ex) {
System.out.println("No such file");
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
System.out.println("Can't close file");
}
}
}
static class SnailRanch {
public static final int REPRODUCTION_RATE = 100;
public static final int BASE_POPULATION = 200;
private int population;
public SnailRanch(int population) {
this.population = population;
}
public int getPopulation() {
return population;
}
public void breed() {
population += (population / 2) * REPRODUCTION_RATE;
}
public int harvest() {
int harvested = population - BASE_POPULATION;
population = BASE_POPULATION;
return harvested;
}
}
}
Need a fast expert's response?
Submit order
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!
Learn more about our help with Assignments:
JavaJSPJSF