//Description: There are 3 classes farmbot, farm, and crop. The farm bot that pe
ID: 662530 • Letter: #
Question
//Description: There are 3 classes farmbot, farm, and crop. The farm bot that performs tasks on a field of crops that grow //each day based on a set of predefined rules. Crops can be harvested to obtain coins. Coins can be used to water //crops to help them grow and spray crops with pesticide so they do not become infested. Water -- the water level //between 0 to 8. Water -- the water level between 0 to 8. Infested -- whether or not the crop is infested. Sprayed -- //whether or not the crop plot has been treated with pesticide. Each crop will grow every day if Grow -- if the crop is not //infested and the water level of the crop is one or above then its height will increase by one, and the water level will //decrease by one. If the water level of the crop is zero then neither the water level nor the height of the crop will //change. Decay -- if the crop is infested then its height will decrease by one, but the water level will stay the same. If the //height of an infested crop becomes zero then it is no longer infested. Infest -- if the crop is adjacent to any infested //crops then it will also becomes infested unless the crop plot has been sprayed with pesticide or the crop height is 0. //Two cells are considered to be adjacent if they share an edge or corner.When applying these rules you should create //a copy of the grid that will contain the next state. The next state of the grid should be calculated based on state the //crops were in the previous one. Coins----- You start the epoch with 100 coins. Certain actions including watering crops //or spraying crops with pesticide require coins to be performed. You can obtain more coins by harvesting and selling //crops.
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 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
import java.text.SimpleDateFormat;
import java.util.*;
public class Farmbot {
private static Farm farm;
private static Scanner scan;
private static int coins=100;
private static Date dNow;
private static SimpleDateFormat ft;
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
// ...
width=Integer.parseInt(args[0]);
height=Integer.parseInt(args[1]);
// 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
dNow = new Date( );
ft =
new SimpleDateFormat ("dd/M/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Now use today date.
c.add(Calendar.DATE, 1); // Adding 1 days
String nextday = ft.format(c.getTime());
System.out.println(nextday);
}
// Displays the current day and amount of coins held
public static void statusCommand() {
dNow = new Date( );
ft =
new SimpleDateFormat ("dd/M/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Now use today date.
System.out.println("Current day: "+ft.format(c.getTime()));
System.out.println("Amount of coins held: "+coins);
}
// 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];
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
switch (attribute) {
case "water":
System.out.println(farm.getCrops(i,j).getWater());
break;
case "height":
System.out.println(farm.getCrops(i,j).getHeight());
break;
case "sprayed":
System.out.println(farm.getCrops(i,j).isSprayed());
break;
case "infested":
System.out.println(farm.getCrops(i,j).isInfested());
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]
int x1=Integer.parseInt(tokens[1]);
int x2=Integer.parseInt(tokens[2]);
System.out.println(plot[x1]);
System.out.println(plot[x2]);
} 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("Sprayed");
}
// Infests a crop or region of crop plots
public static void infestCommand(String[] tokens) {
// INFEST <x1> <y1> [<x2> <y2>]
System.out.println("infested");
}
// Harvests a crop plot or region of crop plots
public static void harvestCommand(String[] tokens) {
// HARVEST <x1> <y1> [<x2> <y2>]
coins+=1;
System.out.println("harvested");
}
// 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 Crop getCrops(int i,int j) {
return crops[i][j];
}
public void setCrops(Crop[][] crops) {
this.crops = crops;
}
}
--------------------------------------------------------------------------------------------------------------------------------------------
public class Crop {
private int width;
private int height;
private int water;
private boolean sprayed;
private boolean infested;
public Crop() {
}
public Crop(int width,int height){
this.width=width;
this.height=height;
water=0;
sprayed=false;
infested=false;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
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;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.