HELP! java programming here are the example outputs and inputs: $ java Farmbot I
ID: 662694 • Letter: H
Question
HELP! java programming here are the example outputs and inputs:
$ java Farmbot
Invalid command line arguments
Usage: java Farmbot
----------------------------------------
$ java Farmbot 4 2
> STATUS
Day 1 - 100 coins
> SHOW height
0 0 0 0
0 0 0 0
> SHOW water
0 0 0 0
0 0 0 0
> WATER 1 1 4 2
Cost 80 coins
> NEXT
Day 2 - 20 coins
> NEXT
Day 3 - 20 coins
> SHOW height
2 2 2 2
2 2 2 2
> SHOW water
6 6 6 6
6 6 6 6
> INFEST 1 1
Collect 20 coins
> NEXT
Day 4 - 40 coins
> SHOW height
1 3 3 3
3 3 3 3
> SHOW water
6 5 5 5
5 5 5 5
> HARVEST 1 1
Collect 0 coins
> SHOW height
1 3 3 3
3 3 3 3
> NEXT
Day 5 - 40 coins
> BYE
bye
--------------------------------------------------------------
$ java Farmbot 4 4
> STATUS
Day 1 - 100 coins
> WATER 1 1 4 2
Cost 80 coins
> SHOW WATER
8 8 8 8
8 8 8 8
0 0 0 0
0 0 0 0
> NEXT
Day 2 - 20 coins
> NEXT
Day 3 - 20 coins
> NEXT
Day 4 - 20 coins
> NEXT
Day 5 - 20 coins
> SHOW water
4 4 4 4
4 4 4 4
0 0 0 0
0 0 0 0
> SHOW height
4 4 4 4
4 4 4 4
0 0 0 0
0 0 0 0
> INFEST 2 2
Collect 20 coins
> SHOW infested
. . . .
. x . .
. . . .
. . . .
> NEXT
Day 6 - 40 coins
> SHOW infested
x x x .
x x x .
. . . .
. . . .
> SHOW height
5 5 5 5
5 3 5 5
0 0 0 0
0 0 0 0
> SHOW water
3 3 3 3
3 4 3 3
0 0 0 0
0 0 0 0
> HARVEST 1 1 4 2
Collect 100 coins
> SHOW height
5 5 5 0
5 3 5 0
0 0 0 0
0 0 0 0
> NEXT
Day 7 - 140 coins
> SHOW height
4 4 4 1
4 2 4 1
0 0 0 0
0 0 0 0
> SHOW water
3 3 3 2
3 4 3 2
0 0 0 0
0 0 0 0
> SHOW infested
x x x .
x x x .
. . . .
. . . .
> SPRAY 1 1 3 2
Cost 120 coins
> STATUS
Day 7 - 20 coins
> SHOW infested
. . . .
. . . .
. . . .
. . . .
> HARVEST 1 1 4 4
Collect 240 coins
> SHOW height
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
> WATER 1 1 4 4
Cost 160 coins
> NEXT
Day 8 - 100 coins
> SHOW height
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
> BYE
import java.util.*;
public class Farmbot {
private static Farm farm;
private static Scanner scan;
public static void main(String[] args) {
init(args);
process();
}
// Initialises program based on the command line arguments
public static void init(String[] args) {
int width = 0;
int height = 0;
// Attempt to parse command line arguments
// ...
// Initialise farm with given width and height
// ...
// Initialise scanner to read from standard input
scan = new Scanner(System.in);
}
// Displays command line usage and terminates the program
public static void usage() {
System.out.println("Invalid command line arguments");
System.out.println("Usage: java Farmbot <width> <height>");
System.exit(1);
}
// Process commands
public static void process() {
// Display the prompt
System.out.printf("> ");
// Read from standard input until EOF
while (scan.hasNextLine()) {
// Parse command
String line = scan.nextLine().toLowerCase();
String[] tokens = line.split(" ");
String command = tokens[0];
switch (command) {
case "": break;
case "bye": byeCommand(); break;
case "help": helpCommand(); break;
case "next": nextCommand(); break;
case "status": statusCommand(); break;
case "show": showCommand(tokens); break;
case "water": waterCommand(tokens); break;
case "spray": sprayCommand(tokens); break;
case "infest": infestCommand(tokens); break;
case "harvest": harvestCommand(tokens); break;
default: System.out.println("Invalid command");
}
// Display the prompt
System.out.printf(" > ");
}
System.out.printf("bye ");
}
// Displays bye and terminates the program
public static void byeCommand() {
System.out.println("bye");
System.exit(0);
}
// Displays help message
public static void helpCommand() {
System.out.println("BYE");
System.out.println("HELP");
System.out.println();
System.out.println("NEXT");
System.out.println("STATUS");
System.out.println("SHOW <attribute>");
System.out.println();
System.out.println("WATER <x1> <y1> [<x2> <y2>]");
System.out.println("SPRAY <x1> <y1> [<x2> <y2>]");
System.out.println("INFEST <x1> <y1> [<x2> <y2>]");
System.out.println("HARVEST <x1> <y1> [<x2> <y2>]");
}
// Advances the farm to the next day
public static void nextCommand() {
// NEXT
System.out.println("TODO");
}
// Displays the current day and amount of coins held
public static void statusCommand() {
// STATUS
System.out.println("TODO");
}
// Displays the particular state of the crops
public static void showCommand(String[] tokens) {
// SHOW <attribute>
if (tokens.length != 2) {
System.out.println("Invalid arguments");
return;
}
String attribute = tokens[1];
switch (attribute) {
case "water":
System.out.println("TODO");
break;
case "height":
System.out.println("TODO");
break;
case "sprayed":
System.out.println("TODO");
break;
case "infested":
System.out.println("TODO");
break;
default:
System.out.println("Invalid arguments");
}
}
// Waters a crop plot or region of crop plots
public static void waterCommand(String[] tokens) {
// WATER <x1> <y1> [<x2> <y2>]
if (tokens.length == 3) {
int plot[] = parsePlot(tokens);
if (plot == null) {
System.out.println("Invalid arguments");
return;
}
// Water crop plot
// - x1, y1 maps to plot[0], plot[1]
System.out.println("TODO");
} else if (tokens.length == 5) {
int region[] = parseRegion(tokens);
if (region == null) {
System.out.println("Invalid arguments");
return;
}
// Water region of crop plots
// - x1, y1 maps to region[0], region[1]
// - x2, y2 maps to region[2], region[3]
System.out.println("TODO");
} else {
System.out.println("Invalid arguments");
}
}
// Sprays a crop plot or region of crop plots
public static void sprayCommand(String[] tokens) {
// SPRAY <x1> <y1> [<x2> <y2>]
System.out.println("TODO");
}
// Infests a crop plot or region of crop plots
public static void infestCommand(String[] tokens) {
// INFEST <x1> <y1> [<x2> <y2>]
System.out.println("TODO");
}
// Harvests a crop plot or region of crop plots
public static void harvestCommand(String[] tokens) {
// HARVEST <x1> <y1> [<x2> <y2>]
System.out.println("TODO");
}
// Attempts to parse plot coordinate
public static int[] parsePlot(String[] tokens) {
int[] plot = new int[2];
try {
plot[0] = Integer.parseInt(tokens[1]);
plot[1] = Integer.parseInt(tokens[2]);
} catch (Exception e) {
return null;
}
return plot;
}
// Attempts to parse region coordinates
public static int[] parseRegion(String[] tokens) {
int[] region = new int[4];
try {
region[0] = Integer.parseInt(tokens[1]);
region[1] = Integer.parseInt(tokens[2]);
region[2] = Integer.parseInt(tokens[3]);
region[3] = Integer.parseInt(tokens[4]);
} catch (Exception e) {
return null;
}
return region;
}
}
--------------------------------------------------------------------------------------------
public class Farm {
private Crop[][] crops;
public Farm() {
}
}
----------------------------------------------------------------------------------------------
public class Crop {
public Crop() {
}
}
Explanation / Answer
//Farmbot.java
import java.util.*;
public class Farmbot
{
private static Farm farm;
private static Scanner scan;
private static int coins = 100;
private static int MAXCOINS = 100;
private static int dNow = 1;
private static boolean bool_water = false;
private static boolean bool_infest = false;
private static String waterToken[];
private static String infestToken[];
public static void main(String[] args)
{
init(args);
process();
}
public static void init(String[] args)
{
int width = 0;
int height = 0;
width = Integer.parseInt(args[0]);
height = Integer.parseInt(args[1]);
farm = new Farm(height, width);
scan = new Scanner(System.in);
}
public static void usage()
{
System.out.println("Invalid command line arguments");
System.out.println("Usage: java Farmbot <width> <height>");
System.exit(1);
}
public static void process()
{
System.out.printf("> ");
while (scan.hasNextLine())
{
String line = scan.nextLine().toLowerCase();
String[] tokens = line.split(" ");
int command = getChoice(tokens[0]);
for (int i = 0; i < tokens.length; i++)
System.out.print(tokens[i] + " ");
System.out.println();
switch (command)
{
case 1:
byeCommand();
break;
case 2:
helpCommand();
break;
case 3:
nextCommand();
break;
case 4:
statusCommand();
break;
case 5:
showCommand(tokens);
break;
case 6:
waterCommand(tokens);
waterToken = tokens;
bool_water = true;
break;
case 7:
sprayCommand(tokens);
break;
case 8:
infestCommand(tokens);
infestToken = tokens;
bool_infest = true;
break;
case 9:
harvestCommand(tokens);
break;
default:
System.out.println("Invalid command");
}
System.out.printf(" > ");
}
System.out.printf("bye ");
}
public static int getChoice(String command)
{
int value = 0;
if ("bye".equalsIgnoreCase(command))
value = 1;
else if ("help".equalsIgnoreCase(command))
value = 2;
else if ("next".equalsIgnoreCase(command))
value = 3;
else if ("status".equalsIgnoreCase(command))
value = 4;
else if ("show".equalsIgnoreCase(command))
value = 5;
else if ("water".equalsIgnoreCase(command))
value = 6;
else if ("spray".equalsIgnoreCase(command))
value = 7;
else if ("infest".equalsIgnoreCase(command))
value = 8;
else if ("harvest".equalsIgnoreCase(command))
value = 9;
return value;
}
public static void byeCommand()
{
System.out.println("bye");
System.exit(0);
}
public static void helpCommand()
{
System.out.println("BYE");
System.out.println("HELP");
System.out.println();
System.out.println("NEXT");
System.out.println("STATUS");
System.out.println("SHOW <attribute>");
System.out.println();
System.out.println("WATER <x1> <y1> [<x2> <y2>]");
System.out.println("SPRAY <x1> <y1> [<x2> <y2>]");
System.out.println("INFEST <x1> <y1> [<x2> <y2>]");
System.out.println("HARVEST <x1> <y1> [<x2> <y2>]");
}
public static void nextCommand()
{
dNow++;
statusCommand();
if (bool_water == true)
{
if (waterToken.length == 3)
{
int x1 = Integer.parseInt(waterToken[1]);
int y1 = Integer.parseInt(waterToken[2]);
farm.GrowCrop(x1 - 1, y1 - 1, waterToken[0]);
}
if (waterToken.length == 5)
{
int x1 = Integer.parseInt(waterToken[1]);
int y1 = Integer.parseInt(waterToken[2]);
int x2 = Integer.parseInt(waterToken[3]);
int y2 = Integer.parseInt(waterToken[4]);
for (int i = (x1 - 1); i < y2; i++)
{
for (int j = (x1 - 1); j < x2; j++)
{
farm.GrowCrop(i, j, waterToken[0]);
}
}
}
}
if (bool_infest == true)
{
farm.Infest();
farm.Decay();
}
}
public static void statusCommand()
{
System.out.println("Day " + dNow + " - " + coins + " coins");
}
public static void showCommand(String[] tokens)
{
if (tokens.length != 2)
{
System.out.println("Invalid arguments");
return;
}
String attribute = tokens[1];
int choice = 0;
if (attribute.equalsIgnoreCase("water"))
choice = 1;
else if (attribute.equalsIgnoreCase("height"))
choice = 2;
else if (attribute.equalsIgnoreCase("sprayed"))
choice = 3;
else if (attribute.equalsIgnoreCase("infested"))
choice = 4;
switch (choice)
{
case 1:
farm.showStatus(tokens[1]);
break;
case 2:
farm.showStatus(tokens[1]);
break;
case 3:
farm.showStatus(tokens[1]);
break;
case 4:
farm.showStatus(tokens[1]);
break;
default:
System.out.println("Invalid arguments");
}
}
public static void waterCommand(String[] tokens)
{
if (coins >= 10)
{
if (tokens.length == 3)
{
int plot[] = parsePlot(tokens);
if (plot == null)
{
System.out.println("Invalid arguments");
return;
}
if (plot[0] == 0 && plot[1] == 0)
{
System.out.println("Invalid arguments");
return;
}
else
{
int x1 = Integer.parseInt(tokens[1]);
int y1 = Integer.parseInt(tokens[2]);
farm.GrowCrop(x1 - 1, y1 - 1, tokens[0]);
coins -= 10;
System.out.println("Cost 10 coins");
}
}
else if (tokens.length == 5)
{
int region[] = parseRegion(tokens);
if (region == null)
{
System.out.println("Invalid arguments");
return;
}
int x1 = Integer.parseInt(tokens[1]);
int y1 = Integer.parseInt(tokens[2]);
int x2 = Integer.parseInt(tokens[3]);
int y2 = Integer.parseInt(tokens[4]);
int count = 0;
for (int i = (x1 - 1); i < y2; i++)
{
for (int j = (x1 - 1); j < x2; j++)
{
if (coins >= 10)
{
farm.GrowCrop(i, j, tokens[0]);
coins -= 10;
count++;
}
else
{
System.out.println("Not enough coins");
break;
}
}
}
System.out.println("Cost " + (count * 10) + " coins");
}
else
{
System.out.println("Invalid arguments");
}
}
else
System.out.println("Not enough coins");
}
public static void sprayCommand(String[] tokens)
{
if (coins >= 20)
{
if (tokens.length == 3)
{
int plot[] = parsePlot(tokens);
if (plot == null)
{
System.out.println("Invalid arguments");
return;
}
if (plot[0] == 0 && plot[1] == 0)
{
System.out.println("Invalid arguments");
return;
}
else
{
int x1 = Integer.parseInt(tokens[1]);
int y1 = Integer.parseInt(tokens[2]);
farm.GrowCrop(x1 - 1, y1 - 1, tokens[0]);
coins -= 20;
System.out.println("Cost 20 coins");
}
}
else if (tokens.length == 5)
{
int region[] = parseRegion(tokens);
if (region == null)
{
System.out.println("Invalid arguments");
return;
}
int x1 = Integer.parseInt(tokens[1]);
int y1 = Integer.parseInt(tokens[2]);
int x2 = Integer.parseInt(tokens[3]);
int y2 = Integer.parseInt(tokens[4]);
int count = 0;
for (int i = (x1 - 1); i < y2; i++)
{
for (int j = (x1 - 1); j < x2; j++)
{
if (coins >= 20)
{
farm.GrowCrop(i, j, tokens[0]);
coins -= 20;
count++;
}
else
{
System.out.println("Not enough coins");
break;
}
}
}
System.out.println("Cost " + (count * 20) + " coins");
}
else
{
System.out.println("Invalid arguments");
}
}
else
System.out.println("Not enough coins");
}
public static void infestCommand(String[] tokens)
{
if (coins >= 20)
{
if (tokens.length == 3)
{
int plot[] = parsePlot(tokens);
if (plot == null)
{
System.out.println("Invalid arguments");
return;
}
if (plot[0] == 0 && plot[1] == 0)
{
System.out.println("Invalid arguments");
return;
}
else
{
int x1 = Integer.parseInt(tokens[1]);
int y1 = Integer.parseInt(tokens[2]);
farm.GrowCrop(x1 - 1, y1 - 1, tokens[0]);
coins -= 20;
}
System.out.println("Cost 20 coins");
}
else if (tokens.length == 5)
{
int region[] = parseRegion(tokens);
int count = 0;
if (region == null)
{
System.out.println("Invalid arguments");
return;
}
int x1 = Integer.parseInt(tokens[1]);
int y1 = Integer.parseInt(tokens[2]);
int x2 = Integer.parseInt(tokens[3]);
int y2 = Integer.parseInt(tokens[4]);
for (int i = (x1 - 1); i < y2; i++)
{
for (int j = (x1 - 1); j < x2; j++)
{
if (coins >= 20)
{
farm.GrowCrop(i, j, tokens[0]);
coins -= 20;
count++;
}
else
{
System.out.println("Not enough coins");
break;
}
}
}
System.out.println("Cost " + (count * 20) + " coins");
}
else
{
System.out.println("Invalid arguments");
}
}
else
System.out.println("Not enough coins");
}
public static void harvestCommand(String[] tokens)
{
if (tokens.length == 3)
{
int plot[] = parsePlot(tokens);
if (plot == null)
{
System.out.println("Invalid arguments");
return;
}
if (plot[0] == 0 && plot[1] == 0)
{
System.out.println("Invalid arguments");
return;
}
else
{
int x1 = Integer.parseInt(tokens[1]);
int y1 = Integer.parseInt(tokens[2]);
farm.GrowCrop(x1 - 1, y1 - 1, tokens[0]);
coins += 10;
}
System.out.println("Collect 10 coins");
}
if (tokens.length == 5)
{
int region[] = parseRegion(tokens);
if (region == null)
{
System.out.println("Invalid arguments");
return;
}
int x1 = Integer.parseInt(tokens[1]);
int y1 = Integer.parseInt(tokens[2]);
int x2 = Integer.parseInt(tokens[3]);
int y2 = Integer.parseInt(tokens[4]);
int count = 0;
for (int i = (x1 - 1); i < y2; i++)
{
for (int j = (x1 - 1); j < x2; j++)
{
farm.GrowCrop(i, j, tokens[0]);
coins += 10;
count++;
}
}
System.out.println("Collect " + (count * 10) + " coins");
}
System.out.println("harvested");
}
public static int[] parsePlot(String[] tokens)
{
int[] plot = new int[2];
try
{
plot[0] = Integer.parseInt(tokens[1]);
plot[1] = Integer.parseInt(tokens[2]);
}
catch (Exception e)
{
return null;
}
return plot;
}
public static int[] parseRegion(String[] tokens)
{
int[] region = new int[4];
try
{
region[0] = Integer.parseInt(tokens[1]);
region[1] = Integer.parseInt(tokens[2]);
region[2] = Integer.parseInt(tokens[3]);
region[3] = Integer.parseInt(tokens[4]);
}
catch (Exception e)
{
return null;
}
return region;
}
}
//Farm.java
public class Farm
{
private Crop[][] crops;
public Farm(int width, int height)
{
crops = new Crop[width][height];
for (int i = 0; i < crops.length; i++)
{
for (int j = 0; j < crops[i].length; j++)
{
crops[i][j] = new Crop(0, 0, false, false);
}
}
}
public Crop getCrops(int i, int j)
{
return crops[i][j];
}
public void setCrops(Crop[][] crops)
{
this.crops = crops;
}
public void GrowCrop(int i, int j, String command)
{
if (i <= crops.length && j <= crops[i].length)
{
if (command.equalsIgnoreCase("Water")
&& crops[i][j].isInfested() == false)
{
int waterlevel = crops[i][j].getWater();
int height = crops[i][j].getHeight();
if (waterlevel == 0)
crops[i][j].setWater(8);
waterlevel = crops[i][j].getWater();
if (waterlevel >= 0 && waterlevel <= 8)
{
crops[i][j].setWater(waterlevel - 1);
}
crops[i][j].setHeight(height + 1);
}
else if (command.equalsIgnoreCase("Spray"))
{
boolean flag = crops[i][j].isSprayed();
if (flag == false)
{
crops[i][j].setSprayed(true);
crops[i][j].setInfested(false);
}
}
else if (command.equalsIgnoreCase("Infest"))
{
crops[i][j].setInfested(true);
Decay();
}
else if (command.equalsIgnoreCase("Harvest"))
{
crops[i][j].setHeight(0);
}
}
else
{
System.out.println("Invalid arguments");
}
}
public void Decay()
{
for (int i = 0; i < crops.length; i++)
{
for (int j = 0; j < crops[i].length; j++)
{
if (crops[i][j].isInfested() == true)
{
int height = crops[i][j].getHeight();
if (height > 0)
crops[i][j].setHeight(height - 1);
}
}
}
}
public void Infest()
{
for (int i = 0; i < crops.length; i++)
{
for (int j = 0; j < crops[i].length; j++)
{
if (crops[i][j].isInfested() == true)
{
int height = crops[i][j].getHeight();
if (i == 0 && j == 0)
{
if (!crops[i + 1][j].isSprayed()
&& !crops[i][j + 1].isSprayed())
{
crops[i + 1][j].setInfested(true);
computeHeight(i + 1, j);
crops[i][j + 1].setInfested(true);
computeHeight(i, j + 1);
}
else if (!crops[i + 1][j].isSprayed()
&& crops[i][j + 1].isSprayed())
{
crops[i + 1][j].setInfested(true);
computeHeight(i + 1, j);
}
else if (crops[i + 1][j].isSprayed()
&& !crops[i][j + 1].isSprayed())
{
crops[i][j + 1].setInfested(true);
computeHeight(i, j + 1);
}
}
else if (i == crops[i].length - 1 && j == 0)
{
if (!crops[i + 1][j].isSprayed()
&& !crops[i][j - 1].isSprayed())
{
crops[i + 1][j].setInfested(true);
computeHeight(i + 1, j);
crops[i][j - 1].setInfested(true);
computeHeight(i, j - 1);
}
else if (!crops[i + 1][j].isSprayed()
&& crops[i][j - 1].isSprayed())
{
crops[i + 1][j].setInfested(true);
computeHeight(i + 1, j);
}
else if (crops[i + 1][j].isSprayed()
&& !crops[i][j - 1].isSprayed())
{
crops[i][j - 1].setInfested(true);
computeHeight(i, j - 1);
}
}
else if (j == 0 && i == crops.length - 1)
{
if (!crops[i - 1][j].isSprayed()
&& !crops[i][j + 1].isSprayed())
{
crops[i - 1][j].setInfested(true);
computeHeight(i - 1, j);
crops[i][j + 1].setInfested(true);
computeHeight(i, j + 1);
}
else if (!crops[i - 1][j].isSprayed()
&& !crops[i][j + 1].isSprayed())
{
crops[i - 1][j].setInfested(true);
computeHeight(i - 1, j);
}
else if (!crops[i - 1][j].isSprayed()
&& !crops[i][j + 1].isSprayed())
{
crops[i][j + 1].setInfested(true);
computeHeight(i, j + 1);
}
}
else if (j == crops[i].length - 1 && i == crops.length - 1)
{
if (!crops[i - 1][j].isSprayed()
&& !crops[i][j - 1].isSprayed())
{
crops[i - 1][j].setInfested(true);
computeHeight(i - 1, j);
crops[i][j - 1].setInfested(true);
computeHeight(i, j - 1);
}
else if (!crops[i - 1][j].isSprayed()
&& !crops[i][j - 1].isSprayed())
{
crops[i - 1][j].setInfested(true);
computeHeight(i - 1, j);
}
else if (!crops[i - 1][j].isSprayed()
&& !crops[i][j - 1].isSprayed())
{
crops[i][j - 1].setInfested(true);
computeHeight(i, j - 1);
}
}
else if (i > 0 && i < crops.length - 1 && j > 0
&& j < crops[i].length - 1)
{
if (!crops[i - 1][j].isSprayed()
&& !crops[i][j + 1].isSprayed()
&& !crops[i][j - 1].isSprayed()
&& !crops[i + 1][j].isSprayed())
{
crops[i - 1][j].setInfested(true);
computeHeight(i - 1, j);
crops[i][j + 1].setInfested(true);
computeHeight(i, j + 1);
crops[i + 1][j].setInfested(true);
computeHeight(i + 1, j);
crops[i][j - 1].setInfested(true);
computeHeight(i, j - 1);
}
else if (crops[i - 1][j].isSprayed()
&& !crops[i][j + 1].isSprayed()
&& !crops[i][j - 1].isSprayed()
&& !crops[i + 1][j].isSprayed())
{
crops[i - 1][j].setInfested(true);
computeHeight(i - 1, j);
}
else if (!crops[i - 1][j].isSprayed()
&& crops[i][j + 1].isSprayed()
&& !crops[i][j - 1].isSprayed()
&& !crops[i + 1][j].isSprayed())
{
crops[i][j + 1].setInfested(true);
computeHeight(i, j + 1);
}
else if (!crops[i - 1][j].isSprayed()
&& !crops[i][j + 1].isSprayed()
&& crops[i][j - 1].isSprayed()
&& !crops[i + 1][j].isSprayed())
{
crops[i][j - 1].setInfested(true);
computeHeight(i, j - 1);
}
else if (!crops[i - 1][j].isSprayed()
&& !crops[i][j + 1].isSprayed()
&& !crops[i][j - 1].isSprayed()
&& crops[i + 1][j].isSprayed())
{
crops[i + 1][j].setInfested(true);
computeHeight(i + 1, j);
}
}
crops[i][j].setHeight(height - 1);
}
}
}
}
public void computeHeight(int i, int j)
{
int height = crops[i][j].getHeight();
crops[i][j].setHeight(height - 1);
}
public void showStatus(String command)
{
if (command.equalsIgnoreCase("water"))
{
for (int i = 0; i < crops.length; i++)
{
for (int j = 0; j < crops[i].length; j++)
{
if (crops[i][j].getWater() <= 8)
{
System.out.print(crops[i][j].getWater() + " ");
}
else
{
System.out.println("0 ");
}
}
System.out.println();
}
}
if (command.equalsIgnoreCase("height"))
{
for (int i = 0; i < crops.length; i++)
{
for (int j = 0; j < crops[i].length; j++)
{
if (crops[i][j].getHeight() <= 8)
{
System.out.print(crops[i][j].getHeight() + " ");
}
}
System.out.println();
}
}
if (command.equalsIgnoreCase("sprayed"))
{
for (int i = 0; i < crops.length; i++)
{
for (int j = 0; j < crops[i].length; j++)
{
if (crops[i][j].isSprayed())
{
System.out.print("+ ");
}
else
{
System.out.print(". ");
}
}
System.out.println();
}
}
if (command.equalsIgnoreCase("infested"))
{
for (int i = 0; i < crops.length; i++)
{
for (int j = 0; j < crops[i].length; j++)
{
if (crops[i][j].isInfested())
{
System.out.print("x ");
}
else
{
System.out.print(". ");
}
}
System.out.println();
}
}
}
}
//Crop.java
public class Crop
{
private int width;
private int height;
private int water;
private boolean sprayed;
private boolean infested;
public Crop()
{
}
public Crop(int water, int height, boolean spray, boolean infest)
{
this.height = height;
this.water = water;
sprayed = spray;
infested = infest;
}
public int getWidth()
{
return width;
}
public void setWidth(int width)
{
this.width = width;
}
public int getHeight()
{
return height;
}
public void setHeight(int height)
{
if (this.height == 8)
return;
else
this.height = height;
}
public int getWater()
{
return water;
}
public void setWater(int water)
{
this.water = water;
}
public boolean isSprayed()
{
return sprayed;
}
public void setSprayed(boolean sprayed)
{
this.sprayed = sprayed;
}
public boolean isInfested()
{
return infested;
}
public void setInfested(boolean infested)
{
this.infested = infested;
}
}
Sample Output:
> status
status
Day 1 - 100 coins
> show water
show water
0 0 0 0
0 0 0 0
> show height
show height
0 0 0 0
0 0 0 0
> water 1 1 4 2
water 1 1 4 2
Cost 80 coins
> show height
show height
1 1 1 1
1 1 1 1
> next
next
Day 2 - 20 coins
> next
next
Day 3 - 20 coins
> show height
show height
3 3 3 3
3 3 3 3
> harvest 1 1
harvest 1 1
Collect 10 coins
harvested
> status
status
Day 3 - 30 coins
> show height
show height
0 3 3 3
3 3 3 3
> spray 2 2
spray 2 2
Cost 20 coins
> show sprayed
show sprayed
. . . .
. + . .
> harvest 1 2 3 2
harvest 1 2 3 2
Collect 60 coins
harvested
> show height
show height
0 0 0 3
0 0 0 3
> bye
bye
bye
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.