2013-03-12 43 views
1

我現在在做任務,我有兩個班級的房間和房子。作爲轉讓的一部分,我需要啓用房子類存儲在ArrayList房間:如何實現ArrayList以將「房間」對象添加到「房屋」對象列表中?

實現方法在從鍵盤上House教室信息閱讀 - 這應該叫Room構造的要求。創建一個零參數的House構造函數來調用這個新方法。

我道歉,如果這是含糊不清或已在別處回答,但已經試圖瞭解其他類似查詢的解釋,我不知道如何將它們應用到我的情況。

我如何申請一個RoomArrayList到家裏上課嗎?

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'; 
} 
} 
+2

問到ArrayList的有關具體問題。 – 2013-03-12 17:51:15

回答

0

由於這是家庭作業我不會解決它的整體,但給你一個工作的基礎...

public class House { 
    private List<Room> rooms; 

    public House() { 
    rooms = new ArrayList<Room>(); 
    } 

    public void addRoom(...) { 
    Room room = new Room(...); 
    rooms.add(room); 
    } 

    public int numRooms() { 
    return rooms.size(); 
    } 

    public Room getRoom(int roomNumber) { 
    return rooms.get(roomNumber); 
    } 
} 

public class Room { 
    public Room(...) { 
    ... 
    } 
} 

有問題?

+0

重新審視我的問題,這是完美的!謝謝! – user2160931 2013-03-14 19:41:35

1

你檢查的API爲ArrayList的?

你需要的東西,如:

rooms.add(new Room()); 

此外,我會利用Java泛型的,因此定義列表:

private ArrayList<Room> rooms; 

這使得利用generics,並確保這隻能是房間清單,而不是雜項物品。這將節省你一些悲痛中,你不能在不經意間房子添加到您的房間(通過編程錯誤)

0

使用專業化的列表中Room類型的列表

List<Room> rooms = new ArrayList<Room>(); 

while (i < numRooms) //This will loop until all rooms have been described 
{ 
    rooms.add(new Room()); //this is correct 
    i++; 
} 
+0

感謝大家!這些答案中的任何一個都可以爲我解決這個問題!我只是希望我在5個小時前尋求幫助。謝謝你。 – user2160931 2013-03-12 18:05:58

0
ArrayList<Room> rooms = new ArrayList<Room>(); 
rooms.add(new Room()); 
0

更改您的構造函數方法,添加一些關於您的房間的代碼ArrayList

public House(..., ArrayList aRooms) { 

... 
rooms = aRooms; 
} 

public House() { 

... 
// It's better to instantiate the rooms array here. 
rooms = new ArrayList<Room>(); 
} 

要的房間添加到您的房間ArrayList,只是做這樣的事情(不要忘記檢查ArrayList API,http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html)。

ArrayList<Room> rooms = new ArrayList<Room>(); 
rooms.add(new Room(...)); 
// Add here all the rooms you need. 

House house = new House(rooms); 

// You can also create a method into the House class to allow adding new rooms once you have created a House object. 
public void addRoom(Room room) { 

this.rooms.add(room); 
} 
0

更改這個循環:

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++; 
} 

要:

while (i < numRooms) 
{ 
Room newRoom = new Room(); 
rooms.add(newRoom); 
i++; 
}