Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Car dealership design queston  RSS feed

 
Daniel Lucas
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK now I'm doing question #6 for the AP Comp Sci A sample multiple choice questions provided by collegeboard's AP Central:

A car dealership needs a program to store information about the cars for sale. For each car, they want to keep track of the following information: number of doors (2 or 4), whether the car has air conditioning, and its average number of miles per gallon. Which of the following is the best design?

(A) Use one class, Car, which has three data fields:
int numDoors, boolean hasAir, and double milesPerGallon.

(B) Use four unrelated classes: Car, Doors, AirConditioning, and MilesPerGallon.

(C) Use a class car which has three subclasses: Doors, AirConditioning, and MilesPerGallon.

(D) Use a class Car, which has a subclass Doors, with a subclass AirConditioning, with a subclass MilesPerGallon.

(E) Use three classes: Doors, AirConditioning, and MilesPerGallon, each with a subclass Car.

My analysis:

OK so they are looking for the BEST design out of the five. I'm guessing best means the one with the least lines that works properly.

Looking at them, D looks like it wouldn't work properly because AirConditioning is a subclass or Doors, and MilesPerGallon a subclass of AirConditioning, and that doesn't make logical sense.

For E, using three classes, each with a subclass Car, doesn't make sense.

For B, using four unrelated classes would be inefficient.

So I think it's between A and C. Since I don't think it's necessary to have subclasses to accomplish the objective, I think A is the best answer. Am I right? *checks answer* Yep! It's A.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not only is it not necessary to subclass it, but does not make sense. Does it pass the is-a test?

Car is a Door?
Car is an AirConditioning?
Car is a MilesPerGallon?

That is the sort of thing you need to ask. Incidently, the first 2 pass the has-a test and is a good canditate for composition, not inheritance. The Strategy or Decorator pattern would be good here, but that might be a bit too much info right now.
 
Daniel Lucas
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you explain the is-a and has-a tests to me a bit more? My Java textbook does not mention them.

From what you said, I see that the is-a question is used to determine if a subclass belongs in a class.

What is the has-a question used to determine?
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dog is a Animal, so this could be a safe inheritance heirarchy. Dog extends Animal.

Dog has a tail. This could be done several ways. A simple boolean hasTail in Dog might suffice, but a Tail object in Dog could be more useful. This way the tail could wag or not, or whatever. This is essentially composition.

Dogs also Bark, but some really yap, howl, ect, so you could have a Barkable interface or abstract class. So a Yap class that implements Barkable. So in Dog, you can have a Barkable reference, to make things more flexible.

But Dog is a Pet also, and you can't have multiple inheritance, but you can inherit any behavior or characteristics of pet by implementing a Pet interface.

So we have a Dog which is a Animal and a Pet and has a Tail and bark determined at runtime. It is not a perfect example, but hopefully will illistrate things well enough.

There is a lot to this subject and goes deeper with design patterns. More then can be fit in a message board post. Hopefully this will make the basic a bit more clear.
[ April 30, 2006: Message edited by: Rusty Shackleford ]
 
Daniel Lucas
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read your post and re-read it again for understanding, then read it again for the heck of it. Thanks, it does help a lot. I do realize, however, that I have a lot more to learn. I trust these more advanced characteristics will be explained in college. I'm just preparing for the AP Comp Sci A exam right now, futile or not.
 
Tom Fulton
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would disagree that "is a" is a valid test. "Is a" can be a shorthand way of referring to inheritance, but inheritance can only be tested (robustly, anyway) with the test of substitution, which in summary goes like this: "If (class A) is a valid subclass of (class B), then any place you expect an instance of B, you should be able to replace it with an instance of A and it should work properly." Those who know the real rule (called the Liskov Substitution Principle) know that technically a reference, not an object, has to be replaceable, but in Java we can only access objects via reference anyway, so it works in this simplified version.

For example, if Orange is a subclass of Fruit, any time someone asks for a Fruit you should be able to give them an Orange and they will be happy. If a police officer needs a motor vehicle for an emergency, I should be able to give him/her my (car, RV, motorcycle) and it should meet their need.

The problem with "is a" as a test is that the business environment in which we often program is too "squishy"...what might sound OK in a sentence would not pass the substitution test. "A Homeowner Policy is a Contract" might sound OK, but might not meet every requirement for Contract as defined in the business.

English is just too damn flexible to be worthwhile for testing object relationships rigorously.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Give a cop an RV during an emergency, in rush hour and see what he says.

A homeowner policy is a contract, and so is a partnership agreement. That a partnership agreement and a insurance policy are very different doesn't invalidate the fact that both can have contract as a super class. That is the entire point of inheritance and polymorphism. It can be written in such a way that the invoking program doesn't need to care which type of contract is being used, the desired attributes will be there.
 
Tom Fulton
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A homeowner policy is a contract, and so is a partnership agreement.


You are using terms from an environment without defining them, and without explaining the environment. I will say that in many, if not most, environments the statement is accurate. But it doesn't mean that it works from an object-oriented perspective unless the classes have been defined accordingly.

Let's take the word "transaction". It means different things to different people. In a database, we mean a unit of work which must succeeed or fail as a whole. In a business, we may instead say that a transaction is an event on a particular day that results in an exchange of money between a customer and the business. Is an update to the inventory a transaction? "Yes", you reply, because you are thinking about it from the database perspective. "No", the SME answers, because no money is exhanged.

I repeat: "is a" is a perfectly acceptable shorthand terms that represents generalization. It is not a test. The only acceptable test for inheritance is that of replaceability. If it fails the test of replaceability, it must properly be modeled in some other way than inheritance.
 
Daniel Lucas
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well I took the AP Computer Science A exam today. I did alright on the multiple choice section, but the free response owned me. I tried to get as many points as possible by at least filling in what I knew would be in there. I hope they give points out as freely as the AP Calculus exam. If they do then I have a chance for a 3.

What's really going to be hard is the AP Music Theory exam I take tomorrow...
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to me that it would be best if you don't pass. That might sound harsh, but look at how many times you have misunderstood concepts. Do you really want to jump ahead of an intro class, not having a firm grasp on the basics? Most classes past intro classes, expect you do know the prereqs, and will not slow down for you, which can be frusterating and waste lots of time and money, not too mention kill your GPA. Please don't take it in the wrong way, it is just an opinion of a computer science honors student, that started college with near zero knowlege of programming and hardware design. although admittedly Java is not my strongest area.

"English is just too damn flexible to be worthwhile for testing object relationships rigorously."

No it is not, it works just fine for rigorous definitions in mathematics, you just have to be careful. Just because transaction can have multiple meanings doesn't mean is-a isn't rigorous enough. That is why things like packages exist. You can have 2 transaction base classes in 2 packages that do two totally different things, yet both work well. Like mathematics, it is all relative to the context, given due care to the wording. Ever seen a proof that there exists infinite sets of different sizes(cardinality)? By the everday use of the word that seems to be senseless, but yet it is true. In much the same way, with care towards wording and attention to context you can subclass in a logical and flexible manner using is-a.
 
Tom Fulton
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see that we are never going to agree on this. It has been my experience that the development of good software is directly related to the level of rigor used to identify and define the business objects (and their relationships) we model. Your experience, apparently, is not the same as mine.

I do agree with you on one thing: understanding the fundamental principles of OO is critical before going on to more advanced concepts. In my experience, the vast majority of developers don't really understand these principles...they understand the mechanism, but not the rationale.
 
Daniel Lucas
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rusty Shackleford:
It seems to me that it would be best if you don't pass. That might sound harsh, but look at how many times you have misunderstood concepts. Do you really want to jump ahead of an intro class, not having a firm grasp on the basics? Most classes past intro classes, expect you do know the prereqs, and will not slow down for you, which can be frusterating and waste lots of time and money, not too mention kill your GPA. Please don't take it in the wrong way, it is just an opinion of a computer science honors student, that started college with near zero knowlege of programming and hardware design. although admittedly Java is not my strongest area.
I'm not skipping any intro courses even if I pass. I know a girl who took 13 AP exams in high school and passed 12 of them with 4s and 5s. She actually only took 5 AP classes in high school, all the other work she did on her own. She went into college as a junior and did well there too. She then went on to law school and is doing well there. She's not me; I'm not motivated to work my butt off like that.

It would be nice if I pass, however, because it would please not only my parents but also my teacher. None of my teacher's students have ever passed, and sad but true, I'm the best student in our 18 person class in terms of knowledge and grades. Only one other person besides me even studied (the little bit that we did) and tried to pass the test.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!