Question: How to write this decimal class with all these six methods? Numbers This code will go in the file Numbers.java, any tests in a class

How to write this decimal class with all these six methods?

Numbers

This code will go in the file Numbers.java, any tests in a class called ExampleNumbers that you add to that file.

Remember that you will not be nesting any interfaces or classes. Doing so will cause the autograder to fail.

We saw in our reading that representing fractional numbers like 0.6 with doubles can be fraught. Some languages and libraries do support exact fractions, and we can implement classes that act like them in Java. We wont be able to use the built-in + and * operators, because these are only defined for numbers and strings, but we can define methods for the operations we care about. We can represent numbers with an interface:

interface Number { int numerator(); int denominator(); Number add(Number other); Number multiply(Number other); String toText(); double toDouble(); } 

Your task is to create three classes that implement the interface above. One should be called WholeNumber and represent whole integers. The second should be called Fraction and represent mixed numbers. The third should be called Decimal and represent decimals

WholeNumber should have:

  • A field int n and a constructor that takes a single int
  • An implementation of all the methods above.
    • numerator should return the value of n
    • denominator should return 1
    • add should return a new Number that represents adding this whole number to the one provided as an argument. Note that the argument could be either a WholeNumber, Fraction, or Decimal
    • multiply should return a new Number that represents multiplying this whole number to the one provided as an argument. Note that the argument could be either a WholeNumber, Fraction, or Decimal
    • toText should return the value of n as a String, so if n is 500, it should return "500"
    • toDouble should return the value of n as a double

Fraction should have:

  • A field int n representing the numerator
  • A field int d representing the denominator
  • An implementation of all the methods above:
    • numerator should return the value of n
    • denominator should return the value of d
    • add should return a new Number that represents adding this fraction to the one provided as an argument. Note that the argument could be either a WholeNumber, Fraction, or Decimal
    • multiply should return a new Number that represents multiplying this fraction by the one provided as an argument. Note that the argument could be either a WholeNumber, Fraction, or Decimal
    • toText should return a String in the format "n/d" where n and d are the corresponding fields. So if n and d were 1 and 2, this should be "1/2"
    • toDouble should return the value of n/d as a double. So if n is 1 and d is 2, this should return 0.5

    Decimal should have:

    You may find it helpful to use Integer.parseInt() to convert a string to an integer.

    • A field int mantissa representing the mantissa of the decimal when expressed in scientific notation
    • A field int exponent representing the exponent of the decimal when expressed in scietific notation
    • An implementation of all the methods above:
      • numerator should return the numerator of the decomal when expressed as a fraction
      • denominator should return the denominator of the decimal when expressed as a fraction
      • add should return a new Number that represents adding this decimal to the one provided as an argument. Note that the argument could be either a WholeNumber, Fraction, or Decimal
      • multiply should return a new Number that represents multiplying this decimal to the one provided as an argument. Note that the argument could be either a WholeNumber, Fraction, or Decimal
      • toText should return a String in the format #.#. So if mantissa is 314 and exponent is -2, this should be "3.14". However, if mantissa is 12345 and exponent is -3, then the result should be "12.345". The amount of numbers before and after the decimal point will depend on the original input.
      • toDouble should return the value of the number expressed by the mantissa and exponent as a double. So if mantissa is 314 and exponent is -2, this should be 3.14

Some example tests that you can use are below. You can copy-paste these into your solution as you implement the various methods. All of these tests must pass on your implementation.

 Number n1 = new WholeNumber(5); Number n2 = new WholeNumber(7); Number n3 = new Fraction(7, 2); Number n4 = new Fraction(1, 2); Number n5 = new Decimal("3.25"); Number n6 = new Decimal("5.5"); void testAdd(Tester t) { t.checkExpect(this.n1.add(this.n2).toDouble(), 12.0); t.checkExpect(this.n1.add(this.n3).toDouble(), 5 + 7.0/2.0); t.checkExpect(this.n3.add(this.n3).toDouble(), 7.0); t.checkExpect(this.n5.add(this.n4).toDouble(), 3.75); } void testMultiply(Tester t) { t.checkExpect(this.n1.multiply(this.n4).toDouble(), 2.5); t.checkExpect(this.n3.multiply(this.n4).toDouble(), 7.0/4.0); t.checkExpect(this.n6.multiply(this.n1).toDouble(), 27.5); } void testNumDem(Tester t) { t.checkExpect(this.n3.numerator(), 7); t.checkExpect(this.n1.numerator(), 5); t.checkExpect(this.n5.numerator(), 325); t.checkExpect(this.n4.denominator(), 2); t.checkExpect(this.n2.denominator(), 1); t.checkExpect(this.n6.denominator(), 10); } void testToString(Tester t) { t.checkExpect(this.n4.toText(), "1/2"); t.checkExpect(this.n3.toText(), "7/2"); t.checkExpect(this.n2.toText(), "7"); t.checkExpect(this.n5.toText(), "3.25"); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!