write a program on 3D Shape that consists of length, width and height. Then implement two classes (Square and Rectangle) that 'inherit' length, width and height from Shape. Then write a main program that asks the user to enter the three parameters: length, width and height. Then have your program compute the area and volume of each shape.
The Answer to the Question
is below this banner.
Can't find a solution anywhere?
NEED A FAST ANSWER TO ANY QUESTION OR ASSIGNMENT?
Get the Answers Now!You will get a detailed answer to your question or assignment in the shortest time possible.
Here's the Solution to this Question
Solution in C# Programming Language
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ShapeApplication
{
//------------------------------
public class Square //-
{
private double length;
private double height;
private double width;
public void setLength (double l)
{
length = l;
}
public void setHeight (double h)
{
height = h;
}
public void setWidth (double w)
{
width = w;
}
public double getSquareVolume()
{
return width * height * length;
}
public double getSquareArea()
{
return 2 * ((width * width) + (height * height) + (length * length));
}
}
///--------------------------------------------------------------------------------------
public class Rectangle //--
{
private double length1;
private double height1;
private double width1;
public void setLength(double l)
{
length1 = l;
}
public void setHeight(double h)
{
height1 = h;
}
public void setWidth(double w)
{
width1 = w;
}
public double getRectangleVolume()
{
return width1 * height1 * length1;
}
public double getRectangleArea()
{
return 2 * ((width1 * length1) + (height1 * length1) + (height1 * width1));
}
}
///--------------------------------------------------------------------------------------------------------
public class Shape //-
{
public static void Main() //-
{
Square Square1 = new Square();
Rectangle Rectangle1 = new Rectangle();
double volume;
double area;
Console.WriteLine("enter a side of the 3D square");
double length = double.Parse(Console.ReadLine());
double height = length;
//is the 3d square like a square box or a cube?
///if this is supposed to be a straight up cube cmnt the next two lines, and uncomment the third
Console.WriteLine("now enter the width (will be the same if it's a cube)"); // cmnt out for cube
double width = double.Parse(Console.ReadLine()); //cmnt out for cube
// double width = length; //uncmnt for cube
Square1.setLength(length);
Square1.setHeight(height);
Square1.setWidth(width);
Console.WriteLine("enter the rectangular prism's length");
double length1 = double.Parse(Console.ReadLine());
Console.WriteLine("enter the rectangular prism's height");
double height1 = double.Parse(Console.ReadLine());
Console.WriteLine("enter the rectangular prism's width");
double width1 = double.Parse(Console.ReadLine());
Rectangle1.setLength(length1);
Rectangle1.setHeight(height1);
Rectangle1.setWidth(width1);
volume = Square1.getSquareVolume();
Console.WriteLine("volume of 3D Square: {0:#.##}", volume);
area = Square1.getSquareArea();
Console.WriteLine("area of 3D Square: {0:#.##}", area);
volume = Recatangle1.getRectangleVolume();
Console.WriteLine("volume of 3D Rectangle: {0:#.##}", volume);
area = Rectangle1.getRectangleArea();
Console.WriteLine("area of 3D Rectangle: {0:#.##}", area);
}
}
}