You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java编程初学者求助:LandVille类构造函数代码补全及理解

Hey there! As a fellow Java learner, I totally get how confusing bits of code can feel at first. Let's walk through fixing and understanding this LandVille class together.

LandVille Class: Full Implementation & Explanation

First, here's the complete, working version of your code with missing parts filled in:

public class LandVille {
    private int[][] land;
    private boolean hasHouse;

    public static void main(String args[]) {
        LandVille landVille = new LandVille(3, 4);
        landVille.displayLand();
    }

    // Task A - Constructor
    LandVille(int numRows, int numColumns) {
        land = new int[numRows][numColumns];
        // Initialize all land cells to 0 (representing empty land)
        for (int i = 0; i < numRows; ++i) {
            for (int j = 0; j < numColumns; ++j) {
                land[i][j] = 0;
            }
        }
        // Initially, no house has been built, so set to false
        hasHouse = false;
    }

    // Added: Method to display the current state of the land
    public void displayLand() {
        System.out.println("Current Land State (0 = Empty):");
        for (int[] row : land) { // Enhanced for-loop (cleaner than nested indexes)
            for (int cell : row) {
                System.out.print(cell + " ");
            }
            System.out.println(); // New line after each row
        }
        // Show house status
        System.out.println("Has a house been built? " + (hasHouse ? "Yes" : "No"));
    }
}

Breakdown of Key Parts

1. Constructor Fix: hasHouse Initialization

In your original code, the hasHouse variable was left unassigned. Here's why we set it to false:

  • hasHouse is a private boolean field that tracks whether a house exists on the land.
  • When you first create a LandVille object, no house has been built yet, so the logical starting value is false.
  • While Java does give boolean fields a default value of false, explicitly initializing it makes your code easier to read and understand for anyone (including future you!).

2. Land Array Initialization

The nested loops in the constructor:

for (int i = 0; i < numRows; ++i) {
    for (int j = 0; j < numColumns; ++j) {
        land[i][j] = 0;
    }
}
  • This creates a 2D array with numRows rows and numColumns columns, where every cell is set to 0.
  • 0 here represents "empty land" — you could later expand this to use other numbers (like 1 for a house, 2 for a farm) to add more features.

3. Added displayLand() Method

Your main method calls landVille.displayLand();, but this method wasn't defined. I added it to:

  • Print out the grid of land cells so you can see the state visually.
  • Use an enhanced for-loop (for (int[] row : land)) which is cleaner than using index variables for simple iteration.
  • Show the status of hasHouse so you can confirm if a house exists.

4. Class Fields Explained

  • private int[][] land: A private 2D array that stores the state of each land cell. Making it private follows the encapsulation principle (keeping internal data hidden from outside code).
  • private boolean hasHouse: A private boolean flag that tracks house presence. Again, this is encapsulation in action — you can later add methods like buildHouse() to change this value safely.

What Happens When You Run It?

When you execute the code, you'll see this output:

Current Land State (0 = Empty):
0 0 0 0
0 0 0 0
0 0 0 0
Has a house been built? No

This confirms your land grid is initialized correctly, and no house exists yet.

内容的提问来源于stack exchange,提问作者Josi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:21:14