tags:

views:

73

answers:

1

How can i model foollwing problem statatement into java classe?

I have a class vehincle.

  • Vehicles can be of type Trucks, Cars, Scooters, motorcycles.
  • Vehicle has a engine.
  • engine shold have following contraints
    • Petrol Engine and Diesel Engine are types of Engines
    • Truck comes with 4 stroke, 12 cylinder Diesel Engine
    • Car can have either 4 stroke Petrol Engine or 4 stroke Diesel Engine
    • Car can have either 4 Cylinder Engine or 6 Cylinder Engine.
    • Motorcycle can have 4 stroke single Cylinder Petrol Engine or 4 stroke twin-cylinder Petrol engine.
    • Scooters can have Single Cylinder 2 Stroke Petrol Engine or Single Cylinder 4 stroke Petrol Engine.
  • Each engine consists of
    • Number of cylinders [1/2/4/6/12 only]
    • Number of strokes [2/4 only].
    • ‘Engine Number’ is always 6 characters alphanumeric.
  • Diesel engine always comes with 4 strokes but Petrol engine can come with either 2 strokes or 4 strokes.
  • Only Petrol engines have Spark plugs [One per cylinder]. Diesel Engine cannot have Spark plugs.
  • Sparkplug just has sequence number [e.g Car with 6 Cylinder Engine will have Sparkplugs SP1, SP2, SP3, SP4, SP5, SP6]
  • Only Trucks and Cars have Oil Pump.
  • Each Oil pump has
    • Identification number 6 char alphanumeric
    • Capacity in cubic centimeters(cc) per seconds [value can be between 1-5]
+4  A: 

This sounds very much like a beginner OOP task. In this case you're going to model each class of things in the real world as classes in your class model. That is, there will be a class Vehicle, Engine, SparkPlug, etc.

Then those classes have certain relationships, such as DieselEngine being a kind of Engine. You should have learned that inheritance can be used to model such a relationship.

Furthermore, some things are composed of other things, such as PetrolEngine having a SparkPlug. Classes can have attributes as you probably know already. Use them accordingly.

In UML the two things you need here for a class diagram would be generalization and composition.

Also, I'm sure if you ask your fellow students you can together come up with a solution. That's usually easier than dumping the task description into a forum or Q&A site and waiting for ready-made solutions.

Joey
+1 for not giving too much away. (Not sure it's worth mentioning UML, though - I don't think that would be used at a beginner level)
David Zaslavsky
@David: At least basic class diagrams were part of our schedule almost at the same time as generic OO modeling.
Joey