gpt4 book ai didi

java - 如何实现 ArrayList 将 "Room"对象添加到 "House"对象列表中?

转载 作者:太空宇宙 更新时间:2023-11-04 07:45:24 25 4
gpt4 key购买 nike

我目前正在做作业,我有两个类(class)“房间”和“房子”。作为作业的一部分,我需要启用房屋类将房间存储在 ArrayList 中。 :

Implement a method to read in room information in the House class from the keyboard — this should call the Room constructor as required. Create a zero-argument House constructor to call this new method.

如果这含糊不清或已在其他地方得到回答,我深表歉意,但在尝试理解其他类似查询的解释后,我不知道如何将它们应用到我的情况。

如何申请ArrayListRoom去家庭类吗?

<强> House :

import java.util.ArrayList;
import java.util.Scanner;

public class House {

private int idNum;
private static int internalCount = 0;
private ArrayList rooms = new ArrayList();
private String address;
private int numRooms;
private String houseType;

public House (String address, int numRooms, String houseType) {
idNum = internalCount++;

this.address = address;
this.numRooms = numRooms;
this.houseType = houseType;
}


public House () {
int i = 0;
idNum = ++internalCount;

Scanner scan = new Scanner(System.in);
scan.useDelimiter("\n");

System.out.println("Enter address of house:");
address = scan.next();

System.out.println("Enter number of rooms:"); //Number of rooms in the House
numRooms = scan.nextInt();

while (i < numRooms) //This will loop until all rooms have been described
{
add.room = new Room[100]; //I understand this is incorrect but I don't know what I should have in here
i++;
}

System.out.println("Enter type of house:");
houseType = scan.next();
}
public void addroom(String description, double length, double width)
{

}

int getIdNum() {
return idNum;
}


@Override

public String toString()
{
String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
report += "Address: " + address + "\n";
report += "No. of Rooms: " + numRooms + "\n";
report += "House Type: " + houseType+ "\n";
report += "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";

return report;
}
}

<强> Room :

import java.util.Scanner;

public class Room {

private String description;
private double length;
private double width;

public Room (String description, double length, double width) {
this.description = description;
this.length = length;
this.width = width;
}

public Room () {
Scanner scan = new Scanner(System.in);
scan.useDelimiter("\n");

System.out.println("Enter description of room:");
description = scan.next();

System.out.println("Enter length of room:");
length = scan.nextDouble();

System.out.println("Enter width of room:");
width = scan.nextDouble();
}

/*
* Calculates and returns area of Room
*/
public double getArea () {
return length*width;
}


@Override
public String toString() {
return description + "- Length: " + length + "m; Width: " + width + 'm';
}
}

最佳答案

您检查过API吗?对于数组列表?

你需要这样的东西:

rooms.add(new Room());

此外,我将利用 Java 泛型并这样定义列表:

private ArrayList<Room> rooms;

这利用了 generics ,并确保这只能是房间列表,而不是杂项对象。这将为您省去一些麻烦,因为您不会无意中将房屋添加到房间列表中(通过编程错误)

关于java - 如何实现 ArrayList 将 "Room"对象添加到 "House"对象列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15368415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com