akosiraff

Download Fraction

Mar 4th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1.  
  2. Download: http://solutionzip.com/downloads/fraction/
  3. Write a driver and fraction class that performs addition, multiplication, prints the fraction, and prints as a double.
  4. Provide a driver class, LastNameFirstNameProg7, that demonstrates this Fraction class. The driver class should contain this main method:
  5. public static void main(String[] args)
  6. {
  7. Scanner stdIn = new Scanner(System.in);
  8. Fraction c, d, x; // Fraction objects
  9. System.out.println(“Enter numerator; then denominator.”);
  10. c = new Fraction(stdIn.nextInt(), stdIn.nextInt());
  11. c.print();
  12. System.out.println(“Enter numerator; then denominator.”);
  13. d = new Fraction(stdIn.nextInt(), stdIn.nextInt());
  14. d.print();
  15. x = new Fraction(); // create a fraction for number 0
  16. System.out.println(“Sum:”);
  17. x.add(c).add(d);
  18. x.print();
  19. x.printAsDouble();
  20. x = new Fraction(1, 1); // create a fraction for number 1
  21. System.out.println(“Product:”);
  22. x.multiply(c).multiply(d);
  23. x.print();
  24. x.printAsDouble();
  25. System.out.println(“Enter numerator; then denominator.”);
  26. x = new Fraction(stdIn.nextInt(), stdIn.nextInt());
  27. x.printAsDouble();
  28. } // end main
  29. 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.
  30. *****Fraction.java*****
  31. Write a Fraction class called Fraction.java that implements these methods:
  32. add – This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction.
  33. multiply – This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction.
  34. print – This method prints the fraction using fraction notation (1/4, 21/14, etc.)
  35. printAsDouble – This method prints the fraction as a double (0.25, 1.5, etc.)
  36. Separate accessor methods for each instance variable in the Fraction class.
  37. *****Sample Output*****
  38. Enter numerator; then denominator.
  39. 5
  40. 8
  41. 5/8
  42. Enter numerator; then denominator.
  43. 4
  44. 10
  45. 4/10
  46. Sum:
  47. 82/80
  48. 1.025
  49. Product:
  50. 20/80
  51. 0.25
  52. Enter numerator; then denominator.
  53. 6
  54. 0
  55. infinity
  56.  
  57. Download: http://solutionzip.com/downloads/fraction/
Add Comment
Please, Sign In to add comment