Results 1 to 5 of 5
  1. #1
    smallmos1 is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default Beginner Q: help needed

    Newish to programming - 2nd year college, only touching on polymorphism etc.

    Currently doing a work sheet and have a problem.

    We've been asked to create a parent class (Building) and two subclasses (House, School). These have been created, variables, constructors etc as specified.

    We're now asked to:

    "Create a test class that creates an array of five buildings. Prompt the user to enter the details of each building, by first asking them what type of building they want to create."

    I'm able to create an object array for one object ok, and then fill it with user input, with no problem. What I can't figure out is how we do this for two objects when the user can input what ever they want...the user could choose 1 or 5 Houses, or 1 or 5 Schools. Should I use a polymorphic array?

    Not sure if this is in the right section, this is my first post.

    Any and all help appreciated.

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Could you try to explain this again. thanks

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,463
    Rep Power
    25

    Default

    Can an array of type Building hold both Schools and Houses?
    You'd have to use instanceof and casting to use this technique.

  4. #4
    smallmos1 is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    i have super class Building. sub classes House and School.

    i created an array in the test class:

    Buiding buildings[] = new Building[5];

    //ask user what they want to create

    String answer;

    if answer.equalsIgnoreCase("house")
    {
    etc etc etc
    }

    i fugured out that when i was instantiating the object house i was making it type building instead of type house, and that's why i was getting the error. anyway, thanks for the answers.

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    If you properly subclassed House and School, you should be able to instantiate them as a Building object:
    Java Code:
    public class Parent {
    }
    
    public class Child extends Parent {
    }
    
    public class InhTest {
      public static void main(String[] args) {
        Parent p = new Child(); //valid, will not produce errors
      }
    }
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Need Help - Beginner
    By ooooohmaul in forum New To Java
    Replies: 4
    Last Post: 08-08-2010, 04:22 AM
  2. Help Needed.. Java Beginner
    By levis lover in forum New To Java
    Replies: 5
    Last Post: 06-17-2010, 06:29 AM
  3. Beginner, need a little help
    By jimmy-lin in forum New To Java
    Replies: 6
    Last Post: 10-10-2009, 01:00 AM
  4. beginner here...help please
    By shroomiin in forum New To Java
    Replies: 6
    Last Post: 09-15-2009, 11:06 PM
  5. almost done...beginner needs help plz..
    By shongo in forum New To Java
    Replies: 15
    Last Post: 11-10-2008, 08:14 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
  •