# Need Help with this problem

**URL:** <https://www.ruby-forum.com/t/need-help-with-this-problem/177183>\
**Category:** Ruby\
**Created:** [October 12, 2009, 11:38pm UTC](https://www.ruby-forum.com/t/need-help-with-this-problem/177183 "2009-10-12T23:38:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghostalpha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@ghostalpha](https://www.ruby-forum.com/u/ghostalpha)\
**Post date:** [October 12, 2009, 11:38pm UTC](https://www.ruby-forum.com/t/need-help-with-this-problem/177183/1 "2009-10-12T23:38:33Z")

</div>

I am in this programming class and need help on this problem:

- Defines a Ruby class, Vehicle, with attributes weight, length width  
and height. The Vehicle class should have a class variable counts the  
number of vehicle objects created.
- Define three subclasses, landvehicle, watervehicle, and airvehicle so  
they inherit the attributes of the vehicle class and so that each has  
some attributes of its own, numberofwheels for landvehicles, keeldepth  
for watervehicles, and maxairspeed or airvehicles
- creates 1 landvhicle objects, 2 watervehicle objects and 3 airvehicle  
objects, use [constructors] in each subclass to assign initial values  
of numberofwheels, keeldepth and maxairspeed to the appropriate objects.  
You should invent the actual values to use when these objects are  
created
- Display the number of vehicle objects created then ends.

attached is what i did, i was wondering if its correct

---

<div class="post-metadata">

**Author:** ![ghostalpha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@ghostalpha](https://www.ruby-forum.com/u/ghostalpha)\
**Post date:** [October 12, 2009, 11:43pm UTC](https://www.ruby-forum.com/t/need-help-with-this-problem/177183/2 "2009-10-12T23:43:02Z")

</div>

Ghost Alpha wrote:

> I am in this programming class and need help on this problem:
> 
> - Defines a Ruby class, Vehicle, with attributes weight, length width  
> and height. The Vehicle class should have a class variable counts the  
> number of vehicle objects created.
> - Define three subclasses, landvehicle, watervehicle, and airvehicle so  
> they inherit the attributes of the vehicle class and so that each has  
> some attributes of its own, numberofwheels for landvehicles, keeldepth  
> for watervehicles, and maxairspeed or airvehicles
> - creates 1 landvhicle objects, 2 watervehicle objects and 3 airvehicle  
> objects, use [constructors] in each subclass to assign initial values  
> of numberofwheels, keeldepth and maxairspeed to the appropriate objects.  
> You should invent the actual values to use when these objects are  
> created
> - Display the number of vehicle objects created then ends.
> 
> attached is what i did, i was wondering if its correct

here is the link to the

[http://demo.ovh.org/en/8c779e3d297b9839114ac4c737c22065/](http://demo.ovh.org/en/8c779e3d297b9839114ac4c737c22065/)

---

<div class="post-metadata">

**Author:** ![ghostalpha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@ghostalpha](https://www.ruby-forum.com/u/ghostalpha)\
**Post date:** [October 13, 2009, 12:22am UTC](https://www.ruby-forum.com/t/need-help-with-this-problem/177183/3 "2009-10-13T00:22:02Z")

</div>

> keeldepth
> 
> > for watervehicles, and maxairspeed or airvehicles
> > 
> > - creates 1 landvhicle objects, 2 watervehicle objects and 3  
> > airvehicle  
> > objects, use [constructors] in each subclass to assign initial  
> > values  
> > of numberofwheels, keeldepth and maxairspeed to the appropriate  
> > objects.

You aren’t using constructors to define numberofwheels, keeldepth, and  
maxairspeed. In your code they are simply constant functions.

---

<div class="post-metadata">

**Author:** ![ghostalpha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@ghostalpha](https://www.ruby-forum.com/u/ghostalpha)\
**Post date:** [October 13, 2009, 12:48am UTC](https://www.ruby-forum.com/t/need-help-with-this-problem/177183/4 "2009-10-13T00:48:19Z")

</div>

Walton H. wrote:

> > keeldepth
> > 
> > > for watervehicles, and maxairspeed or airvehicles
> > > 
> > > - creates 1 landvhicle objects, 2 watervehicle objects and 3  
> > > airvehicle  
> > > objects, use [constructors] in each subclass to assign initial  
> > > values  
> > > of numberofwheels, keeldepth and maxairspeed to the appropriate  
> > > objects.
> 
> You aren’t using constructors to define numberofwheels, keeldepth, and  
> maxairspeed. In your code they are simply constant functions.

can you show me the correct way?

---

<div class="post-metadata">

**Author:** ![ghostalpha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@ghostalpha](https://www.ruby-forum.com/u/ghostalpha)\
**Post date:** [October 13, 2009, 7:16am UTC](https://www.ruby-forum.com/t/need-help-with-this-problem/177183/5 "2009-10-13T07:16:00Z")

</div>

Ghost Alpha wrote:

> Walton H. wrote:
> 
> > > keeldepth
> > > 
> > > > for watervehicles, and maxairspeed or airvehicles
> > > > 
> > > > - creates 1 landvhicle objects, 2 watervehicle objects and 3  
> > > > airvehicle  
> > > > objects, use [constructors] in each subclass to assign initial  
> > > > values  
> > > > of numberofwheels, keeldepth and maxairspeed to the appropriate  
> > > > objects.
> > 
> > You aren’t using constructors to define numberofwheels, keeldepth, and  
> > maxairspeed. In your code they are simply constant functions.
> 
> can you show me the correct way?

Hi Ghost Alpha,

It looks like you forgot to add initialize methods to your sub-classes

you wrote:

class Land \< Vehicle  
def numberofwheels  
“#@numberofwheels Numberofwheels is four”  
end  
end

That doesn’t do what you need it to do. What if I wanted to create a 10  
wheel truck? I would be out of luck, because I can only have four wheels  
with a land vehicle. None of my deliveries would get to where they need  
to get to because of the limited cargo space of four wheeled vehicles.  
It would be just awful. Instead …

class Land \< Vehicle  
def initialize(a\_hash) # lets initialize  
super # this loads in the other initialize stuff  
@num\_wheels = a\_hash[:num\_wheels]  
end  
end

dump\_truck = Land.new(:weight =\> “30000”,  
:length =\> “28”,  
:height =\> “13.2”,  
:width =\> “9”,  
:num\_wheels =\> “10”)

Now that’s a truck that’ll hall a load of dirt away from my build site!
