Menu

[Solved]Java Signed Fractions Assignment Implemented Interfaces Several Times Lab Week Require Ext Q37116442

Java Signed Fractions Assignment

We have implemented interfaces several times. Your Lab this weekwill require you to Extend the Fraction class you wrote a whileback. You also inpliment the comparable interface in the Fractionclass. Now you will extend Fraction to create the signed Fractionclass. We will start with a small working Fraction class and extendit to define a sub-type or child-class named SignedFraction.
You are given a Fraction class definitionFraction.java and a tester programFractionTester.java

The FractionTester creates an array of Fractions where some ofthem have negative numerators or denominators or both. Read thesetwo files, compile them and execute the tester. Notice it printseach Fraction as a ratio then as a floating point value on the sameline. In particular notice that both the numer and denom have theirsign printed in front of them exactly as originally initialized inmain.

Your task for Lab#1 is to extend Fraction to defineSignedFraction.java and get it to compile andexecute correctly with thisSignedFractionTester.java

The output of the SignedFractionTester differs from the originalFractionTester becuase the SignedFraction over-rides it’s parentfull constructor and toString() such that the new full constructorstores a sign value in the SignedFraction and stores the numer anddenom as positives. As a result the new toString() prints the sign(if negative) only once before the ratio instead printing the signsin front of each number and denom.

  • Your job is to complete the SignedFraction file as follows:

    • add a private int named sign. The setter and getter are writtenfor you.
    • write a full constructor named SignedFraction( int n, int d )that looks at the incoming n and d and sets the sign to either 1 or-1. Once the sign is set, assign into n and d to their absolutevalues using Math.abs(). The rest of the full constructor iswritten for you.
    • over-ride the toString so that it prints out the sign at thebeginning of the line (only it it’s negative). You then must printthe value of the SignedFrction as a double with the same sign (ifnegative) in front of it. **See output.

All files and correct outputs are posted below. Do not modifyfiles more than the instructions direct.

Fraction.java X 1 public class Fraction private int numer; private int denom; 4 6 8 10 /I ACCESSORS (SETTERS) public int getNraction lester.java X 1 import java.io.*; 2 import java.util.*; 4 public class FractionTester 6 public static void main( StriCX Command prompt Microsoft windows iversion 10.0.17134. 5231 (c) 2018 Microsoft Corporation. A1l rights reserved CuserstimSignedFraction.iava X 1 public class SignedFraction extends Fraction // your new int member here 4 /I FULL CONSTRUCTOR STORES

public class SignedFraction extends Fraction{ // your new int member here // FULL CONSTRUCTOR – STORES SIGN OF THE FRACTION THEN SETS BOTH number and denom POSITIVE public SignedFraction( int n, int d ) { // FYI: JAVA JUST CALLED YOUR PARENT’S DEFAULT CONSTRUCTOR. YOU ARE 0/1 NOW // WE’LL FIX THAT SOON ENUF // SET YOUR sign TO EITHER -1 OR 1 BASED ON INCOMING n AND d // ASSIGN IN TO n AND D, THEIR abs VALUES. i.e. FLIP EM POSITIVE // NOW DO EXACTLY AS YOUR PARENT DID AT THIS POINT int gcd = gcd( n, d ); setNumer(n/gcd); setDenom(d/gcd); } private void setSign( int s ) { if (s!=1 && s!=-1) // THE ONLY POSSIBLE LEAGAL VALUES FOR THE SIGN { System.out.println( “FATAL ERROR: Attempt to assign invalid sign value: ” + s ); System.exit(0); } sign = s; } private int getSign() { return sign; } // OVERWRITE/RIDE PARENT toString public String toString() { return “”; // REPLACE WITH YOUR CODE }}// EOF

SignedFractionTester.java*X 1 import java.io.*; 2 import java.util.*; 4 public class SignedFractionTester public static voidCommand Prompt C:Users timDesktop>dir *.java You must have the 3 files listed below in your working directory to execute you

Fraction.java X 1 public class Fraction private int numer; private int denom; 4 6 8 10 /I ACCESSORS (SETTERS) public int getNumer() return numer; public int getDenom() return denom; 12 13 14 15 16 17 18 19 20 21 // MUTATORS (GETTERS) public void setNumer( intn) numer = n; public void setDenom( int d) if (d–0) System.out.println(“Can’t have in denom”) System.exit(0); 1 else denom-d; 23 24 25 26 27 28 29 30 31 32 /I FULL CONSTRUCTOR – an arg for each class data member public Fraction int n, int d) { int gcd -gcd Math.abs (n), Math.abs(d) ; setNumer (n/gcd); setDenom(d/gcd); /I DEFULT CONSTRUCTOR 0/1 public Fraction() this , 1); I/ DEFER TO FULL C’TOR 34 35 36 37 38 39 40 41 42 43 // GIVING YOU A WORKING (ITERATIVE) GCD J.1.C. YOU HAVE NEVER WRITTEN ONE public int gcd( int n, int d ) { int gcd n«d ? n : d; // same as > İf (n«d) gcd-n; else gcd-d; while( gcd> return gcd; else gcd; return 1; // they were co-prime no GCD exceopt 1:( 45 46 47 48 49 50 public string tostring() // USE AS İS. DO NOT DELETE OR MODIFY return getNumer() “/” getDenon() + ((double)getNumer()/(double)getDenom()); “t=” + + + + 51 / EOF raction lester.java X 1 import java.io.*; 2 import java.util.*; 4 public class FractionTester 6 public static void main( String args[ Random generatornew Random 17) int[] numers = { 13, -7, 26, -5, 8, -1 }; int [ ] denoms { 3, -49, 13, 15, -7, -1 }; 9 10 /I POPULATE AN ARRAY OF FRACTIONS WITH SOME VALUES 12 13 Fraction[] plainArr new Fraction numers.length ]; for ( int 1-0 ; ¡ く numers.length ; ++1 ) 15 16 17 18 19 20 ); denoms [1] numers [1], plainArr[i] = new Fraction ( System.out.println(“InplainArr OF FRACTIONS”); for Fraction f plainArr) System.out.println( f / END MAIN END 23 CX Command prompt Microsoft windows iversion 10.0.17134. 5231 (c) 2018 Microsoft Corporation. A1l rights reserved Cuserstim cd Desktop Cuserstim Desktop>javac FractionTester.java Cuserstim Desktop>java FractionTester plainArr oF FRACTIONS 13/3 .333333333333333 -1/-7 =0.14285714285714285 =2.0 1/3 8/-7 0. 3333333333333333 =-1.1428571428571428 C:userstim Desktop> SignedFraction.iava X 1 public class SignedFraction extends Fraction // your new int member here 4 /I FULL CONSTRUCTOR STORES SIGN OF THE FRACTION THEN SETS BOTH number and denom POSITIVE public SignedFraction int n, int d) 6 II FYI JAVA JUST CALLED YOUR PARENT’S DEFAULT CONSTRUCTOR. YOU ARE 0/1 NOW /I WE’LL FIX THAT S0ON ENUF 9 10 /I SET YOUR sign TO EITHER -1 OR 1 BASED ON INCOMING n AND d // ASSIGN IN TO n AND D, THEIR abs VALUES. İ.e. FLIP EM POSITIVE 12 13 14 15 16 17 18 19 20 21 I NOW DO EXACTLY AS YOUR PARENT DID AT THIS POINT int gcdgcd( n, d ); setNumer (n/gcd) setDenom(d/gcd); private void setSign( int s) if (s!-1 && s!1) THE ONLY POSSIBLE LEAGAL VALUES FOR THE SIGN 23 System.out.println( “FATAL ERROR: Attempt to assign invalid sign value: “s; System.exit(0); 25 26 27 28 29 30 sign = s; private int getSign() 32 return sign; 34 35 36 37 38 39 40 41 /I EOF // OVERWRITE/RIDE PARENT toString public String toString() return ” // REPLACE WITH YOUR CODE We were unable to transcribe this imageCommand Prompt C:Users timDesktop>dir *.java You must have the 3 files listed below in your working directory to execute your solution. Volume in drive C is Win10Edu Volume Serial Number is F4D4-CAFC Directory of c:UserstimDesktop 1,126 Fraction.java 1,338 SignedFraction. java 01/14/2019 12:55 AM 01/14/2019 04:55 PM 01/13/2019 10:58 PM 629 SignedFractionTester.java 3,093 bytes 3 File(s) 0 Dir(s) 37,017,460,736 bytes free As long as you only have these files in C:UserstimDesktop>javac *. java C:UserstimDesktop>java signedFractionTesterthe dir you can do a plainArr oF SIGNED FRACTIONS javac*.java and it WIII complle them all 13/34.333333333333333 -0.14285714285714285 -2.0 1/3 –0.3333333333333333 8/7-1.1428571428571428 c:userstimDesktop:> Show transcribed image text Fraction.java X 1 public class Fraction private int numer; private int denom; 4 6 8 10 /I ACCESSORS (SETTERS) public int getNumer() return numer; public int getDenom() return denom; 12 13 14 15 16 17 18 19 20 21 // MUTATORS (GETTERS) public void setNumer( intn) numer = n; public void setDenom( int d) if (d–0) System.out.println(“Can’t have in denom”) System.exit(0); 1 else denom-d; 23 24 25 26 27 28 29 30 31 32 /I FULL CONSTRUCTOR – an arg for each class data member public Fraction int n, int d) { int gcd -gcd Math.abs (n), Math.abs(d) ; setNumer (n/gcd); setDenom(d/gcd); /I DEFULT CONSTRUCTOR 0/1 public Fraction() this , 1); I/ DEFER TO FULL C’TOR 34 35 36 37 38 39 40 41 42 43 // GIVING YOU A WORKING (ITERATIVE) GCD J.1.C. YOU HAVE NEVER WRITTEN ONE public int gcd( int n, int d ) { int gcd n«d ? n : d; // same as > İf (n«d) gcd-n; else gcd-d; while( gcd> return gcd; else gcd; return 1; // they were co-prime no GCD exceopt 1:( 45 46 47 48 49 50 public string tostring() // USE AS İS. DO NOT DELETE OR MODIFY return getNumer() “/” getDenon() + ((double)getNumer()/(double)getDenom()); “t=” + + + + 51 / EOF
raction lester.java X 1 import java.io.*; 2 import java.util.*; 4 public class FractionTester 6 public static void main( String args[ Random generatornew Random 17) int[] numers = { 13, -7, 26, -5, 8, -1 }; int [ ] denoms { 3, -49, 13, 15, -7, -1 }; 9 10 /I POPULATE AN ARRAY OF FRACTIONS WITH SOME VALUES 12 13 Fraction[] plainArr new Fraction numers.length ]; for ( int 1-0 ; ¡ く numers.length ; ++1 ) 15 16 17 18 19 20 ); denoms [1] numers [1], plainArr[i] = new Fraction ( System.out.println(“InplainArr OF FRACTIONS”); for Fraction f plainArr) System.out.println( f / END MAIN END 23
CX Command prompt Microsoft windows iversion 10.0.17134. 5231 (c) 2018 Microsoft Corporation. A1l rights reserved Cuserstim cd Desktop Cuserstim Desktop>javac FractionTester.java Cuserstim Desktop>java FractionTester plainArr oF FRACTIONS 13/3 .333333333333333 -1/-7 =0.14285714285714285 =2.0 1/3 8/-7 0. 3333333333333333 =-1.1428571428571428 C:userstim Desktop>
SignedFraction.iava X 1 public class SignedFraction extends Fraction // your new int member here 4 /I FULL CONSTRUCTOR STORES SIGN OF THE FRACTION THEN SETS BOTH number and denom POSITIVE public SignedFraction int n, int d) 6 II FYI JAVA JUST CALLED YOUR PARENT’S DEFAULT CONSTRUCTOR. YOU ARE 0/1 NOW /I WE’LL FIX THAT S0ON ENUF 9 10 /I SET YOUR sign TO EITHER -1 OR 1 BASED ON INCOMING n AND d // ASSIGN IN TO n AND D, THEIR abs VALUES. İ.e. FLIP EM POSITIVE 12 13 14 15 16 17 18 19 20 21 I NOW DO EXACTLY AS YOUR PARENT DID AT THIS POINT int gcdgcd( n, d ); setNumer (n/gcd) setDenom(d/gcd); private void setSign( int s) if (s!-1 && s!1) THE ONLY POSSIBLE LEAGAL VALUES FOR THE SIGN 23 System.out.println( “FATAL ERROR: Attempt to assign invalid sign value: “s; System.exit(0); 25 26 27 28 29 30 sign = s; private int getSign() 32 return sign; 34 35 36 37 38 39 40 41 /I EOF // OVERWRITE/RIDE PARENT toString public String toString() return ” // REPLACE WITH YOUR CODE

Command Prompt C:Users timDesktop>dir *.java You must have the 3 files listed below in your working directory to execute your solution. Volume in drive C is Win10Edu Volume Serial Number is F4D4-CAFC Directory of c:UserstimDesktop 1,126 Fraction.java 1,338 SignedFraction. java 01/14/2019 12:55 AM 01/14/2019 04:55 PM 01/13/2019 10:58 PM 629 SignedFractionTester.java 3,093 bytes 3 File(s) 0 Dir(s) 37,017,460,736 bytes free As long as you only have these files in C:UserstimDesktop>javac *. java C:UserstimDesktop>java signedFractionTesterthe dir you can do a plainArr oF SIGNED FRACTIONS javac*.java and it WIII complle them all 13/34.333333333333333 -0.14285714285714285 -2.0 1/3 –0.3333333333333333 8/7-1.1428571428571428 c:userstimDesktop:>

Expert Answer


Answer to Java Signed Fractions Assignment We have implemented interfaces several times. Your Lab this week will require you to Ex… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *