Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decide what constructor call based on user input

I would need to ask the user how many sides has the Figure he wants to draw, then call the right constructor to instantiate the object.

Below is my attempt to solve the answer using IF statements (or I could have used a switch) , but I don't know if it's the best way to do this, possibly with Java inheritance and polymorphism.

All the classes extends the class Figure.

CLASSES DIAGRAM:

      --------------- Figure ---------------
      ^         ^             ^            ^
      |         |             |            |
      |         |             |            |
   Circle    Triangle      Rectangle     Exagone

MAIN CLASS:

import java.util.Scanner;

class Draw {

   static void main(String[] args){

       Scanner userInput = new Scanner(System.in);
       int num_sides;

       //user input
       System.out.println("How many sides has the figure you want to draw?");   
       num_sides = userInput.nextInt();


       //---> deciding what constructor to call with if statements

       if(num_sides == 0){
          Figure f1 = new Circle();
       }
       else if(num_sides == 3){
          Figure f1 = new Triangle(); 
       }
       //...
       else{
          System.out.println("Error. Invalid sides number");
       }


   }
}

CLASSES CODE:

class Figure{

    private int sides;

    public Figure(int num_sides){
       sides = num_sides;
    }
}


class Circle extends Figure{

   public Circle(){
      super(0);
   }

}

//... all the other classes has the same structure of Circle
like image 386
fbid Avatar asked Dec 29 '15 22:12

fbid


People also ask

Can constructors take input parameters?

A Java class constructor initializes instances (objects) of that class. Typically, the constructor initializes the fields of the object that need initialization. Java constructors can also take parameters, so fields can be initialized in the object at creation time.

Which constructor is called first parent or child?

In simple words, we can say that the parent constructor gets called first, then of the child class.

What is meant by parameterized constructor?

Parameterized Constructor – A constructor is called Parameterized Constructor when it accepts a specific number of parameters. To initialize data members of a class with distinct values.


1 Answers

Factory Methods

Have you considered the Factory Method Pattern? Basically, you have a method that's something like this:

public Figure createInstance(int numSides) {
    Figure figure = null;

    switch(numSides) {
    case 0:
        figure = new Circle();
        break;
    case 3:
        // etc...
        // Make a case for each valid number of sides
        // Don't forget to put a "break;" after each case!
    default:
        // Not a valid shape; print your error message
    }
    return figure;
}

And let that factory method do the deciding instead.

like image 55
Caleb Brinkman Avatar answered Sep 28 '22 03:09

Caleb Brinkman