IN C++
You are to write a program which will get the following variables from the user.
Length, Width, Height, Radius, Base.
You should also have variables for Pi as 3.14 and choice1 and choice2.
Your program will first ask the following:
Press 1 to calculate Area
Press 2 to calculate Perimeter
The choice will be saved in choice1.
You should then ask another choice:
Press 1 for Rectangle
Press 2 for Triangle
Press 3 for Circle
You have all the data required to calculate these except the Perimeter of the triangle. Only in this case will you ask the user to input lengths of side1, side2 and side3.
**You can do the shapes first and then the area/perimeter if you prefer**
Display the data neatly to the user.
Giving the code assuming that we have to input (length, width, height, base and radius) all at the same time. As it is not clearly mentioned in the question. You can change this thing easily. Or ask in comment for any change regarding this
Used switch case to manage first choice so that will make code more clean and understandable.
Below is the complete code:
#include <iostream>
using namespace std;
int main() {
float length, width, height, radius, base;
float pi = 3.14;
int choice1, choice2;
cout<<"Enter Length:";
cin>>length;
cout<<"\nEnter Width:";
cin>>width;
cout<<"\nEnter Height:";
cin>>height;
cout<<"\nEnter Radius:";
cin>>radius;
cout<<"\nEnter Base:";
cin>>base;
cout<<"\n1. Press 1 to calculate Area \n2. Press 2 to calculate Perimeter"<<endl;
cout<<"choice1: ";cin >> choice1;
cout<<"\nPress 1 for Rectangle\nPress 2 for Triangle\nPress 3 for Circle"<<endl;
cout<<"choice2: ";cin>>choice2;
switch(choice1){
case 1: {
if(choice2 == 1){
cout << "\nArea of Rectangle is: " << (length*width)<<endl;
}
else if(choice2 == 2){
cout<<"\nArea of Traingle is: " << (0.5*height*base)<<endl;
}
else if(choice2 == 3){
cout << "\nArea of Circle is: " << (pi * radius * radius)<<endl;
}
else{
cout<<"\nChoice other than 1 2 or 3 i.e. Rectangle Triangle or Circle. Quiting !!!";
break;
}
break;
}
case 2:{
if (choice2 == 1){
cout<< "\nPerimeter of Rectangle is: " << (length+width)<<endl;
}
else if (choice2 == 2){
float side1,side2,side3;
cout << "\nEnter side1 of triangle:"; cin>>side1;
cout << "Enter side2 of triangle:"; cin>>side2;
cout << "Enter side3 of triangle:"; cin>>side3;
cout << "\nPerimeter of Triangle is: " << (side1+side2+side3)<<endl;
}
else if(choice2 == 3){
cout<< "\nPerimeter of Circle is: " << (2*pi*radius)<<endl;
}
else{
cout<<"\nInvalid choice2. Quiting !!";
break;
}
break;
}
default: {
cout<<"\nChoice other than 1 or 2 i.e. Area or Perimeter. Quiting !!!"<<endl;
break;
}
}
}
IN C++ You are to write a program which will get the following variables from the...
Write a Java program that prompts the user to enter a number representing a geometric shape. Options are 1 for square, 2 for rectangle, and 3 for right triangle. According to the user input, the program will collect either 1, 2 or 3 values from the user representing the lengths of the shape sides. The user is responsible for ensuring the 3 sides of the triangle represent a right triangle. The program will print the perimeter of the shape. Use...
(The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class contains: - Three float data fields named side1, side2, and side3 to denote the three sides of the triangle. - A constructor that creates a triangle with the specified side1, side2, and side3 with default values 1.0. - The accessor methods for all three data fields. - A method named getArea() that returns the area of this triangle. - A method named getPerimeter() that...
I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...
Purpose: The purpose of this lab is for you to design and implement several classes that use Inheritance. The problem: The program must handle a collection of different 2-dimensional shapes: triangles, rectangles and circles. All shapes have a color (use a Stringl and are either filled or not (use a boolean). All shapes must calculate and return their perimeter, and area. toStrina) for all shapes must implement the standardized formatting for inheritance. You are required to implement each of the...
Design a general class GeometricObject can be used to model all geometric objects. This class contains the properties color and filled and their appropriate get and set methods. Assume that this class also contains toString() methods. The toString() method returns a string representation of the object. Define the Triangle, Circle, and Rectangle classes that extend the GeometricObject class. The Triangle class inherits all accessible data fields and methods from the GeometricObject class.In addition, it has three double data fields named...
Welcome to the Triangles program Enter length 1: 3 Enter length 2: 5 Enter length 3: 5 Enter the base: 5 Enter the height: 8 --------------------- The triangle is Isosceles since it has two equal length sides. Isosceles triangle also has two equal angles The area is: 20 The permimeter is: 13 Continue? (y/n): y Enter length 1: 10 Enter length 2: 10 Enter length 3: 10 Enter the base: 10 Enter the height: 7 --------------------- The triangle is Equilateral...
JAVA Code Requried Copy the file java included below. This program will compile, but, when you run it, it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create this. Below the main method, but in the Geometry class, create a static method called printMenu that has no parameter list and does not return a value. It will...
this is what i have so far but it does not work. please help
thank you
Part 2: Perimeter of a Triangle Write a program named Triangle.java that asks for the lengths of the three sides of a triangle and computes the perimeter if the input is valid.The input is valid if the sum of every pair of two sides is greater than the remaining side. For example, the lengths 3, 4, and 5 define a valid triangle: 3 plus...
Using C++, Write a program that will provide the user a menu from which the user may select to calculate the area of one of four geometric shapes: a circle, a rectangle, a triangle, and a trapezoid. You should use the most appropriate SWITCH block for this program. The user will input all data needed to calculate the area. The program should output all data input by the user, the calculated area, and the geometric shape selected. Run this program...
import math ''' Finish the code below as described. Use the completed class Square as an example. ''' class Square: ''' Each Square has a width and can calculate its area, its perimeter, and return a string representation of itself. ''' def __init__(self, width): ''' (float) -> None Create a new Square with the given width. ''' self.width = width def get_area(self): ''' () -> float Return this square's area. ''' return self.width*self.width def get_perimeter(self): ''' () -> float Return...