Basic OOP concepts and array of objects help.
Hi all, I am very new to the Java world and I'm taking an intro class to java and I have an assignment that I will post the details in below. OOP is kind of hard for me to figure out. I feel like I have a good grasp of the concepts of OOP but when it comes to the actual syntax and calling/creating objects I get very lost.
The program criteria is below:
----------------------------------------------------------------------
You are helping a friend, who is a real estate agent, maintain a list of houses that are for sale in the Austin area. You have to define a class called House that has the following attributes and methods. The plus (+) sign indicates public and the (-) sign indicates private.
House
+address: Address
+area: int // area in square feet
+bedRooms: int // number of bedrooms
+bathRooms: int // number of bathrooms
+garage: int // number of cars in garage
+price: double // list (asking) price in dollars
+House() // default constructor
+House ( address: Address, area: int, bedRooms: int, bathRooms: int,
garage: int, price: double )
+toString(): String // string representation of a house object
+priceSqft(): double // price per square feet
You need a helper class called Address to represent the address of the houses. This class has the following structure:
Address
+street: String // house number and street name
+town: String // name of town or city
+state: String // two letter abbreviation of state
+zip: String // five digit zip code
+Address() // default constructor
+Address ( street: String, town: String, state: String, zip: String )
+toString(): String // string representation of address
You will need a class that maintains a list of houses. It also has methods to search for houses according to some criteria: by zip code, by price range, by range in square feet, and by the number of bedrooms. The structure for this class HouseList is as follows:
HouseList
-houseList: House[] // array of houses
-numHouses: int // number of houses on the list
+HouseList() // default constructor
+HouseList( n: int ) // creates an array of houses of size n
+getNumHouses (): int // get the number of houses on the list
+addHouse ( house: House ): void // adds a house to the list if
// it is not full
+searchByZip ( zip: String): void // prints the houses in that zip code
+searchByPrice ( lowPrice: double, highPrice: double ): void
// prints the houses in the price range
+searchByArea ( lowArea: int, highArea: int ): void
// prints the houses in that square feet
// range
+searchByRooms ( rooms: int ): void // prints the houses having that many
// bedrooms
Your main program will be called Realtor. You will be testing out the methods that you wrote for the classes Address, House, and HouseList. The structure for that class will be:
public class Realtor
{
public static void main ( String[] args )
{
// Create a HouseList object that can hold a hundred houses
...
// Populate the array with 10 houses. Makeup the data for these houses
...
// Write out the number of houses in your list
...
// Write out the houses in a certain zip code
...
// Write out the houses in a certain price range
...
// Write out the houses in a certain square foot range
...
// Write out the houses that have a certain number of bedrooms
...
}
}
--------------------------------------------------------------------
Here is what I have: Code:
import java.util.Scanner;
class House
{
public Address houseAddress;
public int area;
public int bedRooms;
public int bathRooms;
public int garage;
public double price;
public House() //DEFAULT CONSTRUCTOR
{
this.Address = null; //error message says cannot find address.
this.area = 0;
this.bedRooms = 0;
this.bathRooms = 0;
this.garage = 0;
this.price = 0;
}
public House(Address a, int sqft, int br, int bath, int gar, double price ) // make sure address is in before sqft
{
this.Address = Address; //Error msg says cannot find variable address
this.area = sqft;
this.bedRooms = br;
this.bathRooms = bath;
this.garage = gar;
this.price = price;
}
public String toString()
{
return ""; //just a placeholder for no error
}
public double priceSqft()
{
return 1.0; // just a placeholder (it should be return sqft / price;)
}
}
class Address
{
public String street;
public String town;
public String state;
public String zip;
public Address() //DEFAULT Constructor
{
this.street = "";
this.town = "";
this.state = "";
this.zip = "";
}
public Address(String street, String town, String state, String zip)
{
this.street = street;
this.town = town;
this.state = state;
this.zip = zip;
}
}
class HouseList
{
private HouseList [] list;
private int numHouses = 0;
public HouseList()
{
}
public HouseList(int n)
{
}
public int getNumHouses()
{
return 4; //just a placeholder
}
public void addHouse()
{
numHouses++;
}
public void searchByZip(String zip)
{
}
public void searchByPrice(double lowPrice, double highPrice)
{
}
public void searchByArea(double lowArea, double highArea)
{
}
public void searchByRooms(int rooms)
{
}
}
public class RealtorTest
{
public static void main ( String[] args )
{
Scanner sc = new Scanner(System.in);
// Create a HouseList object that can hold a hundred houses
HouseList [] list = new HouseList[100];
// Populate the array with 10 houses. Makeup the data for these houses
//THIS IS THE SYNTAX THAT I NEED TO CREATE A HOUSE
[COLOR="Blue"]HouseList.addHouse(new House(new Address("1715 Giles St", "Austin", "TX", "78722"), 1660, 3, 3, 2, 200000.00));[/COLOR]
/*
(String street, String town, String state, String zip)
( int sqft, int br, int bath, int gar, double price )
[COLOR="Red"]House h1 = new House( 10000 , 2 , 2 , 2 , 60000.00 );
Address h1 = new Address( "MLK Street" , "Austin", "TX", "78705" );
House h2 = new House( 20000, 3, 4, 2, 95000.00 );
Address h2 = new Address ( "Cesar Chaves Road" , "Austin", "TX", "78710");
House h3 = new House( 15000 , 1 , 1, 0, 50000.00 );
Address h3 = new Address( "Guadalupe Street", "Austin", "TX", "78705");
House h4 = new House ( 40000 , 4 , 4, 2, 120000.00 );
Address h4 = new Address ( "Royal Lane", "Austin", "TX" , "78700");
House h5 = new House ( 18000, 3, 3, 1, 70000.00 );
Address h5 = new Address ( "Dean Keaton Street" , "Austin" , "TX", "78710" );
House h6 = new House ( 25000, 4 , 3 , 2, 75000.00);
Address h6 = new Address( "Braker Lane" , "Austin" , "TX", "78715");
House h7 = new House ( 50000, 3, 3, 3, 175000.00 );
Address h7 = new Address ("Westlake Lane", "Austin" , "TX", "78715" );
House h8 = new House ( 10000 , 1 , 1 , 0, 35000.00 );
Address h8 = new Address( "Leon Street", "Austin", "TX", "78705" );
House h9 = new House ( 60000, 4, 5, 3, 200000.00 );
Address h9 = new Address ( "Haven Road" , "Austin", "TX", "78700");
House h10 = new House ( 20000, 3 , 3, 2, 77000.00);
Address h10 = new Address ( "Congress Avenue" , "Austin", "TX", "78710" );[/COLOR]
*/
// Write out the number of houses in your list
//System.our.println("The number of houses for sale is: " +numHouses);
// Write out the houses in a certain zip code
//System.out.print("Please enter a zip code you are looking for: ");
// int zip = sc.nextInt();
// Write out the houses in a certain price range
[COLOR="Red"]//System.out.print("Please enter the minimum price you are looking for: ");
//double lowPrice = sc.nextDouble();[/COLOR]
[COLOR="Red"]//System.out.print("\nPlease enter the maximum price you are looking for: ");
//double highPrice = sc.nextDouble();[/COLOR]
// Write out the houses in a certain square foot range
//System.out.print("Please enter the minimum square footage you are looking for: ");
[COLOR="Red"]//double lowArea = sc.nextDouble();
//System.out.print("\nPlease enter the maximum square footage you are looking for: ");
//double highArea = sc.nextDouble();[/COLOR]
// Write out the houses that have a certain number of bedrooms
[COLOR="Red"]//System.our.print("How many bedrooms are you looking for: ");
//int rooms = sc.nextInt();[/COLOR]
}
}
The code in red is the stuff that will be added in later for when I get the whole house list situation figured out.
Can anyone just point me in the right direction for my code? If it was just one class then I could, but there are 3 classes I'm needing to work with (address, house, and houselist) and I'm unsure of how to link everything together.. Thanks for your help