public class Corn {
private int age;
private int moistureLevel;
public Corn(int age, int moistureLevel) {
this.age = age;
this.moistureLevel = moistureLevel;
}
// Getters and setters for age and moistureLevel
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getMoistureLevel() {
return moistureLevel;
}
public void setMoistureLevel(int moistureLevel) {
this.moistureLevel = moistureLevel;
}
}
public class FarmingSimulator {
private static final int FIELD_ROWS = 5;
private static final int FIELD_COLS = 5;
private Corn[][] field;
public FarmingSimulator() {
field = new Corn[FIELD_ROWS][FIELD_COLS];
initializeField();
}
private void initializeField() {
// Fill the field with some Corn objects and leave some empty
// This is just a basic example, you can customize it as needed
for (int i = 0; i < FIELD_ROWS; i++) {
for (int j = 0; j < FIELD_COLS; j++) {
if (Math.random() < 0.5) { // 50% chance of having a corn in a cell
int age = (int) (Math.random() * 10); // Random age between 0 and 9
int moistureLevel = (int) (Math.random() * 100); // Random moisture level between 0 and 99
field[i][j] = new Corn(age, moistureLevel);
}
}
}
}
public Corn getDataAtPosition(int row, int col) {
if (row < 0 || row >= FIELD_ROWS || col < 0 || col >= FIELD_COLS) {
return null; // Coordinates out of bounds
}
return field[row][col];
}
public void displayField() {
for (int i = 0; i < FIELD_ROWS; i++) {
for (int j = 0; j < FIELD_COLS; j++) {
if (field[i][j] != null) {
System.out.print("C "); // Representing a cell with corn
} else {
System.out.print(". "); // Representing an empty cell
}
}
System.out.println(); // Move to the next row
}
}
public static void main(String[] args) {
FarmingSimulator simulator = new FarmingSimulator();
simulator.displayField();
// Example usage of getDataAtPosition method
int row = 2;
int col = 3;
Corn dataAtPosition = simulator.getDataAtPosition(row, col);
if (dataAtPosition != null) {
System.out.println("Position: (" + row + ", " + col + ")");
System.out.println("Age: " + dataAtPosition.getAge());
System.out.println("Moisture Level: " + dataAtPosition.getMoistureLevel());
} else {
System.out.println("No data at position (" + row + ", " + col + ")");
}
}
}
FarmingSimulator farm = new FarmingSimulator();
farm.main(null);
. C C C .
. . . C C
C . C C .
. . . . C
C C . . C
Position: (2, 3)
Age: 3
Moisture Level: 24