Jennifer Bell

Jennifer Bell

  • NA
  • 14
  • 13.9k

default constructor not catching

Nov 9 2012 1:22 AM
When 0 is entered for the side and length the default constructor is supposed to use the default values of side = 7 and length = 10. I do not know why mine is not working can anyone please explain to me why mine this is happening.

using



System;


using



System.Collections.Generic;


using



SC = System.Console;


using



System.Linq;


using



System.Text;



public



class abc


{


   


public static void Main()


    {


       


String continueStr = "Y";


       


double side, length;



       



       


while (continueStr.ToUpper()!="N")


        {


           


SC.Write("\nEnter the value for side ");


            side =


Convert.ToDouble(SC.ReadLine());


           


while (side < 0)


            {


               


SC.WriteLine("The side may not be Negative! Please re-enter");


               


SC.Write("Enter the value for side ");


                side =


Convert.ToDouble(SC.ReadLine());


            }



           


SC.Write("\nEnter the value for length ");


            length =


Convert.ToDouble(SC.ReadLine());


           


while (length < 0)


            {


               


SC.WriteLine("The width may not be Negative! Please re-enter");


               


SC.Write("Enter the value for length ");


                length =


Convert.ToDouble(SC.ReadLine());


            }



           


if (length == 0 && side == 0 )


            {


               


Prism myPrism = new Prism(side, length);


                myPrism.displayData();


            }


           


else if (length >= 0 && side >= 0 )


            {


               


Prism myPrism = new Prism(side, length);


                myPrism.displayData();


            }



           


SC.Write("\nWould you like to enter more data (Y/N) ");


            continueStr =


SC.ReadLine();


        }


      


    }


// end of Main method


}


// end of Main class








public



class Square



private double side;     //in a square, both sides are the same


   


public Square ()


    {  


// default constructor


         side = 7;


    }


   


public Square (double s)


    {   side = s;


    }


   


public double getSide()           // a getter method


    {  


return side;


    }


   


public double findArea()


    {  


return (side * side);


    }


}                                                                                            


// see the next page for more                                       





public



class Prism : Square


{


   


private double side;


   


private double length;



   


public Prism():base()



    {


        length = 10;


//default constructor


    }



   


public Prism(double s, double l):base(s)


    {


        length = l;


// non-default constructor


    }



   


new public double findArea()


    {


       


double surfaceArea;


        surfaceArea =


base.findArea();


        side =


Math.Round((((5.1 / 2) * (5.1 / 2)) + (9.2 * 9.2)),2);


       


return Math.Round((2 * surfaceArea) + (5.1 * 6.7) + (2 * (getSide() * 6.7)), 2);


    }


//???



   


public double volume()


    {


       


return Math.Round(findArea() * length,2) ;


    }



   


public void displayData()


    {


       


SC.WriteLine("\nThe side of the Prism is {0,5}",


       


Math.Round(side, 2).ToString("F2"));



       


SC.WriteLine("\nThe length of the Prism is {0,9}",


       


Math.Round(length, 2).ToString("F2"));



       


//SC.WriteLine("\nThe Volume is



    }





}





Answers (2)