Results 1 to 7 of 7
  1. #1
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default Inheritance and Subclasses

    Hello there,
    I am trying to solve this exercise I am doing.
    Basically I need to create a ,
    Java Code:
    Vehicle
    class ( I already did) that has 3 instance variables (
    Java Code:
     double horsepower, aerodynamics, weight
    )
    In this class as well I did the following
    Java Code:
    public class Vehicle
    {
        private double horsepower, aerodynamics, weight;
    
        public Vehicle(double initialHorsepower, double initialAerodynamics, double initialWeight)
        {
            // initialise instance variables
            horsepower = initialHorsepower;
            aerodynamics = initialAerodynamics;
            weight = initialWeight;
        }
        
        public double getHorsepower()
        {
            return horsepower;
        }
        
        public double getAerodynamics()
        {
            return aerodynamics;
        }   
        
        public double getWeight()
        {
            return weight;
        }
        
    
    }
    Then I created a subclass of Vehicle called SportCar (I extended the class)
    it has an additional variable in it )
    Java Code:
     double topspeed
    All sportcars have aerodynamics = 0.5

    Java Code:
    public class SportCar extends Vehicle
    {
        public double topspeed;
    
        public SportCar(double initialHorsepower, double initialAerodynamics, double initialWeight)
        {
           super(initialHorsepower);
           super(initialAerodynamics);
           super(initialWeight);
        }
        
        public double getTopspeed()
        {
            return topspeed;
        }
    
    }
    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
    Java Code:
    public class TestConsumption
    {
       
        public static void main(String[] args)
        {
            SportCar SportCar1 = new SportCar();
            SportCar SportCar2 = new SportCar();
            SportCar SportCar3 = new SportCar();
            
            SportCar.getAerodynamics(0.5);
        }
    
    }
    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.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

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

    If the compiler says "Cannot find symbol" then they're not correct! It is a good idea to fix compiler errors as they arise so they don't build up.

    The "Cannot find symbol" message will occur whenever you call a method with the wrong number or wrong type of arguments. (Along with other times like when you misspell a variable or method.)

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

    No.

    Since all the sportscars have an aerodynamic value of 0.5 you should change the SportCar constructor so that it only takes two arguments: initialHorsepower and initialWeight.

    You are only allowed to call the super() constructor once. (Read your textbook! ;) The way to do it here is:

    Java Code:
    super(initialHorsepower, 0.5, initialWeight);

    Basically that is the way that all sportscars end up with an aerodynamics value of 0.5: the constructor puts it there at the time they are constructed.

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

    You really should read your textbook to see how contructors are called, give it your best guess, wrestle with the compiler messages, and then post here. What you have posted, "new SportCar()", clearly won't do because it doesn't set the appropriate values for the cars. Remember the "Cannot find symbol" message and its meaning: "you aren't using the right arguments!"

    One thing I will say is that sportscars are supposed to have top speeds - so you should add another argument to the SportCar constructor that allows you to set the top speed when you construct a sportscar:

    Java Code:
    public SportCar(double initialHorsepower, double initialWeight, double initialTopspeed)
    {
        // etc

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    You're mixing up things; a Vehicle (the superclass) has three properties and you've implemented the constructor correctly:

    Java Code:
    public Vehicle(double initialHorsepower, double initialAerodynamics, double initialWeight) { ... }
    A SportCar (a subclass of class Vehicle) always has an aerodonymics == 0.5 and it has an additional property: topSpeed; so, there's no need to pass a value for the aerodynamics to the constructor of a SportCar:

    Java Code:
    public SportCar(double initialHorsepower, double initialWeight, double topSpeed) { ... }
    This constructor should call its superclass constructor with the sensible values:

    Java Code:
    super(initialHorsePower, 0.5, initialWeight);
    and it should store the topSpeed property:

    Java Code:
    this.topSpeed= topSpeed;
    So the constructor for your SportCar should look like this:

    Java Code:
    public class SportCar extends Vehicle {
       private double topSpeed;
       public SportCar(double initialHorsepower, double initialWeight, double topSpeed) { 
          super(initialHorsePower, 0.5, initialWeight);
          this.topSpeed= topSpeed;
       }
       ...
    }
    kind regards,

    Jos

    edit: too slow again ... ;-)
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    edit: too slow again ... ;-)

    It's a timezone thing. I'm n coffees ahead of you.

  5. #5
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    Thank you very much pbrockway2 and josAH.
    I will change my code accordingly.

    and pbrockway2, yes I have been reading a textbook. I asked in the forum to get a better solution.

    Thank you again.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by pbrockway2 View Post
    It's a timezone thing. I'm n coffees ahead of you.
    I'm working on it at full speed: I just finished my first cigaret together with my first espresso! ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    Hey there, regarding my previous problem, now I have to implement polymorphism to the code.

    I have to create a van class that extends vehicle class.
    A van object has an additional variable carryweight. this class also has a new getter method, for carryweight.
    All vans have aerodynamics = 0.9

    In the vehicle class, I added new method, an acceleration method that returns the acceleration of the vehicle according to the formula I have written below.
    Java Code:
    public class Vehicle
    {
        // instance variables - replace the example below with your own
        private double horsepower, aerodynamics, weight;
    
        public Vehicle(double initialHorsepower, double initialAerodynamics, double initialWeight)
        {
            // initialise instance variables
            horsepower = initialHorsepower;
            aerodynamics = initialAerodynamics;
            weight = initialWeight;
        }
        
        public double getHorsepower()
        {
            return horsepower;
        }
        
        public double getAerodynamics()
        {
           return aerodynamics;
        }   
        
        public double getWeight()
        {
            return weight;
        }
        public double acceleration()
        {
            return ((100/this.horsepower)*this.aerodynamics*this.weight/100);
        }
        
    
    }
    class Van
    Java Code:
    public class Van extends Vehicle
    {
        // instance variables - replace the example below with your own
        private double carryweight;
    
        public Van(double initialHorsepower,double initialWeight,double initialCarryweight)
        {
            // initialise instance variables
            super(initialHorsepower,0.9,initialWeight);
            this.carryweight = initialCarryweight;
        }
        public double getCarryweight()
        {
            return carryweight;
        }
     
    }
    currently this is what my main method looks like
    Java Code:
    public class TestConsumption
    {
        public static void main(String[] args)
        {
            SportCar SportCar1 = new SportCar(200,1500,220);
            SportCar SportCar2 = new SportCar(100,1000,170);
            SportCar SportCar3 = new SportCar(135, 1100.2, 173);
            Van Van1 = new Van(100,0.9,3500);
            
            System.out.println("The fuel consumption for SportCar1 is"+ SportCar1.getConsumption());
            System.out.println("The fuel consumption for SportCar2 is"+ SportCar2.getConsumption());
            System.out.println("The fuel consumption for SportCar3 is"+ SportCar3.getConsumption());
         
        }
    
    }
    what I want the program to compute is the acceleration for the previous defined sportcars as well as Van with horsepower=100, aerodynamics=0.9,weight=3500,carryweight=160.4

    my problems are:
    - how do I make the carryweight of the van =160.4 (should I add double param in my get method)

    - how do I compute accelerations of the sportcars and vans.
    - how do I make use of polymorphism in this case.


    thanks.

Similar Threads

  1. Dynamic Subclasses?
    By MrFish in forum Advanced Java
    Replies: 5
    Last Post: 11-07-2010, 01:28 AM
  2. Lost with subclasses
    By vinyacam in forum New To Java
    Replies: 3
    Last Post: 05-09-2010, 07:12 PM
  3. subclasses and inheritance - how's it all work?
    By 711groove in forum New To Java
    Replies: 14
    Last Post: 12-18-2009, 09:04 AM
  4. Generics and subclasses
    By Sven in forum Advanced Java
    Replies: 1
    Last Post: 12-22-2008, 06:06 PM
  5. Hibernate subclasses
    By Ed in forum JDBC
    Replies: 2
    Last Post: 07-02-2007, 04:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •