Question: I'm getting the following error in my Java code from public Arraylist onwards and need help with a fix IndexOutOfBoundsException was thrown: Index: 0, Size:

I'm getting the following error in my Java code from public Arraylist onwards and need help with a fix

IndexOutOfBoundsException was thrown: Index: 0, Size: 0

import java.util.ArrayList;

public class Hotel { private ArrayList rooms; private String name; public Hotel(String aName) { this.name = aName; rooms = new ArrayList(); } public void addRooms() { int i = 10; while (i < 19) { String number = "" + i; double rate; if(i % 3 == 1) { number += "A"; rate = 100; } else if (i % 3 == 2) { number += "B"; rate = 180; } else { number += "C"; rate = 250; } Room r = new Room("", number, rate); rooms.add(r); i++; } } public double calculateBill(Room vacated_room, int occupied_days) { double bill =0.0; bill = occupied_days* vacated_room.getDailyRate(); if(occupied_days>=4) { double discount = 0.1*bill; bill-=discount; } return bill; }

public ArrayList getMatchingRooms(String room_type) { ArrayList matching_rooms = new ArrayList(); for(int i=0; i

} ROOM Class:

public class Room { // instance variables - replace the example below with your own private String guest; private String number; private double dailyRate; /** * Write a description of class Room here. */ public Room(String aGuest, String aNumber, double aRate) { guest = aGuest; number = aNumber; dailyRate = aRate; } /** * Write a description of class Room here. */ public String getGuest() { return guest; } /** * Write a description of class Room here. */ public String getNumber() { return number; } /** * Write a description of class Room here. */ public double getDailyRate() { return dailyRate; } /** * Write a description of class Room here. */ public void setGuest(String guest) { this.guest = guest; } /** * Write a description of class Room here. */ public void setDailyRate(double dailyRate) { this.dailyRate = dailyRate; } /** * Write a description of class Room here. */ public boolean isAvailable(){ return this.guest.length() == 0; } public boolean verifyRoom() { if(this.getNumber().length()==3) if(this.getNumber().charAt(0)>='0' && this.getNumber().charAt(1)<='9' && !(this.getNumber().charAt(0)=='0' && this.getNumber().charAt(1)=='0')) if(this.getNumber().charAt(2)=='A' || this.getNumber().charAt(2)=='B' || this.number.charAt(2)=='C') return true; return false; } public String getType(){ String roomType = "";

if(number.charAt(2) == 'A'){ roomType = "Single"; } else if(number.charAt(2) == 'B'){ roomType = "Double"; } else{ roomType = "Family"; }

return roomType; } public String description(){ String availability = isAvailable() ? "available" : "reserved"; return getType() + " room " + number + " (" + availability + ") " + guest;

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!