I'm implementation polymorphism and have one confusing that if I use instanceof then I'm trying to perform casting which voids polymorphism so will that be any way to avoid instanceof with the code and still have the program working? Block of code where I have used instanceof starts from line , also if anyone can point out if there is any redundant code here. Any help is much appreciated.

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

//implements class Orders from Constant

public class Orders implements Constants
{
//Declaring array

private static Rocks[] rocksArray;
private static int numOrders;
    /**
     * @param args
     */
    public static void main(String[] args)
    {
        numOrders = 0;

        //setting the max value for array

        rocksArray = new Rocks[MAX_ORDERS];
        String choice;
        while (true)
{
//Choice option begins --->

choice = "";

//While statement

while (!choice.equalsIgnoreCase("m") && !choice.equalsIgnoreCase("b")
&& !choice.equalsIgnoreCase("e") && !choice.equalsIgnoreCase("d"))
{
choice = JOptionPane.showInputDialog
(" Add monument-only order >> (m)\n " +
 "Add a monument & base order >> (b) \n " +
 "Stop entering orders >> (e)\n Display orders >> (d) ");
if (!choice.equalsIgnoreCase("m") && !choice.equalsIgnoreCase("b")
&& !choice.equalsIgnoreCase("e") && !choice.equalsIgnoreCase("d"))
    {
JOptionPane.showMessageDialog(null, "Invalid option!");
    }
}

if (choice.equalsIgnoreCase("e"))
{
break;
}
if (choice.equalsIgnoreCase("d"))
{
displayOutput();
}
else
{
    //asking & getting user input -->

    int quatity = Integer.parseInt(JOptionPane.showInputDialog
    ("Please enter the quatity: "));
    double width = Double.parseDouble(JOptionPane.showInputDialog
    ("Please enter the width: "));
    double thickness = Double.parseDouble(JOptionPane
    .showInputDialog("Please enter the thickness: "));
    double height = Double
    .parseDouble(JOptionPane.showInputDialog
    ("Please enter the height: "));
    if (choice.equalsIgnoreCase("m"))
    {
    Monument newMonument = new Monument
    (quatity, width, thickness, height);
    rocksArray[numOrders] = newMonument;
    }
    else
    {
    double baseThickness = Double.parseDouble(JOptionPane
    .showInputDialog("Please enter the base thickness: "));
    double baseDiameter = Double.parseDouble(JOptionPane
    .showInputDialog("Please enter the base diameter: "));
    MonumentBase newMonumentBase = new MonumentBase
    (quatity, width, thickness, height,
    baseThickness, baseDiameter);
    rocksArray[numOrders] = newMonumentBase;
        }
    numOrders++;
    }  // user input ends
        }
    // Displays Output --->
    displayOutput();
    }
    private static void displayOutput()
    {
    StringBuilder msg = new StringBuilder();
    for (int i = 0; i < numOrders; i++)
    {

    //appending the appropriate values for monument & monument base.

    msg.append("Width: " + rocksArray[i].getWidth());
    msg.append(" Height: " + rocksArray[i].getHeight());
    msg.append(" Thickness: " + rocksArray[i].getThickness());
    msg.append(" Cubic feet: " + rocksArray[i].getCubicFeet());
    msg.append(" Cubic inches: " + rocksArray[i].getCubicInches());
    msg.append(" Quantity: " + rocksArray[i].getQuantity());
    msg.append(" Total price: " + rocksArray[i].getTotalPrice());

    **if (rocksArray[i] instanceof MonumentBase)
    {
    msg.append(" Base diameter: " +
    ((MonumentBase) rocksArray[i]).getBaseDiameter());
    msg.append(" Base radius: " +
    ((MonumentBase) rocksArray[i]).getBaseRadius());
    msg.append(" Base thickness: " +
    ((MonumentBase) rocksArray[i]).getBaseThickness());
    }
    msg.append("\n");**

    } // display output ends

        // create a JTextArea

        JTextArea textArea = new JTextArea(10, 80);
        textArea.setText(msg.toString());
        textArea.setEditable(false);

        // wraping scrollpane around it

        JScrollPane scrollPane = new JScrollPane(textArea);

        // display them in a message dialog

        JOptionPane.showMessageDialog(null, scrollPane);
    }
} // end class

Edited 4 Years Ago by maverick420

Here's the rest four java files which completes the enitre package now the question is if I use** instanceof** then I'm trying to perform casting which voids polymorphism so will that be any way to avoid instanceof with the code and still have the program working? P.S: I have used instanseof in the above mentioned code

// Class Monument Starts
public class Monument implements Rocks, Constants
{
    private double width;
    private double thickness;
    private double height;
    private int quantityOrdered;
    public Monument()
    {
        setWidth(DEFAULT_WIDTH);
        setThickness(DEFAULT_THICKNESS);
        setHeight(DEFAULT_HEIGHT);
        setQuantity(DEFAULT_QUANTITY);
    }
    public Monument(int inputQuantity)
    {
        setWidth(DEFAULT_WIDTH);
        setThickness(DEFAULT_THICKNESS);
        setHeight(DEFAULT_HEIGHT);
        setQuantity(inputQuantity);
    }
    public Monument(int inputQuantity, double inputWidth,
    double inputThickness, double inputHeight)
    {
        setWidth(inputWidth);
        setThickness(inputThickness);
        setHeight(inputHeight);
        setQuantity(inputQuantity);
    }
    public void setWidth(double inputWidth)
    {
        width =
        ((inputWidth >= DEFAULT_WIDTH && inputWidth <= MAX_WIDTH) ?
                            inputWidth : ERROR_VALUE);
    }
    public void setThickness(double inputThickness)
    {
        thickness =
        ((inputThickness >= DEFAULT_THICKNESS &&
                                inputThickness <= MAX_THICKNESS) ?
                                    inputThickness : ERROR_VALUE);
    }
    public void setHeight(double inPutHeight)
    {
        height =
    ((inPutHeight >= DEFAULT_HEIGHT && inPutHeight <= MAX_HEIGHT) ?
    inPutHeight : ERROR_VALUE);
    }

    public void setQuantity(int inputQuantity)
    {
        quantityOrdered = (inputQuantity >= DEFAULT_QUANTITY ?
                        inputQuantity : ERROR_VALUE);
    }
    public int getQuantity()
    {
        return quantityOrdered;
    }

    public double getThickness()
    {
        return thickness;
    }

    public double getWidth()
    {
        return width;
    }

    public double getHeight()
    {
        return height;
    }

    public double getCubicInches()
    {
        return getWidth() * getThickness() * getHeight() *
        getQuantity();
    }
    public double getCubicFeet()
    {
        return (getCubicInches() / CU_INS_TO_CU_FEET);
    }

    public double getTotalPrice()
    {
        return getCubicFeet() * CURRENT_PRICE;
    }
} //Class Monument Ends

//MonumentBase Starts
public class MonumentBase extends Monument
{
    private double baseThickness;
    private double baseDiameter;
    public MonumentBase()
    {
        setBaseThickness(DEFAULT_THICKNESS);
        setBaseDiameter(DEFAULT_WIDTH);
    } // end zero parameter constructor

    public MonumentBase(int inQuantity)
    {
        super(inQuantity);
        setBaseThickness(DEFAULT_THICKNESS);
        setBaseDiameter(DEFAULT_WIDTH);
    } // end single parameter constructor
    public MonumentBase(int inQuantity, double inMonumentWidth,
    double inMounumentThickness, double inMonumentHeight,
    double inBaseThickness, double inBaseDiameter)
    {
    super(inQuantity, inMonumentWidth,
    inMounumentThickness, inMonumentHeight);
        setBaseThickness(inBaseThickness);
        setBaseDiameter(inBaseDiameter);
    } // end 6 parameter constructor
    public void setBaseThickness(double localBaseThickness)
    {
        if(localBaseThickness >= getThickness() &&
        localBaseThickness <= (getThickness() + EXTRA_THICKNESS))
            baseThickness = localBaseThickness;
        else
            baseThickness = ERROR_VALUE;
    }// end method setBaseThickness

    public void setBaseDiameter(double localBaseDiameter)
    {
        if(localBaseDiameter >= getWidth() &&
                localBaseDiameter <= (getWidth() + EXTRA_DIAMETER))
                    baseDiameter = localBaseDiameter;
        else
            baseDiameter = ERROR_VALUE;
    } // end method setBaseDiameter

    public double getBaseDiameter()
    {
        return baseDiameter;
    }
    public double getBaseRadius()
    {
        return (getBaseDiameter() / 2);
    }
    public double getBaseThickness()
    {
        return baseThickness;
    }
    public double getCubicInches()
    {
        return super.getCubicInches() +
            (Math.PI * Math.pow(getBaseRadius(), 2) *
                getBaseThickness() * getQuantity());
    }
    public double getCubicFeet()
    {
        return (getCubicInches() / CU_INS_TO_CU_FEET);
    }
    public double getTotalPrice()
    {
        return (getCubicFeet() * CURRENT_PRICE);
    }
} // end MonumentBase class

//interface starts
public interface Rocks
{
    double getCubicFeet();
    double getCubicInches();
    double getTotalPrice();
    double getHeight();
    int getQuantity();
    double getThickness();
    double getWidth();
    void setHeight(double inPutHeight);
    void setQuantity(int inputQuantity);
    void setThickness(double inputThickness);
    void setWidth(double inputWidth);
}  // interface ends

// start interface
public interface Constants
{
    public final static double DEFAULT_WIDTH = 24.5;
    public final static double MAX_WIDTH = 80.5;

    public final static double DEFAULT_THICKNESS = 4.5;
    public final static double MAX_THICKNESS = 12.5;

    public final static double DEFAULT_HEIGHT = 16.5;
    public final static double MAX_HEIGHT = 50.5;

    public final static int DEFAULT_QUANTITY = 5;
    public final static int ERROR_VALUE = 0;
    public final static double CURRENT_PRICE = 478.97;
    public final static int CU_INS_TO_CU_FEET = 1728;

    public static final int EXTRA_THICKNESS = 3;
    public static final int EXTRA_DIAMETER = 24;
    public static final int MAX_ORDERS = 100;
}  // end interface
This article has been dead for over six months. Start a new discussion instead.