Fraction class, help via java oop?
-
Write a Fraction class that implements these methods: add ─ This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction. multiply ─ This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction. print ─ This method prints the fraction using fraction notation (1/4, 21/14, etc.) printAsDouble ─ This method prints the fraction as a double (0.25, 1.5, etc.) Separate accessor methods for each instance variable in the Fraction class. b) Modify the code so that it can handle negative numerators and negative driver public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); Fraction c, d, x; // Fraction objects System.out.println("Enter numerator; then denominator."); c = new Fraction(stdIn.nextInt(), stdIn.nextInt()); c.print(); System.out.println("Enter numerator; then denominator."); d = new Fraction(stdIn.nextInt(), stdIn.nextInt()); d.print(); x = new Fraction(); // create a fraction for number 0 System.out.println("Sum:"); x.add(c).add(d); x.print(); x.printAsDouble(); x = new Fraction(1, 1); // create a fraction for number 1 System.out.println("Product:"); x.multiply(c).multiply(d); x.print(); x.printAsDouble(); System.out.println("Enter numerator; then denominator."); x = new Fraction(stdIn.nextInt(), stdIn.nextInt()); x.printAsDouble(); } // end main Note that this demonstration driver does not call the accessor methods. That’s OK. Accessor methods are often implemented regardless of whether there’s an immediate need for them. That’s because they are very useful methods in general and providing them means that future code can use them when the need arises. what i have so far, I cant seem to fill int the blanks public class Fraction { private int numerator; private int denominator; public Fraction( int numerator, int denominator ) { this.numerator = numerator; //A Fraction cannot have a zero denominator if( denominator != 0 ) { this.denominator = denominator; } else { this.denominator = 1; } } public int getNumerator() { return numerator; } public int getDenominator() { return denominator; } //public Fraction add ( Fraction) { } public Fraction multiply( Fraction otherFraction ) { return new Fraction( otherFraction.getNumerator() * this.getNumerator(), otherFraction.getDenominator() * this.getDenominator() ); } public void print() { } public void printAsDouble() { } private void reduce() { } }
-
Answer:
public Fraction add ( Fraction A) { Fraction X=new Fraction(( numerator*A.denominator + A.numerator*denominator ), numerator*A.numerator ); X.reduce(); return X; } public Fraction multiply( Fraction otherFraction ) { return new Fraction( otherFraction.getNumerator() * this.getNumerator(), otherFraction.getDenominator() * this.getDenominator() ); } public void print() { System.out.println(numerator + "/"+denominator); } public void printAsDouble() { double x= (double)numerator/(double)denominator; System.out.println(x); } private void reduce() {//this is supposed to calculate GCF and divide both denominator and numerator with them int x=denominator < numerator? denominator:numerator; while(x>1) { if( (numerator%x==0) && (numerator%x==0)) break; x--; } //x is now GCF denominator /=x; numerator /=x; }
ddakota8... at Yahoo! Answers Visit the source
Related Q & A:
- what to do with fraction as index number?Best solution by Mathematics
- What are the fundamental concepts of OOP?Best solution by Programmers
- I need help with writing up the Java Code.Best solution by Yahoo! Answers
- I can't send email via windows mail. all settings for in/out mail are correct.help.Best solution by Yahoo! Answers
- What to wear at my first jazz dance class which is just tomorrow? help?Best solution by dancefullout.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.