Results 1 to 3 of 3
- 05-23-2012, 01:14 PM #1
Member
- Join Date
- May 2012
- Posts
- 3
- Rep Power
- 0
Creating a "Parking Garage" with cars as objects
Hi everyone,
I'm trying to create a "garage" filled with specific cars, ranging from simple Toyotas to fancy Aston Martins.
When I create the cars as objects, though, I'm not sure which of the following to use:
should I make an object Car, and use Car Toyota = new Car()? Or, should I make a separate object for each type of car, such as Toyota redcar = new Toyota()? Each car is going to have variables such as speed, number of passengers, etc.
What are the benefits or drawbacks of using each type of object? I know it's a vague question, but I'm looking moreso for a theoretical answer than an actual block of code.
Thanks in advance!
- 05-23-2012, 02:24 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Re: Creating a "Parking Garage" with cars as objects
Suppose you want to distinguish three different top speeds and you als have cars that can have two, four or six passengers. If each were a different type then you'd have to deal with nine different car types (the Euclidean product). It's much easier to make the top speed and the number of passengers a property of a single Car class.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-23-2012, 02:47 PM #3
Member
- Join Date
- May 2012
- Posts
- 18
- Rep Power
- 0
Re: Creating a "Parking Garage" with cars as objects
I would personally go with the first constructor: Car Toyota = new Car(). Thus, Car would be the top class...with variables common to all cars such as color, speed, number of passengers, etc. You could then do inheritance from the Car class... so perhaps class Toyota extends Car if there was something specific to the Toyota class...perhaps all Toyotas come with a special type of transmission, etc.
Similar Threads
-
scanning lines from notepad, by creating a loop "while(input_line=NULL) {"
By noobplus in forum New To JavaReplies: 3Last Post: 03-17-2012, 03:19 AM -
a parking garage program
By jihad in forum New To JavaReplies: 3Last Post: 12-24-2011, 02:06 PM -
Creating Java objects using "new"
By linking90 in forum New To JavaReplies: 1Last Post: 07-03-2010, 11:38 PM -
"source not found" at debug time when creating new class
By rafamd11 in forum New To JavaReplies: 0Last Post: 11-22-2008, 01:49 AM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks