Member Avatar for die_raupe2

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");
	}
}
Member Avatar for ztini

First of all, you have some fundamental errors that need corrected:

import java.util.*;

You should import specific classes, not entire packages...and certainly not packages you do not use.

class House

You need to declare a modifier for each class, typically public. Each class should therefore lie on their own .java which in turn is compiled into .class.

//attributes
	String street;
	String town;
	String state;
	String zip;

	//default constructor
	Address()
	{
		street = "";
		town = "";
		state = "";
		zip = "";
	}

If you declare a default value to the global variables, there is really no need to declare a default constructor. Moreover, if you are using a default constructor to propagate your globals with junk data, this is bad practice. Simply put, you should require your users to give you good data by forcing them to use a certain constructor and/or getter/setters

To put this into context, your Address class might look like this:

public class Address {

	private String street, town, state, zip;
 
	Address(String street, String town, String state, String zip) {
		setStreet(street);
		setTown(town);
		setState(state);
		setZip(zip);
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public String getStreet() {
		return street;
	}

	public void setTown(String town) {
		this.town = town;
	}

	public String getTown() {
		return town;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getState() {
		return state;
	}

	public void setZip(String zip) {
		this.zip = zip;
	}

	public String getZip() {
		return zip;
	}
	
	@Override
	public String toString() {
		return street + '\n' + town + ", " + state + '\n' + zip;
	}

}

Notice with this class, the only option is the constructor that takes parameters. You can clearly see how in this case, if the user puts in bad data, they will get bad data -- whereas before they could give no data and get bad data.

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());
			}
		}
	}

Now that you see how the Address class should be structured, you should be able to produce a House class in the same manner. Also, all of your "search" methods do the same thing...they check the array and print, why not break some of that out into other methods? Using an Address and Home class, you could therefore use a Realtor class like this:

public class Realtor {
	
	private House[] houses = new House[100];
	int index = 0;

	public int getNumHouses() {
		return index;
	}
	
	private void grow() {
		House[] temp = new House[houses.length * 2];
		int tempIndex = index;
		for (index = 0; index < tempIndex; index++)
			temp[index] = houses[index];
		houses = temp;
	}
	
	public void addHouse(House house) {
		if (index == houses.length)
			grow();
		houses[index++] = house;
	}

	public String searchByZip(String zip) {
		House[] results = new House[index];
		int resultIndex = 0;
		
		for (int i = 0; i < index; i++) {
			if (houses[i].getAddress().getZip().equals(zip))
				results[resultIndex++] = houses[i];				
		}
		
		return toString(results);
	}
	
	public String searchByPrice(int low, int high) {
		House[] results = new House[index];
		int resultIndex = 0;
		
		for (int i = 0; i < index; i++) {
			if (houses[i].getPrice() >= low &&
					houses[i].getPrice() <= high)
				results[resultIndex++] = houses[i];
		}
		
		return toString(results);
	}

	public String searchByArea(int low, int high) {
		House[] results = new House[index];
		int resultIndex = 0;
		
		for (int i = 0; i < index; i++) {
			if (houses[i].getArea() >= low &&
					houses[i].getArea() <= high)
				results[resultIndex++] = houses[i];
		}
		
		return toString(results);
	}
	
	public String searchByRooms(int rooms) {
		House[] results = new House[index];
		int resultIndex = 0;
		
		for (int i = 0; i < index; i++) {
			if (houses[i].getBed() == rooms)
				results[resultIndex++] = houses[i];
		}
		
		return toString(results);
	}
	
	@Override
	public String toString() {
		return toString(houses);
	}
	
	private String toString(House[] list) {
		String line = "";
		
		for (int i = 0; i < list.length; i++) {
			if (list[i] != null)
				line += list[i].toString();
			else
				break;
		}
		
		return line;
	}
	
	public static void main(String[] args) {
		
		Realtor realtor = new Realtor();
		
		Address address = new Address("4501 Speedway", "Austin", "TX", "78751");
		House house = new House(address, 2500, 3, 2, 2, 300000);
		
		realtor.addHouse(house);
		
		System.out.println(realtor.searchByPrice(250000, 350000));
		System.out.println();
		System.out.println(realtor.searchByZip("78751"));
		
	}
}
Member Avatar for ztini
list.addHouse(house1); //these two line are where i get the cannot find symbol error
		list.searchByZip("78751");

To specifically address your problem -- you have 2 methods addHouse() and searchByZip() that you are you trying to invoke on the array object "list". Array objects do not have these methods, hence your error. You should create a new Realtor object, add a house object to it using addHouse(), and then invoke whatever search method you want on it. See the main method in the previous post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.