Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Rational Class - Computer Science I - Lecture Notes | MAT 2170, Study notes of Computer Science

Material Type: Notes; Class: Computer Science I; Subject: Mathematics; University: Eastern Illinois University; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-0yu
koofers-user-0yu 🇺🇸

10 documents

1 / 2

Toggle sidebar

Related documents


Partial preview of the text

Download Rational Class - Computer Science I - Lecture Notes | MAT 2170 and more Study notes Computer Science in PDF only on Docsity! MAT 2170: Rational Class and program to calculate π The Rational Class In mathematics, the rational numbers are defined as the set Q = {ab | a, b ∈ Integers ∧ b 6= 0}. To develop a Java class to implement rational numbers, including the operations on them, we must first determine what invariants (rules), data members and member methods are necessary. Invariants. 1. The denominator for the rational number is always positive. If the denominator is negative, the constructor transfers the sign to the numerator by negating that value. 2. The fraction represented by the numerator and denominator is always reduced to lowest terms by dividing both parts of the fraction by the greatest common divisor. This design decision ensures that rational numbers are always displayed in their simplest form. 3. Rational class should be immutable — once a Rational object is constructed, the client should not be allowed to modify it. Data Members. A Rational class object will need to store a numerator and a denominator, both of type int. We shall need to guarantee they are always “in lowest (or reduced) terms” — in other words, that the numerator and denominator have no factors (other than 1 or −1) in common. Access to these data members should be restricted to the class itself — the client should not be able to manipulate the numerator or denominator directly. Member Methods. When designing a class, it is necessary to consider what constructors, inspec- tors, mutators, and facilitators are required or might be useful. 1. Constructors: (a) Rational() — no parameters (aka default constructor), set to 01 (b) Rational(n) — a single parameter, set to n1 (c) Rational(n, d) — two parameters, set to nd (d) copy constructor? 2. inspectors: (a) getNumerator() — returns the value of the numerator (b) getDenominator() — returns the value of the denominator 3. mutators: — are not allowed for this class. 4. facilitators will need to implement the usual arithmetic operations — add, subtract, multiply, and divide — the results of which are shown in the table below: Rules for rational arithmetic Addition Multiplication a b + c d = ad+bc bd a b ∗ c d = ac bd Subtraction Division a b − c d = ad−bc bd a b ÷ c d = ad bc
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved