1. Engineering
  2. Computer Science
  3. implement a class to represent rational numbers each rational number...

Question: implement a class to represent rational numbers each rational number...

Question details

Implement a class to represent rational numbers. Each rational number consists of a numerator and a denominator, both of type int. Since each rational number has its own numerator and denominator, these must be instance variables. Furthermore, good object-oriented programming suggests that the visibility of the variables should be private 2.1 Constructors The class Rational has two constructors. One of them has two formal parameters, which provide the initial values for both instance variables. The other constructor has a single parameter, which provides the initial value for the nunerator; the denominator is assumed to be 1. 2.2 gctters Implement access methods that return the numerator and denominator of this rational, but no setter methods. An object that has no setter methods, and no other methods for transforming the state of the object, is said to be immutable. Immutable is a great property. Do you see why? Discuss this with your neighbors and TA 2.3 plus Implement the instance method plus. The method has a single formal parameter, of type Rational. The method returns a new Rational object that represents the sum of this number and that of the parameter. 2.4 plus (part 2) Implement a class method plus. The method has two formal parameters, both of type Rational. The method returns a new Rational object that represents the sum of the two numbers. 2.5 ged Implement a private class method for calculating the greatest common divisor of two integers, which are the formal parameters of the method.Implement a private class method for calculating the greatest common divisor of two integers, which are the formal parameters of the method 2.6 reduce Implement a private instance method called reduce that transforms this number into its reduced form.(ex: 18/4->9/2 2.7 reduce (part 2 Make all the necessary changes so that a rational number is always stored in reduced form. 2.8 cquals Implement the instance method public boolean equals(Rational o) that returns true if this fraction and the one designated by o represent the same fraction (content equality) 2.9 toString Add a method public String toString) that returns a String representation of this fraction with the numerator followed by the symbol , followed by the denominator. If the denominator is 1 then the method returns a String consisting of the numerator only. 210 compareTo Implements the instance method int compareTol Rational o). It compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.pRationaljava Sublime Text (UNREGISTERED File Edit Selection Find View Goto Tools Project Preferences Help A1Q01.java 1 Q2java 103,ava Card.java Die.java xRational. java private int numerator; private int denominator; // constructors public Rational (int numerator) Your code here 10 12 13 14 15 16 17 18 19 2e 21 public Rational (int numerator, int denominator) Your code here // getters public int getNumerator) return numerator; public int getDenominator) return denominator 23 24 25 26 27 28 29 3e 31 32 // instance methods public Rational plus(Rational other) Your code here public static Rational plus (Rational a, Rational b) t // Your code here 34 35 36 37 38 / Transforms this number into its reduced form private void reduce() Line 16, column 15 Soaces: 4 ava 4:77 PM Type here to search 1/28/2019pRationaljava Sublime Text (UNREGISTERED File Edit Selection Find View Goto Tools Project Preferences Help A1Q1.java 1 Q2java A103.,ava × ▼ Card.java Die.java Rational.java ㄨ ran his number into 1ts reduced torm 37 38 39 40 41 42 43 private void reduce() f Your code here Euclids algorithm for calculating the greatest common divisor private int ged(int a, int b) t while (a != b) if (a> b) 45 46 47 48 49 5e 51 52 53 54 else b=b-a; return a public int compareTo (Rational other) t Your code here 56 57 58 59 60 61 62 53 54 65 public boolean equals (Rational other) i Your code here public String toString) f String result; if (denominator =-1) { Your code here else Your code here 67 58 69 70 return result Line 1, tolumn 15 Soaces: 4 4:77 PM Type here to search 1/28/2019

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution