# Help with Inheritance and Polymorphism

• October 2nd, 2013, 05:41 AM
jason3460
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-------

Code :

```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-------------

Code :

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

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

Code :

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

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

Code :

```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.
• October 2nd, 2013, 06:16 AM
GregBrannon
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?
• October 2nd, 2013, 06:31 AM
jason3460
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.
• October 2nd, 2013, 07:04 AM
GregBrannon
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:
Code java:

```// 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() );```
• October 2nd, 2013, 08:53 AM
jason3460
Re: Help with Inheritance and Polymorphism
Code :

```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 ?

Code :

```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----
Code :

```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----
Code :

```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----
Code :

```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----
Code :

```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); } }```
• October 2nd, 2013, 03:45 PM
GregBrannon
Re: Help with Inheritance and Polymorphism
```// 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 }```