Questions tagged [car-analogy]
15 questions
67
votes
35 answers
What is a practical, real world example of the Linked List?
I understand the definition of a Linked List, but how can it be represented and related to a common concept or item?
For example, composition (EDIT: originally said 'inheritance') in OOP can be related to automobiles. All (most) automobiles in…
JStims
24
votes
9 answers
OOP design problem
What is good design in this simple case:
Let's say I have a base class Car with a method FillTank(Fuel fuel) where
fuel is also a base class which have several leaf classes, diesel, ethanol etc.
On my leaf car class DieselCar.FillTank(Fuel fuel)…

Marcus
- 1,866
- 1
- 20
- 33
19
votes
10 answers
How to display all elements in an arraylist?
Say I have a car class with attributes make and registration, and i create an ArrayList to store them. How do I display all the elements in the ArrayList?
I have this code right now:
public Car getAll()
{
for(int i = 0; i < cars.size(); i++)…
Newtojava
18
votes
10 answers
How can I avoid dynamic_cast in my C++ code?
Let's say I have the following class structure:
class Car;
class FooCar : public Car;
class BarCar : public Car;
class Engine;
class FooEngine : public Engine;
class BarEngine : public Engine;
Let's also give a Car a handle to its Engine. A…

Michael Kristofik
- 34,290
- 15
- 75
- 125
12
votes
12 answers
Why stick to get-set and not car.speed() and car.speed(55) respectively?
Apart from unambiguous clarity, why should we stick to:
car.getSpeed() and car.setSpeed(55)
when this could be used as well :
car.speed() and car.speed(55)
I know that get() and set() are useful to keep any changes to the data member manageable by…

namespaceform
- 690
- 1
- 6
- 14
12
votes
2 answers
Ruby: difference between @cars.each do |car| and for car in @cars do
(Sorry for the newbie question.) In Ruby, what is the difference between the loops:
@cars.each do |car|
and
for car in @cars do
?
is there a difference in efficiency, or why do we need two (or more) ways to express the same thing? The second way…

jacob
- 1,214
- 2
- 13
- 22
7
votes
4 answers
OOP design : Car - Connection between Tank and Engine
I'm not sure whether I'm violating OOP conepts insanely.
Say there is a Carclass which "has" an Engine and a Tank.
When the Engine is operating , it will take oil from the Tank (say unit by unit per a cycle though oil is uncountable)
How should the…

Dinushan
- 2,067
- 6
- 30
- 47
7
votes
10 answers
How do I use composition with inheritance?
I'm going to try to ask my question in the context of a simple example...
Let's say I have an abstract base class Car. Car has-a basic Engine object. I have a method StartEngine() in the abstract Car class that delegates the starting of the engine…

Rob Sobers
- 20,737
- 24
- 82
- 111
5
votes
4 answers
Car steering algorithm?
i've already asked something similar, but now i've the problem to manage and realize a "realistic" steering for a simple 2d (top-down) car racing game.
How can i do a "realistic" steering for the car ? (i use c# but another language is…

stighy
- 7,260
- 25
- 97
- 157
4
votes
15 answers
The object oriented relationship
I was asked to describe the relationship between vehicle, car, toyota in object oriented programming term (let's say in php environment). I was stumped. Can someone help me about it? Thanks...

FlyingCat
- 14,036
- 36
- 119
- 198
4
votes
7 answers
Whether to model a car object (and its parts such as engine) with has-a (composition) or is-a (inheritance)?
I am developing a class library which will include the object Car.
The dilemma is, Car itself will be a class with fields such as Registration Number, and other general information on the car.
But a car has an engine, chassis, etc. These objects…

GurdeepS
- 65,107
- 109
- 251
- 387
1
vote
8 answers
How can I sort a 10 x 10 grid of 100 car images in two dimensions, by price and speed?
Here's the scenario.
I have one hundred car objects. Each car has a property for speed, and a property for price. I want to arrange images of the cars in a grid so that the fastest and most expensive car is at the top right, and the slowest and…

CentralScrutinizer
- 19
- 2
1
vote
8 answers
OOP Best Practices When One Object Needs to Modify Another
(this is a C-like environment) Say I have two instance objects, a car and a bodyShop. The car has a color iVar and corresponding accesors. The bodyShop has a method named "paintCar" that will take in a car object and change its color.
As far as…
user185950
1
vote
3 answers
Simulating a Car (Java)
I'm working on a project to simulate a car. The requirements are to demonstrate the operation of a car by filling it with fuel and then run the car until it has no more
fuel. Simulate the process of filling and running the car at different speeds.…

Mike
- 31
- 2
- 5
0
votes
2 answers
Physics, turn like a car
I'm having difficulty getting the Chipmunk physics engine to do what I want. The only solution that appears to work requires some heavy vector math. Before diving into that rabbit hole for the other components of my game, I was hoping someone could…

Clev3r
- 1,568
- 1
- 15
- 28