Question

Write the C# code and make a screenshot of the output according to tasks given below:...

Write the C# code and make a screenshot of the output according to tasks given below:
Create a class named Prism with protected data fields named height and volume (Use double data type for the fields) [5 points]
Include public properties for each field. The Height and Volume properties should have get and set accessors. [5 points]
Add default constructor which sets the value of Height and Volume properties to 1 and 1 respectively. [5 points]
Add overloading constructor which takes the parameters of _height and _volume. Then assign these values to the corresponding properties. [5 points]
Add public method CalcArea() which returns the value of (Use double data type) [5 points]
Create a derived class named Square with private data fields named area and length. [5 points]
Include public properties for area field. The Area property should have get and set accessors. Set accessor for the Area should be assigned to the method CalcArea().[5 points]
Include public properties for length field. The Length property should have get and set accessors. Set accessor for the Length should be calculated using . [5 points]
In the main method write try and catch pair. Inside the try block declare and instantiate object named p1 from Prism class using default constructor and object initializer. Assign values of 10 and 0 to Volume and Height properties respectively. Inside the catch block catch any exception and display the type of the exception as well as the text “You tried to divide by zero which is impossible”. [10 points]
Write another try and catch pair. Inside the try block declare and instantiate object named s1 from Square derived class using default constructor and display the text “Input value for the volume>>>”, “Input value for the height>>>” and assign input values from the console to the corresponding properties of the object. Inside the catch block catch any exception and display the type of the exception as well as the text “You tried to input string instead of number”. [10points]
Run the program and input any text from console in order to cause conversion exception to be thrown by the program. Take a screenshot of the output you received in online compiler and paste it below. [Bonus 10 points]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*C sharp program that demonstrates the Prism and Square classes
in the below Program.cs .*/

//Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Exercise
{
class Program
{
static void Main(string[] args)
{
//declare variables of double type
double volume;
double height;

//try-catch block
try
{
//Crate a Prism class object
Prism p1=new Prism();
//Set Volume and Height to 10 and 0
p1.Volume=10;
p1.Height =0;
//check if height is 0 then throw exception
if (p1.Height ==0)
throw new DivideByZeroException ();
}
//catch exception
catch (DivideByZeroException exp)
{
Console.WriteLine(exp.Message);
Console.WriteLine ("You tried to divide by zero which is impossible");
}

//try-catch block for Square class
try
{
//Crate a Square class object
Square s1 = new Square();

Console.Write("Input value for the volume>>>");
//Convert the string to double
volume = Convert.ToDouble(Console.ReadLine());
//Set volume
s1.Volume = volume;

Console.Write("Input value for the height>>>");
//Convert the string to double
height = Convert.ToDouble(Console.ReadLine());
//Set Height
s1.Height = height;
}
//catch exception for invalid user input
catch (Exception exp)
{
Console.WriteLine("You tried to input string instead of number");
}
Console.ReadLine();
}
}
}
------------------------------------------------------------------------------------------

//Prism.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Exercise
{
class Prism
{
private double height;
private double volume;
//Default Constructor
public Prism()
{
Height = 1;
Volume = 1;
}
//Constructor
public Prism(double _height, double _volume)
{
Height = _height ;
Volume = _volume;
}
//Height property
public double Height
{
set{ height = value; }
get{ return height ; }
}
//Volume property
public double Volume
{
set { volume = value; }
get { return volume; }
}
//Calculate area
public double CalcArea()
{
double area = 0;
area = volume / height;
return area;
}
} //end of the Prism class
}

------------------------------------------------------------------------------------------
//Prism.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Exercise
{
class Square : Prism
{
private double length;
private double area;
//Default constructor
public Square()
{
length = 0;
area = 0;
}
//Area property
public double Area
{
set { area = CalcArea(); }
get { return area; }
}
//Length property
public double Length
{
set { length = value ; }
get { return length; }
}
/*Return area of the square */
public double CalcArea()
{
return length * length;
}
}
}

-------------------------------------------Sample Output------------------------------------------------------------------

Attempted to divide by zero. You tried to divide by zero which is impossible Input value for the volume>>>abcd You tried to i

Add a comment
Know the answer?
Add Answer to:
Write the C# code and make a screenshot of the output according to tasks given below:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please use C++. Write a class named Circle that has a double data member named radius...

    Please use C++. Write a class named Circle that has a double data member named radius and a static double data member named maxRadius. It should have a default constructor that initializes the radius to 1.0. It should have a constructor that takes a double and uses it to initialize the radius. It should have a method called calcArea that returns the area of the Circle (use 3.14159 for pi). It should have a static set method for the maxRadius....

  • Please write in dr java Here is the driver: import java.util.*; public class SquareDriver { public...

    Please write in dr java Here is the driver: import java.util.*; public class SquareDriver { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String input =""; System.out.println("Welcome to the easy square program"); while(true) { System.out.println("Enter the length of the side of a square or enter QUIT to quit"); try { input = keyboard.nextLine(); if(input.equalsIgnoreCase("quit")) break; int length = Integer.parseInt(input); Square s = new Square(); s.setLength(length); s.draw(); System.out.println("The area is "+s.getArea()); System.out.println("The perimeter is "+s.getPerimeter()); } catch(DimensionException e)...

  • Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default...

    Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default constructor -- that calls the default super class constructor and sets the default height to 0- Overloaded constructor with three parameters (length, width and height) – that calls the two parameterized super class constructor passing in length and width passed and sets the height to passed height.- setDimension method with three parameters (length, width, height) – call the super class setDimension and pass in...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Exercise #1:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to...

  • Please help with a source code for C++ and also need screenshot of output: Step 1:...

    Please help with a source code for C++ and also need screenshot of output: Step 1: Create a Glasses class using a separate header file and implementation file. Add the following attributes. Color (string data type) Prescription (float data type) Create a default constructor that sets default attributes. Color should be set to unknown because it is not given. Prescription should be set to 0.0 because it is not given. Create a parameterized constructor that sets the attributes to the...

  • Type up the GeometricObject class (code given on the back of the paper). Name this file Geometric...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

  • Java - Object Oriented Programming Declare a class named Customer that has two private fields? Write...

    Java - Object Oriented Programming Declare a class named Customer that has two private fields? Write a set method to make sure that if age > 125 years or less than 0 then it is set to 0 (default value) What does it indicate when declaring a class's instance variables with private access modifier? What is the role of a constructor? The data part of by an object's instance variables is known as? Do all methods have to always return...

  • Python only please, thanks in advance. Type up the GeometricObject class (code given on the back...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

  • Please answer this question clearly and correctly! the code should be handwritten if all possible!! ALSO MAKE SURE IT IS WRITTEN IN C++! thank you so much in advance and I will upvote when done right...

    Please answer this question clearly and correctly! the code should be handwritten if all possible!! ALSO MAKE SURE IT IS WRITTEN IN C++! thank you so much in advance and I will upvote when done right! (26 points total) Write an inheritance hierarchy for classes of simple shapes. 1. Create a class Shape. Derive the class ThreeDimensionalShape from the Shape class. Derive the class Sphere from the ThreeDimensionalShape class Use abstract classes to define Shape and ThreeDimensionalShape classes, and then...

  • Please provide the full code...the skeleton is down below: Note: Each file must contain an int...

    Please provide the full code...the skeleton is down below: Note: Each file must contain an int at the beginning, stating the number of records in the file. Afterwards, the number of records in the file will follow. Each record that follows will consist of a last name (String), and a gpa (double). However, to test the error handling of your program, the number of records will not always match the int value. All possible combinations should be tested. 1.) Prompt...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT