Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By ozzyman
  • 1 Post By ozzyman

Thread: Basic inheritence question

  1. #1
    jubbiejub is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Basic inheritence question

    I'm having a hell of a time finishing this assignment.

    "RK4 is a numerical integration technique that can be used to evaluate the integral of a given function over a given range. The function will be defined in your class as

    double f(double t, double y)

    The functions you will be integrating are the following:

    log(x) / x5 from 1 to 2
    log(x)3 from 0.001 to 5
    x3ex2 from -2 to 2
    x3 cos(x2) from 0 to pi

    Using one of your classes from Assignment 4 as a base class, write a class for each of the above functions. You are only allowed to override f(), and you may not make changes to your base class."

    Here is my base class:

    Java Code:
    public class RK4{
    
    public double f(double x, double y){
    return 5 - 1/x + 3 * x * x;
    }
    
    public double rk(double x, double y, double step){
    
    double k = 0.001;
    
     while(k<=20){
    
    
        double h = step;
        double k0 = h * f(x, y);
        double k1 = h * f(x + 0.5 * h, y + 0.5 * k0);
        double k2 = h * f(x + 0.5 * h, y + 0.5 * k1);
        double k3 = h * f(x + h, y + k2);
    
        return y + 1.0/6.0 * (k0 + 2 * k1 + 2 * k2 + k3);
    
    }
    
         return x;
    
    }
    
    }
    And here is my attempt at using override and inheritance:

    Java Code:
    public class RK4one extends RK4{
    
    public double f(double x, double y){
        return (Math.log10(x))/(x*x*x*x*x);
        }{
    
    rk();
    
    
            }
    
    }
    Can somebody please explain what I am supposed to be doing? I don't quite understand this(); or super ();.
    Last edited by jubbiejub; 03-17-2012 at 03:08 AM.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Basic inheritence question

    Did you have any compile errors or runtime errors?

    What are you having trouble with?

    'this' is a keyword used to reference the class from which the keyword 'this' is used.

    'this()' refers to the constructor from 'this' class:
    Java Code:
    int someField;
    
    //constructor 1:
    RK4() {
       this(0); //calls constructor 2 with '0' as the parameter
    }
    
    //constructor 2:
    RK4(int aNumber) {
       someField = aNumber;
    }
    'super' refers to the parent class and 'super()' refers to the parent class constructor, example:
    Java Code:
    class BorderPanel extends JPanel {
        super(); //JPanel constructor
        setBorder(BorderFactory.createLineBorder(Color.black));
    }
    Hope this helps...

    p.s. Please post Java code within CODE tags.
    noobplus likes this.

  3. #3
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Basic inheritence question

    Quote Originally Posted by ozzyman View Post
    calls constructor 2 with '0' as the parameter
    'super' refers to the parent class and 'super()' refers to the parent class constructor,
    thx I was havin doubts in those cases, and got clarified

    thx
    dhilip

  4. #4
    jubbiejub is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: Basic inheritence question

    Basically, I have no idea how to actually use inheritance to integrate the functions. I'm not even sure if my base class is correct at the moment.

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Basic inheritence question

    To integrate the functions... It's probably a lot simpler than you think, and that's why you're confused.

    Consider this Animal class:

    Java Code:
    class Animal {
    
        boolean gender; //male=true
        int numLegs = 4; //4 legs by default
        int numEyes = 2; //2 eyes by default
    
        Animal (boolean isMale) {
            gender = isMale;
        }
    
        public void eat() { System.out.println("Animals need to eat."); }
    
        public void move() { System.out.println("Animals need to move."); }
    
        //methods to obtain properties of Animal
        public boolean isMale() { return gender; }
        public int getNumLegs() { return numLegs; }
        public int getNumEyes() { return numEyes; }
        
    }
    It would be a pain the *** if you had to write out all those fields and methods for every animal you created (e.g. Lion, Tiger, etc).

    Because they are properties and methods shared by all Animals, it is better to put them in the Animal class.

    Any class that extends animal, will inherit those methods.

    Now we want a Lion class. All we have to do is add the extra properties and methods that only Lions and its subclasses have, e.g. a Mane.
    Java Code:
    class Lion extends Animal {
            boolean hasMane;
            Lion (boolean withMane) {
                super(withMane); //only male lions have a Mane
                hasMane = withMane;
            }
    
            public boolean hasMane() { return hasMane; }
    }
    Now if I were to instantiate a Lion, it would have all the methods and properties of Animal as well as Lion:
    Java Code:
    Lion simba = new Lion(true);
    simba.eat(); //output: "Animals need to eat.";
    simba.move(); //output: "Animals need to move.";
    You can also hide an inherited method, if it's public, like this:
    Java Code:
    class Lion extends Animal {
        //as above
        ...
        @Override
        public void move() {
            System.out.println("Lions need to move.");
        }
    }
    Then try this:
    Java Code:
    simba.move(); //output: "Lions need to move";
    noobplus likes this.

  6. #6
    jubbiejub is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: Basic inheritence question

    Could you possibly give me an example of what it would look like with my code? I'm still sort of confused about how to use my rk method in my extended class.

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Basic inheritence question

    So if you create an object RK4one, it will have all the methods of RK4 as well as the added method

    Java Code:
    RK4one example = new RK4one();
    example.f(1.5, 2.5); //refers to the method in RK4one (since it overrides the method 'f(double, double)' in RK4)
    example.rk(1.0, 2.0, 1.0); //refers to method in RK4 (not the one in RK4one because they have different method signatures)
    example.rk(); //refers to method in RK4one
    That's something I forgot to mention, if you want to Override an inherited method, your method signature has to be the same in the subclass.

  8. #8
    jubbiejub is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: Basic inheritence question

    Ok that makes a lot more sense now. For some reason I wasn't getting the fact that to use the methods I had to actually initialize a new RK4 object. I think I'll be able to do what the assignment wants me to now.

    Except for the fact I think my code doesn't do exactly what it should. It is supposed to integrate different functions on different intervals, which I could easily do when I was creating a separate class for each function. However now I think it's all screwed up since I have the same rk method for each interval. Any opinions on that?

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Basic inheritence question

    I'm sorry I didn't understand any of that lol...

    Why doesn't it do what it should?

Similar Threads

  1. Basic exception question
    By wulfgarpro in forum New To Java
    Replies: 4
    Last Post: 09-15-2010, 09:49 AM
  2. Basic JSP question..
    By casid in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 12-28-2009, 06:51 PM
  3. Basic question on OOP.
    By madthinker in forum New To Java
    Replies: 7
    Last Post: 01-26-2009, 07:09 PM
  4. Basic question about EJB
    By javaplus in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-15-2008, 05:44 PM
  5. Very basic question
    By gvi in forum New To Java
    Replies: 2
    Last Post: 10-30-2007, 06:30 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
  •