Question
JAVA PROJECT Extra Credit 0f 100 points for doing Q3 1. Write a CRC card and a class diagram for a proposed class called Counter.
JAVA PROJECT Extra Credit 0f 100 points for doing Q3
1. Write a CRC card and a class diagram for a proposed class called Counter. An object of this class will be used to count things, so it will record a count that is a nonnegative whole number. Include methods to set the counter to a given integer, to increase the count by 1, and to decrease the count by 1. Also include a method that returns the current count as an integer, a method toString that returns the current count as a string suitable for display on the screen, and a method that tests whether the current count is zero. Create an interface for the class Counter .
2. Suppose you want to design software for a restaurant. Give use cases for placing an order and settling the bill. Identify a list of possible classes. Pick two of these classes, and write CRC cards for them.
3. Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two fractions are equal, and convert a fraction to a string.
Your class should handle denominators that are zero. Fractions should always occur in lowest terms, and the class should be responsible for this requirement. For example, if the user tries to create a fraction such as 4/8, the class should set the fraction to 1/2. Likewise, the results of all arithmetic operations should be in lowest terms. Note that a fraction can be improperthat is, have a numerator that is larger than its denominator. Such a fraction, however, should be in lowest terms.
Design the class Fraction. Begin by writing a CRC card for this class. Then write a Java interface that declares each public method.
To reduce a fraction such as 4/8 to lowest terms, you need to divide both the numerator and the denominator by their greatest common denominator. The greatest common denominator of 4 and 8 is 4, so when you divide the numerator and denominator of 4/8 by 4, you get the fraction 1/2. The following recursive algorithm finds the greatest common denominator of two positive integers:
Algorithm gcd(integerOne, integerTwo)
if (integerOne % integerTwo == 0)
result = integerTwo
else
result = gcd(integerTwo, integerOne % integerTwo) return result
It will be easier to determine the correct sign of a fraction if you force the fractions denominator to be positive. However, your implementation must handle negative denominators that the client might provide.
Write a program that adequately demonstrates your class.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started