Results 1 to 6 of 6
  1. #1
    dschm42 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default 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:
    Java 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default

    I don't see a list of houses anywhere in your code; I see an array but you're not using it (yet). Also a house 'has a' address, indicating that an Address should be a member of a House. You are just creating a bunch of separate houses and separate addresses, i.e. you're supposed to link those two together and stick the Houses in a list, a List<House> that is in Java speak.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    dschm42 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    I don't see a list of houses anywhere in your code
    The list of houses are in the red, they are not formatted correctly, I just had them in there to show that I have 10 houses already made up for data entry. I'm trying to add each house using this line of code:

    Java Code:
     HouseList.addHouse(new House(new Address("Street", "City", "State", "zip"), square footage, num bedrooms, num bathrooms, cars in garage, price));
    I see an array but you're not using it (yet)
    This is where I am having trouble. I have a class called houselist but I do not know what to do with it or construct it or call from it. This is why I'm coming here for help and guidance.

    Also a house 'has a' address, indicating that an Address should be a member of a House. You are just creating a bunch of separate houses and separate addresses, i.e. you're supposed to link those two together and stick the Houses in a list, a List<House> that is in Java speak.
    Are you saying that I need the address class within the House class? or do they still have to be different classes. The address class is supposed to be a "helper" class, and I'm unsure about that terminology. I'm assuming its just to not make the house class so bulky so the address class makes it look a little neater.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by dschm42 View Post
    Java Code:
     HouseList.addHouse(new House(new Address("Street", "City", "State", "zip"), 
    square footage, num bedrooms, num bathrooms, cars in garage, price));
    That creates a single house with a single address attached to it; that's ok.

    Quote Originally Posted by dschm42 View Post
    This is where I am having trouble. I have a class called houselist but I do not know what to do with it or construct it or call from it. This is why I'm coming here for help and guidance.
    Don't use arrays. An array can potentially hold n Houses where n is a fixed number. Use a List<House> instead and add( ... ) as many houses to it as you want.

    Quote Originally Posted by dschm42 View Post
    Are you saying that I need the address class within the House class? or do they still have to be different classes. The address class is supposed to be a "helper" class, and I'm unsure about that terminology. I'm assuming its just to not make the house class so bulky so the address class makes it look a little neater.
    Nope, keep the Address class a separate class for now; it is simpler. Read the API documentation for the List<T> interface; a List<House> is so much simpler to use than an array of houses.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    dschm42 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post

    Don't use arrays. An array can potentially hold n Houses where n is a fixed number. Use a List<House> instead and add( ... ) as many houses to it as you want.
    I wish that I could use that but the instructions state That I need to create an array of Houses. I do not think that I can use the List<T> because we have not gone over it in class even though it seems so much more simpler that making an array of Houses.

    I'm sorry if I seem to not be getting the concepts but I am stuck in the code and I have no clue on what to do next. I feel that if I can get all of the houses into an array, then the rest is easy, but I am completely lost. Could someone show me what they would do from the code that I originally posted to get each respective house into the array of HouseList?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by dschm42 View Post
    I wish that I could use that but the instructions state That I need to create an array of Houses. I do not think that I can use the List<T> because we have not gone over it in class even though it seems so much more simpler that making an array of Houses.

    I'm sorry if I seem to not be getting the concepts but I am stuck in the code and I have no clue on what to do next. I feel that if I can get all of the houses into an array, then the rest is easy, but I am completely lost. Could someone show me what they would do from the code that I originally posted to get each respective house into the array of HouseList?
    If you really have to use an array, are you going to stick as many houses in it as will fit (you can't stick more in it) or are you going to leave one or more array slots unused? Are you going to stick them in one by one or all at once? The latter is much easier:

    Java Code:
    House[] houses= {
       new House( ... ),
       new House( ... ),
       ...
    };
    This code snippet defines and initializes the array in one single step.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Array of objects
    By Saletra in forum New To Java
    Replies: 2
    Last Post: 07-31-2010, 01:16 PM
  2. Array of Objects
    By sfe23 in forum New To Java
    Replies: 19
    Last Post: 02-04-2009, 06:57 PM
  3. Basic array question
    By jigglywiggly in forum New To Java
    Replies: 12
    Last Post: 01-09-2009, 05:44 PM
  4. Array of objects
    By rosh72851 in forum New To Java
    Replies: 5
    Last Post: 10-31-2008, 05:03 AM
  5. Array with objects
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 10:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •