Jenni Witzel

Jenni Witzel

  • NA
  • 27
  • 0

Homework

Nov 7 2008 5:53 PM

 

CIS 218 – C# Programming
Lab 10 - 20 points
Using a Subclass

Due: Friday, Nov. 7
by 11:59pm

 

Instructions:  Create a C# program that uses a class named Triangle (shown below) in order to produce a subclass of Triangle called Prism.  Use the program to create a Prism object that calculates the volume and surface area of the Prism when supplied the width, height, and length of the Prism. The volume of the Prism should be calculated by multiplying the area of the triangle by the length of the Prism. 

 

Prompt the user for the values of width, height, and length.  Validate these values to make sure they are not negative.  If a negative value is entered, force the user to make another entry.  If all three values are zero, use a default constructor that you create for Prism that takes the default value of width and height from Triangle and assigns a value of 10 to length in the default constructor of Prism. Within the subclass Prism include an overriding method named findArea() that computes the surface area of the prism (the sum of the areas of all five sides).  Obtain the area of the end of the prism from the findArea() method in the Triangle class.  Use this value and the area of each side to compute the total surface area of a Prism.  (ie., surface area = 2 times the area of an end plus the three sides.  You many assume that the ends are isosceles triangles.  How do you find the area of an isosceles triangle?  Look it up!  You will also need to calculate the length of the slanting side so you can use it to find the surface area.  Welcome to Geometry 101!)

 

Print the values of width, height, length, volume, and surface area before asking the user if he/she would like to create another Prism.  Round all output results to 2 decimal places.  Display an appropriate message when the user enters 'n' or 'N'. 

 

public class FLastLab10

{ . . .

}

public class Triangle

{  protected double width, height;     // protected allows the subclass access to these

    public Triangle ()

Oval: Call this from the subclass when zeros are entered    {   // default constructor

DO NOT ADD ANY MORE METHODS TO THE TRIANGLE CLASS!

 
         width = 7;

         height = 8;

    }

    public Triangle (double w, double h)

Oval: Call this from the subclass when non-zero entries are entered    {   width = w;

         height = h;

    }

    public double findArea()

    {   return (width * height * 0.5);

    }

}                                                                                                                                   

(continued)

 

public class Prism : Triangle

{ // code a default constructor, another constructor, a method for the volume,

   // and a method named findArea() for the surface area;  Also code a method
   // named displayData within the Prism class to print the desired output.

}

 

Submit your program (FLastLab10.cs) before the deadline.  Your output should appear similar to that shown below:

 

Sample output:

 

Lab 10 has started for Your Name

Oval: zeros should still produce default results
 


Enter the value for width   5.1

Enter the value for height  9.4

Enter the value for Length  6.7

 

The width of the Prism is  xx.x

The height of the Prism is xx.x

The length of the Prism is xx.x

The Volume is xxx.xx cubic units

The Surface area is xxx.xx square units

 

Would you like to enter more data (Y/N) Y

 

Enter the value for width  -5.6

The width many not be Negative! Please re-enter!

Enter the value for width   8.1

 

Enter the value for height -5.6

The height many not be Negative! Please re-enter!

Enter the value for height  6.8

 

. . .

 

 

Lab 10 has successfully terminated for Yourname

 

 

 

 

 

 




using System;
using SC = System.Console;
public class JKingLab10
{ // Main Method
    public static void Main()
    {
        String continueStr = "Y";
        double width, height, length;

        SC.WriteLine("Lab 10 has started for Your Name");

        while (continueStr.ToUpper() != "N")
        {
            SC.Write("\nEnter the value for width ");
            width = Convert.ToDouble(SC.ReadLine());
            while (width < 0)
            {
                SC.WriteLine("The width may not be Negative! Please re-enter");
                SC.Write("Enter the value for width ");
                width = Convert.ToDouble(SC.ReadLine());
            }

            SC.Write("\nEnter the value for hieght ");
            height = Convert.ToDouble(SC.ReadLine());
            while (height < 0)
            {
                SC.WriteLine("The hieght may not be Negative! Please re-enter");
                SC.Write("Enter the value for hieght ");
                width = 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 width ");
                width = Convert.ToDouble(SC.ReadLine());
            }

            if (length == 0 && width == 0 && height == 0)
            {
                Prism myPrism = new Prism(width, height, length);
                myPrism.displayData();
            }
            else (length >= 0 && width >= 0 && height >= 0);
            {
                Prism myPrism = new Prism(width, height, length);
                myPrism.displayData();
            }

            SC.Write("\nWould you like to enter more data (Y/N) ");
            continueStr = SC.ReadLine();
        }
        SC.WriteLine("\nLab 10 has successfully terminated for Your Name");
    } // end of Main method
} // end of Main class


public class Triangle
{
    protected double width, height;

    public Triangle()
    { // default constructor
        width = 7;
        height = 8;
    }
    public Triangle(double w, double h)
    { // non-default constructor
        width = 7;
        height = 5;
    }
    public double findArea()
    {
        return (width * height * 0.5);
    }
}

public class Prism : Triangle
{
    private double length;
    private double slantSide;

    public Prism()
        : base()
    {
        length = 10; // default constructor
    }

    public Prism(double w, double h, double l)
        : base(w, h)
    {
        length = l; // non-default constructor
    }

    new public double findArea()
    {
        double triangleArea;
        triangleArea = base.findArea();
        slantSide = Math.Sqrt((((5.1 / 2) * (5.1 / 2)) + (9.2 * 9.2)));
        return Math.Round((2 * triangleArea) + (5.1 * 6.7) + (2 * (slantSide * 6.7)), 2);
    }
    
    public double volume()
    {
        return Math.findArea() * length ;
    }

    public void displayData()
    {
        SC.WriteLine("\nThe width of the Prism is {0,5}",
        Math.Round(width, 2).ToString("F2"));

        SC.WriteLine("\nThe height of the Prism is {0,9}",
        Math.Round(height, 2).ToString("F2"));

        SC.WriteLine("\nThe length of the Prism is {0,7}",
        Math.Round(length, 2).ToString("F2"));

    }
}

Answers (1)