CS2 Lab 09 Assignment: Rational and Counter Class Implementation
VerifiedAdded on 2023/03/17
|5
|1688
|89
Homework Assignment
AI Summary
This document presents the solutions for Computer Science 2 Lab 09, focusing on the implementation of two classes: Rational and Counter. The assignment requires students to define data fields, constraints, constructors, and methods for each class. For the Rational class, the solution addresses responsibilities such as computing negation, reciprocal, equality, sum, difference, product, and quotient, along with a printable representation. The Counter class implementation involves incrementing, decrementing, and managing minimum and maximum values. The solution includes detailed pre-conditions, post-conditions, and test cases for each method, providing a comprehensive understanding of object-oriented programming principles. The student's responses fill in the required fields, providing detailed solutions to the assignment brief's questions about data fields, constraints, constructors, and methods for both the Rational and Counter classes, including the necessary preconditions, post-conditions, and test cases.

Computer Science 2 Lab # 09
Dr. Hanh Pham
Student Last Name: SMITH Student First Name: JAMES
CS2 Section # 03
Due: Problem A by the end of the lab and Problems B by the end of Saturday of the same week.
TOPIC: OBJECTS
Problem A:
Please fill-in the field following this symbol with your own words and in BLUE font color
Pre-Lab Visualization
Rational
Here is a list of responsibilities for the rational class:
a. Know the value of the denominator.
b. Know the value of the numerator.
c. Be able to compute the negation of a rational number.
d. Be able to compute the reciprocal of a rational number.
e. Be able to compare two rational numbers for equality.
f. Be able to compute the sum of two rational numbers.
g. Be able to compute the difference of two rational numbers.
h. Be able to compute the result of multiplying two rational numbers.
i. Be able to compute the result of dividing two rational numbers.
j. Be able to compute a printable representation of the rational number.
1)What data fields will the Rational class need to implement these responsibilities?
Data fields of the Rational class includes
numerator, n
denominator, d
2)Are there any constraints on the values of the data fields?
Some of the constrains on the values of the data field includes
Denominator, d must be non-zero
3)Here is a list of constructors and methods that will be used to implement the responsibilities. Fill in the missing pre-
conditions, post-conditions, and test cases.
Rational()
Pre-condition: none.
Post-condition: The rational number 1 has been constructed.
Test cases: none.
Rational(n, d)
Pre-condition: The denominator d is non-zero.
Post-condition: The rational number n/d has been constructed and is in normal form.
Test cases:
n = 2, d = 4; result is 1/2
n = 0, d = 7; result is 0/1
Dr. Hanh Pham
Student Last Name: SMITH Student First Name: JAMES
CS2 Section # 03
Due: Problem A by the end of the lab and Problems B by the end of Saturday of the same week.
TOPIC: OBJECTS
Problem A:
Please fill-in the field following this symbol with your own words and in BLUE font color
Pre-Lab Visualization
Rational
Here is a list of responsibilities for the rational class:
a. Know the value of the denominator.
b. Know the value of the numerator.
c. Be able to compute the negation of a rational number.
d. Be able to compute the reciprocal of a rational number.
e. Be able to compare two rational numbers for equality.
f. Be able to compute the sum of two rational numbers.
g. Be able to compute the difference of two rational numbers.
h. Be able to compute the result of multiplying two rational numbers.
i. Be able to compute the result of dividing two rational numbers.
j. Be able to compute a printable representation of the rational number.
1)What data fields will the Rational class need to implement these responsibilities?
Data fields of the Rational class includes
numerator, n
denominator, d
2)Are there any constraints on the values of the data fields?
Some of the constrains on the values of the data field includes
Denominator, d must be non-zero
3)Here is a list of constructors and methods that will be used to implement the responsibilities. Fill in the missing pre-
conditions, post-conditions, and test cases.
Rational()
Pre-condition: none.
Post-condition: The rational number 1 has been constructed.
Test cases: none.
Rational(n, d)
Pre-condition: The denominator d is non-zero.
Post-condition: The rational number n/d has been constructed and is in normal form.
Test cases:
n = 2, d = 4; result is 1/2
n = 0, d = 7; result is 0/1
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

n = 12, d =–30; result is –2/5
n = 4, d = 0; result is Exception
int getNumerator()
Pre-condition: The rational n/d is in a valid state.
Post-condition: The value n is returned.
Test cases:
n/d is 1/2; result is 1
n/d is 0/1; result is 0
n/d is –2/5; result is –2
int getDenominator()
Pre-condition: The rational n/d is in a valid state.
Post-condition: The value d is returned.
Test cases:
n/d is 1/2; result is 2
n/d is 0/1; result is 1
n/d is –2/5; result is 5
4)
Rational negate()
Pre-condition: The rational n/d is in a valid state.
Post-condition: The rational number –n/d has been returned.
Test cases:
n/d is 1/2; result is -1/2
n/d is 0/1; result is 0/1
n/d is –2/5; result is 2/5
5)
Rational reciprocal()
Pre-condition: The rational n/d is in a valid state.
The numerator n is non-zero
Post-condition: The rational number d/n has been returned
Test cases:
n/d is 1/2; result is 2/1
n/d is –2/5; result is -5/2
n/d is 0/1; result is Exception
6)
boolean equals(Object other)
Pre-condition: The rational n/d is in a valid state and other is the valid rational x/y.
Post-condition: True is returned if n==x and d==y
False is returned otherwise
Test cases:
n/d is 1/2; x/y is 1/2 result is true
n/d is –2/5; x/y is 2/5 result is false
n/d is 2/3; x/y is 4/5 result is false
n = 4, d = 0; result is Exception
int getNumerator()
Pre-condition: The rational n/d is in a valid state.
Post-condition: The value n is returned.
Test cases:
n/d is 1/2; result is 1
n/d is 0/1; result is 0
n/d is –2/5; result is –2
int getDenominator()
Pre-condition: The rational n/d is in a valid state.
Post-condition: The value d is returned.
Test cases:
n/d is 1/2; result is 2
n/d is 0/1; result is 1
n/d is –2/5; result is 5
4)
Rational negate()
Pre-condition: The rational n/d is in a valid state.
Post-condition: The rational number –n/d has been returned.
Test cases:
n/d is 1/2; result is -1/2
n/d is 0/1; result is 0/1
n/d is –2/5; result is 2/5
5)
Rational reciprocal()
Pre-condition: The rational n/d is in a valid state.
The numerator n is non-zero
Post-condition: The rational number d/n has been returned
Test cases:
n/d is 1/2; result is 2/1
n/d is –2/5; result is -5/2
n/d is 0/1; result is Exception
6)
boolean equals(Object other)
Pre-condition: The rational n/d is in a valid state and other is the valid rational x/y.
Post-condition: True is returned if n==x and d==y
False is returned otherwise
Test cases:
n/d is 1/2; x/y is 1/2 result is true
n/d is –2/5; x/y is 2/5 result is false
n/d is 2/3; x/y is 4/5 result is false

7)
Rational add(Rational other)
Pre-condition: The rational n/d is in a valid state and other is the valid rational x/y.
Post-condition: The rational number (ny+xd)/dy has been returned.
Test cases:
n/d is 1/2, x/y is 1/2; result is 1/1
n/d is 1/2' x/y is 1/6; result is 2/3
n/d is 3/4, x/y is 5/6; result is 19/12
n/d is 1/3, x/y is –2/3; result is –1/3
8)
Rational subtract(Rational other)
Pre-condition: The rational n/d is in a valid state and other is the valid rational x/y.
Post-condition: The rational number (ny-xd)/dy has been returned.
Test cases:
n/d is 1/2, x/y is 1/2; result is 0/1
n/d is 1/2, x/y is 1/6; result is 1/3
n/d is 3/4, x/y is 5/6; result is -1/12
n/d is 1/3, x/y is –2/3; result is 1/1
9)
Rational multiply(Rational other)
Pre-condition: The rational n/d is in a valid state and other is the valid rational x/y.
Post-condition: The rational number nx/dy has been returned.
Test cases:
n/d is 1/2, x/y is 1/2; result is 1/4
n/d is 1/2, x/y is 1/6; result is 1/12
n/d is 3/4, x/y is 5/6; result is 5/8
n/d is 1/3, x/y is –2/3; result is -2/9
n/d is 0/3, x/y is 2/4; result is 0/1
10)
Rational divide(Rational other)
Pre-condition: The rational n/d is in a valid state and other is the valid rational x/y.
Post-condition: The rational number ny/dx has been returned.
Test cases:
n/d is 1/2, x/y is 1/2; result is 1/1
n/d is 1/2, x/y is 1/6; result is 3/1
n/d is 3/4, x/y is 5/6; result is 9/10
n/d is 1/3, x/y is –2/3; result is -1/2
n/d is 0/3, x/y is 2/4; result is 0/1
n/d is 2/4, x/y is 0/4; result is Exception
Rational add(Rational other)
Pre-condition: The rational n/d is in a valid state and other is the valid rational x/y.
Post-condition: The rational number (ny+xd)/dy has been returned.
Test cases:
n/d is 1/2, x/y is 1/2; result is 1/1
n/d is 1/2' x/y is 1/6; result is 2/3
n/d is 3/4, x/y is 5/6; result is 19/12
n/d is 1/3, x/y is –2/3; result is –1/3
8)
Rational subtract(Rational other)
Pre-condition: The rational n/d is in a valid state and other is the valid rational x/y.
Post-condition: The rational number (ny-xd)/dy has been returned.
Test cases:
n/d is 1/2, x/y is 1/2; result is 0/1
n/d is 1/2, x/y is 1/6; result is 1/3
n/d is 3/4, x/y is 5/6; result is -1/12
n/d is 1/3, x/y is –2/3; result is 1/1
9)
Rational multiply(Rational other)
Pre-condition: The rational n/d is in a valid state and other is the valid rational x/y.
Post-condition: The rational number nx/dy has been returned.
Test cases:
n/d is 1/2, x/y is 1/2; result is 1/4
n/d is 1/2, x/y is 1/6; result is 1/12
n/d is 3/4, x/y is 5/6; result is 5/8
n/d is 1/3, x/y is –2/3; result is -2/9
n/d is 0/3, x/y is 2/4; result is 0/1
10)
Rational divide(Rational other)
Pre-condition: The rational n/d is in a valid state and other is the valid rational x/y.
Post-condition: The rational number ny/dx has been returned.
Test cases:
n/d is 1/2, x/y is 1/2; result is 1/1
n/d is 1/2, x/y is 1/6; result is 3/1
n/d is 3/4, x/y is 5/6; result is 9/10
n/d is 1/3, x/y is –2/3; result is -1/2
n/d is 0/3, x/y is 2/4; result is 0/1
n/d is 2/4, x/y is 0/4; result is Exception
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

11)
String toString()
Pre-condition: The rational n/d is in a valid state.
Post-condition: The string “n/d” has been returned.
Test cases:
n/d is 1/2; result is “1/2”
n/d is 0/1; result is “0/1”
n/d is –2/5; result is “–2/5”
Counter
Our counter will be a class that acts like a simple click counter (used for counting attendance) with a few improvements. The
click counter will have a minimum and maximum value. It will start at the minimum value. Each click will add one to the
counter, except when the counter hits the maximum value, where it will roll back over to the minimum. The click counter will
also support an operation that decreases the value of the counter by one. If this would decrease the value below the minimum,
it will roll over to the maximum value.
Think about the preceding description and give a list of responsibilities for the Counter class.
The responsibilities of Counter class includes
a. Know the value of the minimum
b. Know the value of the maximum
c. Know the value of the counter
d. Be able to increment counter by 1
e. Be able to decrement counter by 1
What data fields will the Counter class need to implement these responsibilities?
Fields required to implement the Counter class are given below:
minimum
maximum
counter
Are there any constraints on these data fields?
The counter value can only be between minimum and maximum value
12)
Give a list of constructors and methods that will be used to implement the responsibilities you have listed. Fill in the pre-
conditions, post-conditions, and test cases.
Counter()
Pre-condition: none.
Post-condition: The Counter is initialized with 1 for minimum, maximum and counter value
Test cases: none.
Counter(min, max)
Pre-condition: The min must be less than or equal to max value
Post-condition: The min and max value is set.
The counter is initialized with min value
String toString()
Pre-condition: The rational n/d is in a valid state.
Post-condition: The string “n/d” has been returned.
Test cases:
n/d is 1/2; result is “1/2”
n/d is 0/1; result is “0/1”
n/d is –2/5; result is “–2/5”
Counter
Our counter will be a class that acts like a simple click counter (used for counting attendance) with a few improvements. The
click counter will have a minimum and maximum value. It will start at the minimum value. Each click will add one to the
counter, except when the counter hits the maximum value, where it will roll back over to the minimum. The click counter will
also support an operation that decreases the value of the counter by one. If this would decrease the value below the minimum,
it will roll over to the maximum value.
Think about the preceding description and give a list of responsibilities for the Counter class.
The responsibilities of Counter class includes
a. Know the value of the minimum
b. Know the value of the maximum
c. Know the value of the counter
d. Be able to increment counter by 1
e. Be able to decrement counter by 1
What data fields will the Counter class need to implement these responsibilities?
Fields required to implement the Counter class are given below:
minimum
maximum
counter
Are there any constraints on these data fields?
The counter value can only be between minimum and maximum value
12)
Give a list of constructors and methods that will be used to implement the responsibilities you have listed. Fill in the pre-
conditions, post-conditions, and test cases.
Counter()
Pre-condition: none.
Post-condition: The Counter is initialized with 1 for minimum, maximum and counter value
Test cases: none.
Counter(min, max)
Pre-condition: The min must be less than or equal to max value
Post-condition: The min and max value is set.
The counter is initialized with min value
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

Test cases:
min = 2, max = 4; result is min=2, max=4, counter=2
min = -5, max = 7; result is min=-5, max=7, counter=-5
min = 10, max = 4; result is Exception
int getMin()
Pre-condition: The Counter is in a valid state.
Post-condition: The value of min is returned.
Test cases:
min=2, max=4, counter=2; result is 2
min=-5, max=7, counter=-5; result is -5
int getMax()
Pre-condition: The Counter is in a valid state.
Post-condition: The value of max is returned.
Test cases:
min=2, max=4, counter=2; result is 4
min=-5, max=7, counter=-5; result is 7
int getMax()
Pre-condition: The Counter is in a valid state.
Post-condition: The value of counter is returned.
Test cases:
min=2, max=4, counter=3; result is 3
min=-5, max=7, counter=1; result is 1
void increment()
Pre-condition: The Counter is in a valid state.
Post-condition: The value of counter is incremented by 1
Test cases:
min=2, max=4, counter=3; result is min=2, max=4, counter=4
min=-5, max=7, counter=7; result is min=-5, max=7, counter=-5
void decrement()
Pre-condition: The Counter is in a valid state.
Post-condition: The value of counter is decremented by 1
Test cases:
min=2, max=4, counter=3; result is min=2, max=4, counter=2
min=-5, max=7, counter=-5; result is min=-5, max=7, counter=7
min = 2, max = 4; result is min=2, max=4, counter=2
min = -5, max = 7; result is min=-5, max=7, counter=-5
min = 10, max = 4; result is Exception
int getMin()
Pre-condition: The Counter is in a valid state.
Post-condition: The value of min is returned.
Test cases:
min=2, max=4, counter=2; result is 2
min=-5, max=7, counter=-5; result is -5
int getMax()
Pre-condition: The Counter is in a valid state.
Post-condition: The value of max is returned.
Test cases:
min=2, max=4, counter=2; result is 4
min=-5, max=7, counter=-5; result is 7
int getMax()
Pre-condition: The Counter is in a valid state.
Post-condition: The value of counter is returned.
Test cases:
min=2, max=4, counter=3; result is 3
min=-5, max=7, counter=1; result is 1
void increment()
Pre-condition: The Counter is in a valid state.
Post-condition: The value of counter is incremented by 1
Test cases:
min=2, max=4, counter=3; result is min=2, max=4, counter=4
min=-5, max=7, counter=7; result is min=-5, max=7, counter=-5
void decrement()
Pre-condition: The Counter is in a valid state.
Post-condition: The value of counter is decremented by 1
Test cases:
min=2, max=4, counter=3; result is min=2, max=4, counter=2
min=-5, max=7, counter=-5; result is min=-5, max=7, counter=7
1 out of 5
Related Documents
Your All-in-One AI-Powered Toolkit for Academic Success.
+13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
Copyright © 2020–2026 A2Z Services. All Rights Reserved. Developed and managed by ZUCOL.





