Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask C/C++ Expert


Home >> C/C++

Write a program that will represent an axis-aligned right triangle in the x-y plane as a Class. A right triangle has a right angle (90-degree angle) and two sides adjacent to the right angle, called legs. See http://en.wikipedia.org/wiki/Right_triangle for a complete definition. In addition, an axis-aligned right triangle has one leg parallel with the x-axis and the other leg parallel with the y-axis. The location of the triangle is the vertex that connects the two legs, called the bottom left vertex. This vertex is located at the right angle and represented with the Class Point. The length
CSE1222 - Spring 2014
CSE1222 Lab 13 2
of the triangle is the length of the leg parallel with the x-axis. The height of the triangle is the
length of the leg parallel with the y-axis.
The procedure main() (see the code template triangles_template.cpp) has been written
for you (DO NOT change anything in the main() procedure) as well as the member attributes of
the Class Triangle. You will write code for the member functions of the Class Triangle and also
helper functions as indicated by the comments /* INSERT CODE HERE */.
Run triangles_solution.exe to see an example of the program.
A sample run of the program would look like this (bold indicates what the user will enter):
> triangles.exe
Enter first file name: first.txt
Enter bottom left x coordinate: 0
Enter bottom left y coordinate: 0
Enter length: 5
Enter height: 10
Enter second file name: second.txt
Enter scale factor in x direction: 2
Enter scale factor in y direction: 1.5
After running, the output file called first.txt contains:
----------------------------------------
Lower Left Vertex (0, 0)
Top Left Vertex (0, 10)
Bottom Right Vertex (5, 0)
Dimensions (5, 10)
Hypotenuse = 11.1803
Perimeter = 26.1803
----------------------------------------
and the output file called second.txt contains:
----------------------------------------
Lower Left Vertex (0, 0)
Top Left Vertex (0, 15)
Bottom Right Vertex (10, 0)
Dimensions (10, 15)
Hypotenuse = 18.0278
Perimeter = 43.0278
----------------------------------------
CSE1222 - Spring 2014
CSE1222 Lab 13 3
To view the contents of the file, type "cat first.txt" and "cat second.txt". Your program must work for any file names and any triangle attribute values entered by the user, i.e. not just the user input given in the example above.
1. The function prototypes are provided for you and must NOT be changed in any way.
2. The procedure main() is provided for you and must NOT be changed in any way.
3. Understand the class definitions for Point and Triangle.
4. Understand the algorithm in the procedure main().
5. Do NOT add more functions/procedures to the solution.
6. Each function should have a comment explaining what it does.
7. Each function parameter should have a comment explaining the parameter.
8. Write code by replacing every place /* INSERT CODE HERE */ appears with your solution.
9. Implement the member functions for the class Triangle. Use the appropriate class attributes of the class Triangle.
a. The location of the bottom left vertex is stored in the member attribute blPoint.
b. The top left vertex can be computed from blPoint and the height.
c. The bottom right vertex can be computed from blPoint and the length.
10. You may assume that the user will enter positive values for the length and height.
11. Implement the get and set member functions of the class Triangle.
12. Implement the member function hypotenuse() of the class Triangle that computes the hypotenuse of a triangle object. Use the Pythagorean Theorem to compute the length of the side of the triangle opposite to the right angle. Use the appropriate class attribute(s) of the class Triangle.
13. Implement the member function perimeter() of the class Triangle that computes the perimeter of the triangle object. Sum all three sides of the triangle. Use the appropriate class attribute(s) of the class Triangle and the member function hypotenuse().
14. Implement the member function scaleLength() of the class Triangle that computes the new value of the length as the current length weighted by the scale factor in the x direction. For example, if the current length is 2 and the scale factor is 3, the new length is 6. If the current length is 2 and the scale factor is 0.5, the new length is 1. You may assume that the scale factor will be positive. Update the length class attribute of the class Triangle.
15. Implement the member function scaleHeight() of the class Triangle that computes the new value of the height as the current height weighted by the scale factor in the y direction. You may assume that the scale factor will be positive. Update the height class attribute of the class Triangle.
16. Implement the member function write_out() of the class Triangle that writes all information about the triangle to an output file given a filename.
? Use the appropriate class attributes and member functions of the class Triangle.
? Use the file name to open the file for output. Review and use the program writeFile3.cpp discussed in class as a template for your program. (See the class slides or download the program from Carmen). Make sure to pass a C style string, not the C++ string, to the open routine. Include the C++ command "#include " to use C++ file streams. Be sure to use the type "ofstream", not "ifstream", for your
CSE1222 - Spring 2014
CSE1222 Lab 13 4
output file streams. Check if the file was successfully opened for output. If not, print an error message and exit.
? Include the C++ command "#include " to use the C++ exit command.
? CLOSE the file stream. (Note: You will lose points if you forget to close the output file stream, even though your program runs correctly).
? Important note, do not write redundant code.
17. Implement the function read_filename() that prompts, reads, and returns a file name as a string. Include the C++ command "#include " to use C++ strings.
18. Implement the function read_triangle() that prompts and reads the location of the bottom left vertex, length and height. These values are assigned into the class Triangle object passed into the function using set member functions.
19. TEST YOUR CODE THOROUGHLY. For example, pay close attention to the format of the output including empty lines. Your program must not have a run-time error.
20. Be sure to add the header comments "File", "Created by", "Creation Date" and "Synopsis" at the top of the file. Each synopsis should contain a brief description of what the program does.
21. Be sure that there is a comment documenting each variable.
22. Be sure that your if statements, for and while loops and blocks are properly indented.

23.Check your output against the output from the solution executables provided

#include
#include

using namespace std;

class Point
{
private:
double px;
double py;

public:
void setX(const double x);
void setY(const double y);
double getX() const;
double getY() const;
};

class Triangle
{
private:
Point blPoint;
double length, height;

public:
// member functions
void setBottomLeftX(const double x);
void setBottomLeftY(const double y);
void setLength(const double inLength);
void setHeight(const double inHeight);

Point getBottomLeft() const;
Point getBottomRight() const;
Point getTopLeft() const;
double getLength() const;
double getHeight() const;

double perimeter() const;
double hypotenuse() const;
void scaleLength(const double sx);
void scaleHeight(const double sy);
void write_out(const string fname) const;
};

// FUNCTION PROTOTYPES GO HERE:
string read_filename(const string prompt);
void read_triangle(Triangle & tri);

int main()
{
// Define local variables
string fname;   // Output stream to file
Triangle tri;
double sx, sy;

// Prompt the user for the name of the first file to write
fname = read_filename("Enter first file name: ");
cout << endl;

//Prompt the user for triangle information and fill Class Triangle object, tri,
//with this information
read_triangle(tri);

// Write the triangle information to the first output file
tri.write_out(fname);
cout << endl;

// Prompt the user for the name of the second file to write and open it
fname = read_filename("Enter second file name: ");
cout << endl;

// Prompt and read scale factors to change length and height
cout << "Enter scale factor in x direction: ";
cin >> sx;

cout << "Enter scale factor in y direction: ";
cin >> sy;

// Apply scale factors
tri.scaleLength(sx);
tri.scaleHeight(sy);
cout << endl;

// Write the triangle information to the second output file
tri.write_out(fname);

return 0;
}
   
// FUNCTION DEFINITIONS GO HERE:
    
// CLASS MEMBER FUNCTION DEFINITINOS GO HERE:

void Point::setX(const double x)
{
/* INSERT YOUR CODE */
}

void Point::setY(const double y)
{
/* INSERT YOUR CODE */
}

double Point::getX() const
{
/* INSERT YOUR CODE */
}

double Point::getY() const
{
/* INSERT YOUR CODE */
}

void Triangle::setBottomLeftX(const double x)
{
/* INSERT YOUR CODE */
}

void Triangle::setBottomLeftY(const double y)
{
/* INSERT YOUR CODE */
}

void Triangle::setLength(const double inLength)
{
/* INSERT YOUR CODE */
}

void Triangle::setHeight(const double inHeight)
{
/* INSERT YOUR CODE */
}

Point Triangle::getBottomLeft() const
{
/* INSERT YOUR CODE */
}

Point Triangle::getBottomRight() const
{
/* INSERT YOUR CODE */
}

Point Triangle::getTopLeft() const
{
/* INSERT YOUR CODE */
}

double Triangle::getLength() const
{
/* INSERT YOUR CODE */
}

double Triangle::getHeight() const
{
/* INSERT YOUR CODE */
}

double Triangle::hypotenuse() const
{
/* INSERT YOUR CODE */
}

double Triangle::perimeter() const
{
/* INSERT YOUR CODE */
}

void Triangle::scaleLength(const double scalefact)
{
/* INSERT YOUR CODE */
}

void Triangle::scaleHeight(const double scalefact)
{
/* INSERT YOUR CODE */
}

void Triangle::write_out(const string fname) const
{
/* INSERT YOUR CODE */
}

string read_filename(const string prompt)
{
/* INSERT YOUR CODE */
}

void read_triangle(Triangle & tri)
{
/* INSERT YOUR CODE */
}

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M9455602

Have any Question?


Related Questions in C/C++

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As