Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Inheritance & Hierarchy Exercises?

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Location
    Massachusetts
    Posts
    3
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Inheritance & Hierarchy Exercises?

    Currently, I have only just started Java, and I am looking to elaborate more from my book to enforce what I am learning. As it stands, only two exercises remain in the Workshop section of this book, and I like to make firm my knowledge of a topic, so I would like to do some more exercises. Asked around on the gaming forum I was on, but then I decided to migrate to a specialized forum to maybe aid me in my triumphs with Java far more accurately. Sorry for the digression, but in any case, I am going to give the exercise from my book and maybe you guys can help give me some exercises similar.

    Quote Originally Posted by Exercise 2
    Create an inheritance hierarchy for the pieces of a chess set. Decide where the instance variables color, startingPosition, forwardMovement, and sideMovement should be defined in the hierarchy.
    Here's a code reference, if it helps at all. The first class (VolcanoRobot) is meant to be the superclass, and the second class (VolcanoApplication) is meant to be a subclass. My goal is to do some similar programs, on my own, instead of just reviewing and copying examples from a book. And even the one solo exercise it has me do, I want to do more to get a firm grasp of this, because I feel like inheritance will play a huge role in the future.

    class VolcanoRobot {
        String status;
        int speed;
        float temperature;
     
        void checkTemperature() {
            if (temperature > 660) {
                status = "returing home";
                speed = 5;
            }
        }
     
        void showAttributes() {
            System.out.println("Status: " + status);
            System.out.println("Speed: " + speed);
            System.out.println("Temperature: " + temperature);
        }
    }

    class VolcanoApplication {
        public static void main(String[] arguments) {
            VolcanoRobot dante = new VolcanoRobot();
            dante.status = "exploring";
            dante.speed = 2;
            dante.temperature = 510;
     
            dante.showAttributes();
            System.out.println("Increasing speed to 3.");
            dante.speed = 3;
            dante.showAttributes();
            System.out.println("Changing temperature to 670.");
            dante.temperature = 670;
            dante.showAttributes();
            System.out.println("Checking the temperature.");
            dante.checkTemperature();
            dante.showAttributes();
        }
    }

    Help is much appreciated.
    Last edited by Beacon; September 21st, 2012 at 11:41 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Inheritance & Hierarchy Exercises?

    In your example code I don't see the super-class/sub-class relationship. What you have are two un-related classes in which one happens to use the other one. In order to have a super-class/sub-class relationship, you must use the extends keyword:

    public class BaseClass
    {
       // .. code
    }
     
    public class SubClass extends BaseClass
    {
       // ..code
    }

    The best candidates for practicing inheritance are situations where you can say something is something. For example, consider shapes. A triangle is a shape. A rectangle is a shape. A circle is a shape. The shape class would provide several methods which makes sense for shapes, such as what is the area or perimeter of the shape, and where is the shape.

    Side note:

    You're directly accessing the fields of the VolcanoRobot class from the VolcanoApplication class. I would recommend that you get into the habit of using getter and setter methods and declare your fields private.

    public class Class1
    {
        private int someVal;
     
        public int getSomeVal()
        {
            return someVal;
        }
     
        public void setSomeVal(int value)
        {
            someVal = value;
        }
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Beacon (September 21st, 2012)

  4. #3
    Junior Member
    Join Date
    Sep 2012
    Location
    Massachusetts
    Posts
    3
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance & Hierarchy Exercises?

    Quote Originally Posted by helloworld922 View Post
    In your example code I don't see the super-class/sub-class relationship. What you have are two un-related classes in which one happens to use the other one. In order to have a super-class/sub-class relationship, you must use the extends keyword:

    public class BaseClass
    {
       // .. code
    }
     
    public class SubClass extends BaseClass
    {
       // ..code
    }

    The best candidates for practicing inheritance are situations where you can say something is something. For example, consider shapes. A triangle is a shape. A rectangle is a shape. A circle is a shape. The shape class would provide several methods which makes sense for shapes, such as what is the area or perimeter of the shape, and where is the shape.

    Side note:

    You're directly accessing the fields of the VolcanoRobot class from the VolcanoApplication class. I would recommend that you get into the habit of using getter and setter methods and declare your fields private.

    public class Class1
    {
        private int someVal;
     
        public int getSomeVal()
        {
            return someVal;
        }
     
        public void setSomeVal(int value)
        {
            someVal = value;
        }
    }
    Oh yes! This is just an Inheritance Hierarchy exercise/concept that is being taught in my book currently, not Superclasses (it merely introduced the IDEA of Superclasses, not the code involved); it's the first chapter hehe. I'll learn about defining Superclasses and such in Chapter 5, sorry for the confusion!

    Thanks for the information. It still helps me out for when I get to Chapter 5, and I can apply your example about shapes and such to some Chapter 1 exercises.

Similar Threads

  1. class hierarchy
    By jcarosella10 in forum Object Oriented Programming
    Replies: 2
    Last Post: March 9th, 2012, 09:08 AM
  2. Inheritance
    By lewzax in forum Object Oriented Programming
    Replies: 4
    Last Post: July 8th, 2011, 01:51 PM
  3. Class hierarchy... giving me a headache, so I could use some help
    By Kerr in forum Object Oriented Programming
    Replies: 6
    Last Post: May 30th, 2011, 05:03 PM
  4. inheritance
    By b109 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 30th, 2010, 09:23 PM
  5. Exercises - CRITICAL HELP NEEDED
    By Maximillian in forum Java Theory & Questions
    Replies: 5
    Last Post: November 17th, 2009, 05:55 PM

Tags for this Thread