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 6 of 6

Thread: Help with Inheritance and Polymorphism

  1. #1
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Exclamation Help with Inheritance and Polymorphism

    Hey, I need to allow the user to select a shape, out of three shapes, Square, Circle or Triangle.
    They then input a Length value for that shape, which is then used to calculate the boundary length and area of the chosen shape. Which is then displayed, and the program returns to the shape selection screen.

    This needs to be done with 4 classes, 3 of which hold the calculations for the 3 different shapes, and the last class, the super class(if that's what it is, not too sure).

    Here's what i have so far:

    --------------MyShape.java-------

    package calculations;
     
    import java.io.*;
     
    import java.util.Scanner;
     
    public class MyShape
    {
    double boundaryLength;
    double area;
    double length;
     
    public static void main(String[] args)
    {
    int number ;
    double length;
     
    Scanner scan = new Scanner(System.in);
     
    System.out.println("Select a shape:");
    System.out.println("1. Square");
    System.out.println("2. Circle");
    System.out.println("3. Triangle");
    number = scan.nextInt() ;
     
    if (number == 1)//this will use the Square class
    {
    System.out.println("Enter the length: ") ;
    length = scan.nextDouble();
    }
    else if (number == 2)//this will use the Circle class
    {
    System.out.println("Enter the length: ") ;
    length = scan.nextDouble();
    }
    else if (number == 3)//this will use the Triangle class
    {
    System.out.println("Enter the length: ") ;
    length = scan.nextDouble();
    }
     
    System.out.println("Boundary Length = " +boundaryLength);
    System.out.println("Area = " +area);
     
    }
    }

    --------------Square.java-------------

    package calculations;
     
    class Square
    {
    private double boundaryLength = length + length + length + length;
     
    private double area = length * length;
    }

    --------------Circle.java-------------

    package calculations;
     
    public class Circle
    {
    private double boundaryLength = 2 * length * Math.PI;
     
    private double area = length * length * Math.PI;
    }

    --------------Triangle.java-----------

    package calculations;
     
    public class Triangle
    {
    private double boundaryLength = (length + length) + Math.sqrt(2*length*length);
     
    private double area = 0.5 * length * length;
    }

    I know this is probably completely wrong, but if you could help me in the right direction, it would be great. I'm not looking for a completed program, just help in how i can get this working.

    Thanks, Jason.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,495
    My Mood
    Mellow
    Thanks
    152
    Thanked 610 Times in 597 Posts

    Default Re: Help with Inheritance and Polymorphism

    What help do you need? If you have a question, ask it and be as specific as possible. If there are errors, post them, copied and pasted from just as they appear at your end. If the program is not acting as it should, describe the incorrect operation, provide sample runs, if possible, and point to the code that you think is responsible for the incorrect operation. Otherwise, how are we to know what help you need?

  3. #3
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Default Re: Help with Inheritance and Polymorphism

    How can i make it so that,
    1. the user presses a number to select a shape, for example, 1 for the Square,
    2. then the user enters the Length, for example 4,
    3. this number is then sent to the Square class, which calculates the boundary length and area,
    4. the boundary length is returned, and displayed to the user.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,495
    My Mood
    Mellow
    Thanks
    152
    Thanked 610 Times in 597 Posts

    Default Re: Help with Inheritance and Polymorphism

    1 & 2 - I see you have the code to get the user's menu selection that specifies the shape and then the program collects a length.

    3 & 4 - The next step would be to use that info to create an instance of the desired shape. For example, if the user selects Square with a length of 4, an instance of Square might be created:

    Square square = new Square( length );

    From there, the desired property of the square is calculated and displayed:
    // a method in Square to calculate surface area
    public double getArea()
    {
        return length * length * 6;
    }
     
    // a statement in main()  that shows the area of a square
    System.out.println( "The surface area of the square is: " + square.getArea() );

  5. #5
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Default Re: Help with Inheritance and Polymorphism

    if (number == 1)//this will use the Square class
            {
                Square square = new Square(length);
                System.out.println("Enter the length: ") ;
                length = scan.nextDouble();
            }

    would this be how I would create an instance of Square ?

    package calculations;
     
    class Square 
    {
           public double getArea()
           {
               return length * length;
           }
     
           public double getBoundaryLength()
           {
               return length + length + length + length;
           }
    }

    and, is that what my Square class should look like ?

    thanks, Jason.

    --- Update ---

    I've got it working! Here's my code so far, I need to add some validation into the user input and I'm done.

    ----MyShape----
    package calculations;
     
    import java.util.Scanner;
     
    public class MyShape 
    {
        private static double boundaryLength;
        private static double area;
        private static double length;
     
        public static Scanner scan = new Scanner(System.in);
     
        public static int Menu()
        {
            System.out.println("\nSelect a Shape or Exit: \n");
            System.out.println("1. Square");
            System.out.println("2. Circle");
            System.out.println("3. Triangle");
            System.out.println("4. Exit");
     
            System.out.print("\nEnter choice:");
            int option = scan.nextInt();
     
            return option;
        }// end menu
     
        public static void main(String[] args) 
        {       
            int option = 0;
     
            while (option != 4)
            {
                option = Menu();
     
                switch(option)
                {
                    case 1:
                        Square square;
                        square = new Square(length);
                        System.out.println("Enter the length: ") ;
                        length = scan.nextDouble();
                        boundaryLength = square.getBoundaryLength(length);
                        area = square.getArea(length);
                        System.out.println("Boundary Length = " +Math.round(boundaryLength));
                        System.out.println("Area = " +Math.round(area));
                        break;
                    case 2:
                        Circle circle = new Circle(length);
                        System.out.println("Enter the length: ") ;
                        length = scan.nextDouble();
                        boundaryLength = circle.getBoundaryLength(length);
                        area = circle.getArea(length);
                        System.out.println("Boundary Length = " +Math.round(boundaryLength));
                        System.out.println("Area = " +Math.round(area));
                        break;
                    case 3:
                        Triangle triangle = new Triangle(length);
                        System.out.println("Enter the length: ") ;
                        length = scan.nextDouble();
                        boundaryLength = triangle.getBoundaryLength(length);
                        area = triangle.getArea(length);
                        System.out.println("Boundary Length = " +Math.round(boundaryLength));
                        System.out.println("Area = " +Math.round(area));
                        break;
                    case 4:
                        System.out.println("Exit System");
                        System.out.println("-----------\n");
                        break;
                    default:
                        System.out.println("Invalid option");
                        System.out.println("--------------\n");
                }
            }
        }
    }

    ----Square----
    package calculations;
     
    public class Square 
    {
        private double length;
     
        public Square(double length)
        {
            length = length;
        }
     
          public double getArea(double length)
     
           {
               return length * length;
           }
     
           public double getBoundaryLength(double length)
           {
               return length + length + length + length;
           }
    }

    ----Circle----
    package calculations;
     
    public class Circle 
    {
        private double length;
     
        public Circle (double length)
        {
            length = length;
        }
     
        public double getArea(double length)
        {
            return length * length * Math.PI;
        }
     
        public double getBoundaryLength(double length)
        {
            return 2 * length * Math.PI;
        }
    }

    ----Triangle----
    package calculations;
     
    public class Triangle 
    {
            private double length;
     
        public Triangle(double length)
        {
            length = length;
        }
     
          public double getArea(double length)
     
           {
               return 0.5 * length * length;
           }
     
           public double getBoundaryLength(double length)
           {
               return (length + length) + Math.sqrt(2 * length * length);
           }
    }

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,495
    My Mood
    Mellow
    Thanks
    152
    Thanked 610 Times in 597 Posts

    Default Re: Help with Inheritance and Polymorphism

    You've made great progress.

    In your constructors (all of them), you need to differentiate between the instance variable 'length' and the method parameter 'length'. That is done by using the keyword 'this', as in:
    // the class' instance variable
    double length;
     
    // the single-parameter constructor
    public Square( double length )
    {
        // the class instance variable (this.length) is assigned the value
        // passed to the constructor by the parameter length
        this.length = length
    }
    The current statement, length = length; does nothing.

    I know this is confusing at first. One of your first questions might be, "Well, why don't we just give the method parameter a different name so differentiating isn't necessary?" That's a great question, and I don't know if I'll give the best answer, but it's due to a combination of convention, simplicity, and clarity. You can certainly argue that initial confusion contradicts clarity, but when you get used to it, you'll come to depend on it being done this way.

    I didn't look through your code to see if you'd attempted input validation, but if you need help with that, please be specific.

Similar Threads

  1. Inheritance and Polymorphism assignment!!
    By m7abraham in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 14th, 2012, 04:11 PM
  2. Polymorphism Question
    By BuhRock in forum Java Theory & Questions
    Replies: 1
    Last Post: October 1st, 2012, 11:09 AM
  3. Polymorphism.
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 2
    Last Post: March 11th, 2012, 09:28 AM
  4. Polymorphism test
    By speedycerv in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2011, 07:15 AM
  5. Polymorphism issue
    By LDM91 in forum Java Theory & Questions
    Replies: 5
    Last Post: November 28th, 2010, 02:26 PM