The Fraction Class – Immutable Classes In this section, we’ll build another Fraction class that is unchangeable once initialized and uses the keyword final for its numerator and denominator. Such a Fraction object will have all of its data declared final, and is our first example of building an immutable class. Once a Fraction object is built, its data items will never change for the lifetime of the object. Another way to view this is that the object is completely read-only. As a result, its data will also be declared public, which is the only example of public data you’ll find in this quarter. If you wish to change a fraction object’s numerator or denominator, the old object must be discarded and a new object created in its place. Again, this object’s data will be immutable, constant, non-variable, unchangeable, non-editable, or read-only. So, to add two fractions, our add function will return a new Fraction object that is the sum of the two previous (unchangeable) fractions we wish to add. Once you’ve built this class, uncomment out the appropriate tests in ClassDesignIIDriver.java. Start by building a new Fraction class, and define the following members: Class Invariants Numerators and denominators are unchangeable once set by the constructor. No denominator will be stored as a 0. (i.e., no DivideByZero Exceptions). A Fraction is always in reduced form (reduce in the constructor to ensure this). Data Define a numerator that is public and final. Why don’t we make this data private? Define a denominator that is public and final. What data types should these items be? Methods Define a constructor that takes a numerator and a denominator Do not define a no-argument constructor. Why? Define a constructor that takes a Fraction object and makes a copy of it. public Fraction(Fraction other){...} Define a toString() function as we’ve done for other classes. Define an add function that takes a fraction, adds it to this, then returns a new Fraction object that is the result of the addition of the two public Fraction add(Fraction that) {//add this and that together; remember to consider the denominator here! Define an equals(Object o) function that has the form: public boolean equals(Object other) { if( other != null && ! (other instanceof Fraction ) ) return false; //what does this code do? Fraction that = (Fraction) other; //and this code? //todo: code goes here }

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Modularization Techniques
Section: Chapter Questions
Problem 13RQ
icon
Related questions
Question
100%

The Fraction Class – Immutable Classes

In this section, we’ll build another Fraction class that is unchangeable once initialized and uses the keyword final for its numerator and denominator. Such a Fraction object will have all of its data declared final, and is our first example of building an immutable class. Once a Fraction object is built, its data items will never change for the lifetime of the object. Another way to view this is that the object is completely read-only. As a result, its data will also be declared public, which is the only example of public data you’ll find in this quarter. If you wish to change a fraction object’s numerator or denominator, the old object must be discarded and a new object created in its place. Again, this object’s data will be immutable, constant, non-variable, unchangeable, non-editable, or read-only. So, to add two fractions, our add function will return a new Fraction object that is the sum of the two previous (unchangeable) fractions we wish to add. Once you’ve built this class, uncomment out the appropriate tests in ClassDesignIIDriver.java. Start by building a new Fraction class, and define the following members:

Class Invariants

  • Numerators and denominators are unchangeable once set by the constructor.
  • No denominator will be stored as a 0. (i.e., no DivideByZero Exceptions).
  • A Fraction is always in reduced form (reduce in the constructor to ensure this).

Data

  • Define a numerator that is public and final.
    • Why don’t we make this data private?
  • Define a denominator that is public and final.
    • What data types should these items be?

Methods

    • Define a constructor that takes a numerator and a denominator
      • Do not define a no-argument constructor. Why?
    • Define a constructor that takes a Fraction object and makes a copy of it.
      • public Fraction(Fraction other){...}
    • Define a toString() function as we’ve done for other classes.
    • Define an add function that takes a fraction, adds it to this, then returns a new Fraction object that is the result of the addition of the two
      • public Fraction add(Fraction that) {//add this and that together; remember to consider the denominator here!
    • Define an equals(Object o) function that has the form:

public boolean equals(Object other) {

if( other != null && ! (other instanceof Fraction ) ) return false; //what does this code do?

Fraction that = (Fraction) other; //and this code? //todo: code goes here }

public static void immutableFractionDriver() {
Fraction a = new Fraction(1,2);
Fraction b = new Fraction(3,4);
Fraction c = new Fraction(b);
System.out.println("a:"+a.toString());
System.out.println("b:"+b.toString());
System.out.println("c:"+c.toString());
//(1)uncomment out to discover errors
//a.setNum(3);
//a.setDenom(5);
//(2)fix the following 2 errors
System.out.println("a.num:"+a.getNum());
System.out.println("b.denom:"+b.getDenom());
//(3)which of the following code is correct to change the fraction a?
a.gdd(b);
a - a.gdd(b);
System.out.println("a: "+a.toString());
System.out.println("b:"+b.toString());
System.out.println("c:"+c.toString());
System.out.println("a.equals(b):" + a.equals(b));
System.out.println("b.equals(c):'
+ b.equals(c));
Transcribed Image Text:public static void immutableFractionDriver() { Fraction a = new Fraction(1,2); Fraction b = new Fraction(3,4); Fraction c = new Fraction(b); System.out.println("a:"+a.toString()); System.out.println("b:"+b.toString()); System.out.println("c:"+c.toString()); //(1)uncomment out to discover errors //a.setNum(3); //a.setDenom(5); //(2)fix the following 2 errors System.out.println("a.num:"+a.getNum()); System.out.println("b.denom:"+b.getDenom()); //(3)which of the following code is correct to change the fraction a? a.gdd(b); a - a.gdd(b); System.out.println("a: "+a.toString()); System.out.println("b:"+b.toString()); System.out.println("c:"+c.toString()); System.out.println("a.equals(b):" + a.equals(b)); System.out.println("b.equals(c):' + b.equals(c));
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 7 images

Blurred answer
Knowledge Booster
Reference Types in Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage