QuestionQuestion

Transcribed TextTranscribed Text

Programming Assignment 6 Instructions: This programming assignment will teach you about classes. 6. Create a class named rational which will represent a new data type corresponding to rational numbers. A rational number is a fraction where the numerator and denominator are both integers. Your constructor ( init ) should create the following variables: - n and d corresponding to the numerator and denominator of the rational numbers. Default values are n = 0, and d = 1. And the following functions (or methods): - num which returns the numerator of the fraction - denom which returns the denominator of the fraction - convert which returns a double representing the fraction (i.e. 3/4 becomes 0.75) - gcf which returns the greatest common factor of n and d. (Be careful not to change the values of n and d). - reduce which reduces the fraction to its lowest terms (using the gcd function above). This function changes the value of n and d appropriately. Define the following methods: - str to display the rational number as a fraction to the monitor. A fraction like a/1 should be displayed as a, and a fraction like 0/b should be displayed as 0. - add , sub , mul , truediv should be overloaded to perform the appropriate operations (+, −, ∗, /) on rational numbers. When calculating the new rational number, reduced it to lowest terms (6/8 should become 3/4). - eq , ne , lt , le , gt , ge , should be overloaded to perform the appropriate equalities/unequalities/inequalities between rational numbers (==, ! =, <, <=, >, >=). 7. Use your class to calculate rational approximations for π using the series π = 4 1 − 4 3 + 4 5 − 4 7 + . . . . Your program should ask the user how many terms to include in the approximation. For full credit, use the class above as much as possible (to do all the calculations).

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

{
"cells": [
{
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Homework 5"
   ]
},
{
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "1) Create a class for a 2x2 matrix. Give the init function and create functions to return each entry of the 2x2 matrix."
   ]
},
{
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Matrix:\n",
    "    \n",
    "    def __init__(self, topLeft, topRight, bottomLeft, bottomRight):\n",
    "       self.topLeft = topLeft\n",
    "       self.topRight = topRight\n",
    "       self.bottomLeft = bottomLeft\n",
    "       self.bottomRight = bottomRight\n",
    "    \n",
    "    def getTopLeft(self):\n",
    "       return self.topLeft\n",
    "    \n",
    "    def getTopRight(self):\n",
    "       return self.topRight\n",
    "    \n",
    "    def getBottomLeft(self):\n",
    "       return self.bottomLeft\n",
    "    \n",
    "    def getBottomRight(self):\n",
    "       return self.bottomRight"
   ]
},
{
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2) Create functions to give the trace and determinant of this matrix."
   ]
},
{
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Matrix:\n",
    "    \n",
    "    def __init__(self, topLeft, topRight, bottomLeft, bottomRight):\n",
    "       self.topLeft = topLeft\n",
    "       self.topRight = topRight\n",
    "       self.bottomLeft = bottomLeft\n",
    "       self.bottomRight = bottomRight\n",
    "    \n",
    "    def getTopLeft(self):\n",
    "       return self.topLeft\n",
    "    \n",
    "    def getTopRight(self):\n",
    "       return self.topRight\n",
    "    \n",...

By purchasing this solution you'll be able to access the following files:
Solution.pdf and Solution.ipynb.

$48.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Python Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats