Complete and fully test the class Characteristic that Exercise 5 describes. Include the following methods: getDescriptionreturns the

Question:

Complete and fully test the class Characteristic that Exercise 5 describes. Include the following methods:

  • getDescription—returns the description of this characteristic.
  • getRating—returns the rating of this characteristic.
  • getCompatability(Characteristic otherRating)—returns the compatibility measure of two matching characteristics, or zero if the descriptions do not match.
  • getCompatibilityMeasure(Characteristic otherRating)—a private method that returns a compatibility measure as a double value using the formula

(r1 − r2)2

m = 1 − 81


when both ratings are nonzero; m is zero if either rating is zero. (Recall from Exercise 5 that the constructor sets the rating to zero, indicating that it has not yet been determined.)

  • isMatch(Characteristic otherRating)—a private method that returns true if the descriptions match.


Exercise 5

Consider a class Characteristic that will be used in an online dating service to assess how compatible two people are. Its attributes are

  • description—a string that identifies the characteristic
  • rating—an integer between 1 and 10 that indicates a person’s desire for this characteristic in another person

a. Write a constructor that sets the description of the characteristic to a given string and sets the rating to zero to indicate that it has not yet been determined.

b. Write a private method isValid(aRating) that returns true if the given rating is valid, that is, is between 1 and 10.

c. Write a method setRating(aRating) that sets the rating to aRating if it is valid.

d. Write a method setRating that reads a rating from the keyboard, insisting that the rating supplied by the user be valid.

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Question Posted: