i was just hoping to get help on an assignment i've been racking my brain over for the last couple of hours. i'm asked to program a database of houses using object oriented programming. i'm suppose to do this by writing a house class, an address class, and a third helper class called houselist that constructs an array of house objects. the houselist class also holds methods like search by zip or price. i'm just testing things right now to check if everything works. i got the house and address classes written alright it's just i can't use any of the methods i wrote in the houselist class. i keep getting a cannot find symbol compilation error. i'd really appreciate any help, thanks
here's my code:
import java.util.*;
class House
{
//attributes
Address add;
int area;
int bedRooms;
int bathRooms;
int garage;
double price;
//default constructor
House()
{
Address add = new Address();
area = 0;
bedRooms = 0;
bathRooms = 0;
garage = 0;
price = 0.0;
}
//non-default constructor
House(Address add, int area, int bedRooms, int bathRooms, int garage, double price)
{
this.add = add;
this.area = area;
this.bedRooms = bedRooms;
this.bathRooms = bathRooms;
this.garage = garage;
this.price = price;
}
//returns house attributes as one string
public String toString()
{
return "Address is: " + add.toString() + "\n" + "Area in square feet: " + area + "\n" + "Number of bedrooms: " + bedRooms + "\n" + "Number of bathrooms: " + bathRooms + "\n" + "Garage space: " + garage + "\n" + "Price is: " + price;
}
//computes price per square feet
public double priceSqft()
{
return price / area;
}
}
class Address
{
//attributes
String street;
String town;
String state;
String zip;
//default constructor
Address()
{
street = "";
town = "";
state = "";
zip = "";
}
//non-default constructor
Address(String street, String town, String state, String zip)
{
this.street = street;
this.town = town;
this.state = state;
this.zip = zip;
}
//returns address attributes as one string
public String toString()
{
return street + "\n" + "\t " + town + ", " + state + "\n" + "\t " + zip;
}
}
class HouseList
{
//attributes
House [] list;
int numHouses = 0;
// default constructor
HouseList()
{
House [] list = new House[100];
}
// creates an array of houses of size n
HouseList(int n)
{
House [] list = new House[n];
}
// get the number of houses on the list
public int getNumHouses()
{
return numHouses;
}
// adds a house to the list
public void addHouse(House home)
{
//if list is full more space is allocated
System.out.println("It's going through");
if (numHouses == list.length)
{
House[] temp = new House[list.length + 100];
for (int i = 0; i < list.length; i++)
{
temp[i] = list[i];
}
list = temp;
list[numHouses] = home;
numHouses++;
}
else
{
list[numHouses] = home;
numHouses++;
}
}
// prints the houses in that zip code
public void searchByZip(String zip)
{
for (int i = 0; i < list.length; i++)
{
House home = list[i];
Address add = home.add;
String code = add.zip;
if (zip.equals(code))
{
System.out.println(home.toString());
}
}
}
// prints the houses in the price range
public void searchByPrice(double low, double high)
{
for (int i = 0; i < list.length; i++)
{
House home = list[i];
double cost = home.price;
if ((cost >= low) && (cost <= high))
{
System.out.println(home.toString());
}
}
}
//prints the houses in that square feet range
public void searchByArea(int low, int high)
{
for (int i = 0; i < list.length; i++)
{
House home = list[i];
int space = home.area;
if ((space >= low) && (space <= high))
{
System.out.println(home.toString());
}
}
}
// prints the houses having that many bedrooms
public void searchByRooms(int rooms)
{
for (int i = 0; i < list.length; i++)
{
House home = list[i];
int numRooms = home.bedRooms;
if (numRooms == rooms)
{
System.out.println(home.toString());
}
}
}
}
public class Realtor
{
public static void main(String[] args)
{
HouseList[] list = new HouseList[100];
Address addr1 = new Address("4501 Speedway", "Austin", "TX", "78751");
House house1 = new House(addr1, 2500, 3, 2, 2, 3000000.99);
list.addHouse(house1); //these two line are where i get the cannot find symbol error
list.searchByZip("78751");
}
}