Results 1 to 5 of 5
Thread: Beginner Q: help needed
- 09-30-2010, 09:59 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 23
- Rep Power
- 0
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.
- 10-02-2010, 02:22 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
Could you try to explain this again. thanks
- 10-02-2010, 02:49 AM #3
Can an array of type Building hold both Schools and Houses?
You'd have to use instanceof and casting to use this technique.
- 10-04-2010, 07:09 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 23
- Rep Power
- 0
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.
- 10-05-2010, 07:35 AM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 12
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
-
Need Help - Beginner
By ooooohmaul in forum New To JavaReplies: 4Last Post: 08-08-2010, 05:22 AM -
Help Needed.. Java Beginner
By levis lover in forum New To JavaReplies: 5Last Post: 06-17-2010, 07:29 AM -
Beginner, need a little help
By jimmy-lin in forum New To JavaReplies: 6Last Post: 10-10-2009, 02:00 AM -
beginner here...help please
By shroomiin in forum New To JavaReplies: 6Last Post: 09-16-2009, 12:06 AM -
almost done...beginner needs help plz..
By shongo in forum New To JavaReplies: 15Last Post: 11-10-2008, 09:14 AM
Bookmarks