• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inheritance and Subclasses

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there,
I am trying to solve this exercise I am doing.
Basically I need to create a , class ( I already did) that has 3 instance variables ()
In this class as well I did the following


Then I created a subclass of Vehicle called SportCar (I extended the class)
it has an additional variable in it )
All sportcars have aerodynamics = 0.5



I need to create 3 sportcars which have the following:
SportCar1 : horsepower = 200, weight = 1500, topspeed = 220
SportCar2 : horsepower = 100, weight = 1000, topspeed = 170
SportCar3 : horsepower = 135, weight = 1100.2, topspeed = 173

So I created them in another class called TestConsumption


my problems are:
How do I create 3 instances of sportcar/initialize the horsepowers, weights and topspeeds for all the sportcars?

Is my method of setting aerodynamic =0.5 for all sportcars right?

Are those codes correct. I did get a few cannot find symbol along the way.


Any help is greatly appreciated.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your Super class- Vehicle has the constructor which takes- three parameters. But in the sub class constructor- SportsCar you are calling super(<parameter>), but as you would see- the Vehicle would be expecting: super(param1,param2,param3).

Java provides a default- no-arg constructor if the constructor is not overloaded in the class. But if the constructor is overloaded- the programmer would have to specify the no-arg constructor- something like:


Now coming to initializing the fields for the class- You can pass it in the constructor- while creating the instance or use the setters to set those values.

If you want to set a particular value for a particular variable for all the instances of the class- You would have to declare the variable/field as Static- Something like

 
ashlan cooper
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, will try my best to change accordingly
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should be writingYou are allowed to write super(...) or this(...) in the constructor, but only as the very first statement. So you can't get both in, and you can't use them twice.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, if all sports cars have an aerodynamics value of 0.5, that should be
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote: . . . if all sports cars have an aerodynamics value of 0.5 . . .

I had missed that bit. Sorry.
 
ashlan cooper
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,
thanks. I modified the code






So what do you think?

and what I am trying to do now is that
I want to implement this formula for fuel consumption under the TestConsumption class
(1000+(weight/5))*(topspeed/100)*(aerodynamics*horsepower)/1000

for those 3 new sportcars I created.

How do I pass the values of each sportcar; sportcar1,2,3
into this formula

thanks
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ashlan cooper wrote: . . . . . .

That method will most definitely not do what you want.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic