Олимпиадный тренинг

Задача . 37024


Задача

Темы:
public class Rational
{
private int numerator;
private int denominator;
/** default constructor */
Rational()
{ /* implementation not shown */ }
/** Constructs a Rational with numerator n and
* denominator 1. */
Rational(int n)
{ /* implementation not shown */ }
/** Constructs a Rational with specified numerator and
* denominator. */
Rational(int numer, int denom)
{ /* implementation not shown */ }
/** @return numerator */
int numerator()
{ /* implementation not shown */ }
/** @return denominator */
int denominator()
{ /* implementation not shown */ }
/** Returns (this + r). Leaves this unchanged.
* @return this rational number plus r
* @param r a rational number to be added to this Rational
*/
public Rational plus(Rational r)
{ /* implementation not shown */ }
//Similarly for times, minus, divide
...
/** Ensures denominator > 0. */
private void fixSigns()
{ /* implementation not shown */ }
/** Ensures lowest terms. */
private void reduce()
{ /* implementation not shown */ }
}

Here is the implementation code for the plus method:
/** Returns (this + r). Leaves this unchanged.
* @return this rational number plus r
* @param r a rational number to be added to this Rational
*/
public Rational plus(Rational r)
{
fixSigns();
r.fixSigns();
int denom = denominator * r.denominator;
int numer = numerator * r.denominator
+ r.numerator * denominator;
/* more code */
}

Which of the following is a correct replacement for /* more code */?
(A) Rational rat(numer, denom);
      rat.reduce();
      return rat;
(B) return new Rational(numer, denom);
(C) reduce();
      Rational rat = new Rational(numer, denom);
      return rat;
(D) Rational rat = new Rational(numer, denom);
      Rational.reduce();
      return rat;
(E) Rational rat = new Rational(numer, denom);
      rat.reduce();
      return rat;

time 1000 ms
memory 32 Mb
Правила оформления программ и список ошибок при автоматической проверке задач

Статистика успешных решений по компиляторам
Комментарий учителя