Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My question is what does @overide does in this piece of code : Fraction (in this file) public class Fraction implements Comparable { private int

My question is what does @overide does in this piece of code :

Fraction (in this file)

public class Fraction implements Comparable { private int numer; private int denom;

public int getNumer() { return numer; } public int getDenom() { return denom; }

public void setNumer( int n ) { numer = n; } public void setDenom( int d ) { if (d==0) { System.out.println("Can't have 0 in denom"); System.exit(0); } else denom=d; }

public Fraction( int n, int d ) { int gcd = gcd( n, d ); setNumer(n/gcd); setDenom(d/gcd); } private int gcd( int n, int d ) { if (d == 0) { return n; } return gcd(d, n % d); }

public Fraction( Fraction other ) { this( other.getNumer(), other.getDenom() ); // call my full C'Tor with other Fraction's data }

public int compareTo( Fraction other ) { Fraction f=this;

f=f.subtract(other);

if((f.getNumer()>0 &&f.getDenom()>0) ||(f.getNumer()<0 &&f.getDenom()<0 )) { return 1; }

if((f.getNumer()>0 &&f.getDenom()<0) ||(f.getNumer()<0 &&f.getdenom()>0 )) { return -1; } else { return 0; } } public String toString() { return getNumer() + "/" + getDenom() + "\t=" + + ((double)getNumer()/(double)getDenom()); } private Fraction subtract(Fraction f) { int a=this.numer; int b=this.denom; int c=f.numer; int d=f.denom; int num=(a*d - b*c); int den=(b*d); Fraction frac=new Fraction(num,den); return frac; } }

FractionTester :

import java.io.*;

import java.util.*;

public class FractionTester

{

public static void main( String args[] ) throws Exception

{

Random generator = new Random( 17 );

// POPULATE AN ARRAYLIST OF FRACTIONS WITH RANDOM VALUES

ArrayList AList = new ArrayList();

for (int i=5 ; i>=0 ; --i )

AList.add( new Fraction(1+generator.nextInt(100), 1+generator.nextInt(100)) ); // numer and denom in [1..99]

// MAKE DEEP COPY OF THOSE FRACTIONS IN THE ARAYLIST ABOVE, BUT STORE INTO PLAIN ARRAY OF FRACTION

Fraction[] plainArr = new Fraction[ AList.size() ];

for ( int i = 0 ; i < plainArr.length ; ++i )

plainArr[i] = new Fraction( AList.get(i) );

// PRINT BOTH CONTAINERS TO VERIFY THEY MATCH

System.out.println(" ArrayList OF FRACTIONS UNSORTED:");

for ( Fraction f : AList )

System.out.println( f );

System.out.println(" plainArr OF FRACTIONS UNSORTED:");

for ( Fraction f : plainArr )

System.out.println( f );

// SORT BOTH CONTAINERS USING APPROPRIATE LIBRARY

Collections.sort( AList );

Arrays.sort( plainArr );

//RE-PRINT BOTH AND THEY SHOULD BE IDENTICAL AND IN SORTED ORDER

System.out.println(" ArrayList OF FRACTIONS SORTED:");

for ( Fraction f : AList )

System.out.println( f );

System.out.println(" plainArr OF FRACTIONS SORTED:");

for ( Fraction f : plainArr )

System.out.println( f );

}// END MAIN

} // END

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Conceptual Database Design An Entity Relationship Approach

Authors: Carol Batini, Stefano Ceri, Shamkant B. Navathe

1st Edition

0805302441, 978-0805302448

Students also viewed these Databases questions

Question

Prepare for a successful job interview.

Answered: 1 week ago

Question

Describe barriers to effective listening.

Answered: 1 week ago

Question

List the guidelines for effective listening.

Answered: 1 week ago