car-analogy

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 to the Engine object. How do I allow subclasses of Car (like Ferrari) to declare the En...

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 FooCar will be created with a FooEngine* and a BarCar will be created with a BarEngine*. Is ...

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++) //cars name of arraylist { Car car = cars.get(i); { retu...

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 seems more elegant/natural to me, but I may be missing some crucial observation, why t...

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 implementation, in order to get the bodyShop to actually be able to change a car object's c...

Modelling a car object (and its parts such as engine)

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 need to be modelled too. Should they be classes embedded within Car? If not, what is the us...

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 cheapest car is at the bottom left, and all other cars are in an appropriate spot in the grid...

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 keeping everything in one place. Also, obviously, I understand that car.speed() and car.s...

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) only a certain type of fuel is allowed (no surprises there:)). Now here is my concern, acco...

The object oriented relationship...

Hi guys. 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... ...

Car steering algorithm ?

hi, 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 welcome;)) Using Sin and Cos ? If yes, how ? Thanks in advance! ...

create a Java method called that will return the size of an ArrayList

hi, i'm new to java programming and need to create a method called sizeofCarPark that returns the amount of cars that are stored in a Car Park, i'd appreciate some pointers in what i need to do. thanks. pieter ...