C++ Programming Add 4 new Functions to this Project: 1. calcAreaCircle() 2. calc
ID: 3678512 • Letter: C
Question
C++ Programming
Add 4 new Functions to this Project:
1. calcAreaCircle()
2. calcAreaSquare()
3. calAreaRectangle()
4.. calAreaTriangle()
Each of these functions will process an Area calculation:
(i.e. for a Circle, your function would do the following 4 steps:
1. prompt the User for to enter a radius.
2. Read in the radius,
3. calculate the area by using the formula a=pi*Radius squared.
4. Then print out the area of the circle. )
Lastly, modify the switch statement in the main function. Your main function will no longer do all the work to process the area calculations. Your main() function will call the 4 new functions that you just built. If the uses enters a ‘C’, call the function “calcAreaCircle()” to process the calculation of the area of a circle. Do the same for the other 3 figures.
__________________________________________________________________
#include
using namespace std;
int main()
{
char ch;
float radius = 0.0, side = 0.0, width =0.0, length = 0.0, base =0.0, height = 0.0;
float pi = 3.14159;
double area = 0.0;
while(ch !='X' || ch !='x')
{
cout << " ************* MENU ***************" << endl;
cout << " R. Rectangle C. Circle T. Trianle S. Square X. Exit Enter a letter to find area ";
cin>>ch;
switch(ch)
{
case 'C':
cout<<"Enter Radius: "< cin>>radius;
area = pi * radius * radius;
cout<<"Circle Area is : "< area = 0.0;
break;
case 'c':
cout<<"Enter Radius: "< cin>>radius;
area = pi * radius * radius;
cout<<"Circle Area is : "< area = 0.0;
break;
case 'S':
cout<<"Enter side: "< cin>>side;
area = side * side ;
cout<<"Square Area is : "< area = 0.0;
break;
case 's':
cout<<"Enter side: "< cin>>side;
area = side * side;
cout<<"Square Area is : "< area = 0.0;
break;
case 'R':
cout<<"Enter Width : "< cin>>width;
cout<<"Enter length : "< cin>>length;
area = width * length;
cout<<"Rectangle Area is : "< area = 0.0;
break;
case 'r':
cout<<"Enter Width : "< cin>>width;
cout<<"Enter length : "< cin>>length;
area = width * length;
cout<<"Rectangle Area is : "< area = 0.0;
break;
case 'T':
cout<<"Enter Base : "< cin>>base;
cout<<"Enter Height : "< cin>>height;
area = (base * height)/2;
cout<<"Triangle Area is : "< area = 0.0;
break;
case 't':
cout<<"Enter Base : "< cin>>base;
cout<<"Enter Height : "< cin>>height;
area = (base * height)/2;
cout<<"Triangle Area is : "< area = 0.0;
break;
case 'X':
exit(0);
case 'x':
exit(0);
}
}
return 0;
}
Explanation / Answer
import java.util.Scanner;
abstract class calcArea {
abstract void findTriangle(double b, double h);
abstract void findRectangle(double l, double b);
abstract void findSquare(double s);
abstract void findCircle(double r);
}
class findArea extends calcArea {
void findTriangle(double b, double h)
{
double area = (b*h)/2;
System.out.println("Area of Triangle: "+area);
}
void findRectangle(double l, double b)
{
double area = l*b;
System.out.println("Area of Rectangle: "+area);
}
void findSquare(double s)
{
double area = s*s;
System.out.println("Area of Square: "+area);
}
void findCircle(double r)
{
double area = 3.14*r*r;
System.out.println("Area of Circle: "+area);
}
}
class area {
public static void main(String args[])
{
double l, b, h, r, s;
findArea area = new findArea();
Scanner get = new Scanner(System.in);
System.out.print(" Enter Base & Vertical Height of Triangle: ");
b = get.nextDouble();
h = get.nextDouble();
area.findTriangle(b, h);
System.out.print(" Enter Length & Breadth of Rectangle: ");
l = get.nextDouble();
b = get.nextDouble();
area.findRectangle(l, b);
System.out.print(" Enter Side of a Square: ");
s = get.nextDouble();
area.findSquare(s);
System.out.print(" Enter Radius of Circle: ");
r = get.nextDouble();
area.findCircle(r);
}
}
import java.util.Scanner;
abstract class calcArea {
abstract void findTriangle(double b, double h);
abstract void findRectangle(double l, double b);
abstract void findSquare(double s);
abstract void findCircle(double r);
}
class findArea extends calcArea {
void findTriangle(double b, double h)
{
double area = (b*h)/2;
System.out.println("Area of Triangle: "+area);
}
void findRectangle(double l, double b)
{
double area = l*b;
System.out.println("Area of Rectangle: "+area);
}
void findSquare(double s)
{
double area = s*s;
System.out.println("Area of Square: "+area);
}
void findCircle(double r)
{
double area = 3.14*r*r;
System.out.println("Area of Circle: "+area);
}
}
class area {
public static void main(String args[])
{
double l, b, h, r, s;
findArea area = new findArea();
Scanner get = new Scanner(System.in);
System.out.print(" Enter Base & Vertical Height of Triangle: ");
b = get.nextDouble();
h = get.nextDouble();
area.findTriangle(b, h);
System.out.print(" Enter Length & Breadth of Rectangle: ");
l = get.nextDouble();
b = get.nextDouble();
area.findRectangle(l, b);
System.out.print(" Enter Side of a Square: ");
s = get.nextDouble();
area.findSquare(s);
System.out.print(" Enter Radius of Circle: ");
r = get.nextDouble();
area.findCircle(r);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.