Advertisement
Guest User

Untitled

a guest
Apr 1st, 2018
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.43 KB | None | 0 0
  1. //The Rational class public class
  2.  
  3. public class Rational
  4. {
  5. private int n;
  6. private int d;
  7.  
  8. /**
  9. * The default constructor for objects of class Rational. Creates the rational number 1.
  10. */
  11. public Rational()
  12.  
  13. {
  14. n = 1;
  15. d = 1;
  16. }
  17.  
  18. /**
  19. * The alternate constructor for objects of class Rational. Creates a rational number equivalent to n/d.
  20. * @param n The numerator of the rational number.
  21. * @param d The denominator of the rational number.
  22. */
  23. public Rational(int n, int d)
  24. {
  25.  
  26. if(d == 0){
  27. throw new ZeroDenominatorException
  28. }
  29. this.n = n;
  30. this.d = d;
  31.  
  32.  
  33. /**
  34. * Get the value of the Numerator
  35. *
  36. * @return the value of the numerator
  37. */
  38. public int getNumerator()
  39. {
  40. return n;
  41. }
  42.  
  43. /**
  44. * Get the value of the Denominator
  45. *
  46. * @return the value of the denominator
  47. */
  48. public int getDenominator()
  49. {
  50. return d;
  51. }
  52.  
  53.  
  54. /**
  55. * Negate a rational number r
  56. *
  57. * @return a new rational number that is negation of this number -r
  58. */
  59. public Rational negate()
  60. {
  61. Rational r = new Rational (-this.n, this.d);
  62.  
  63. return r;
  64. }
  65.  
  66.  
  67. /**
  68. * Invert a rational number r
  69. *
  70. * @return a new rational number that is 1/r.
  71. */
  72. public Rational invert()
  73. {
  74. int newN = this.d;
  75. int newD = this.n;
  76. return new Rational (newN, newD);
  77.  
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84. /**
  85. * Add two rational numbers
  86. *
  87. * @param other the second argument of the add
  88. * @return a new rational number that is the sum of this and the other rational
  89. */
  90. public Rational add(Rational other)
  91. {
  92. int newN = (this.n*other.d+this.d*other.n);
  93. int newD = (this.d*other.d);
  94.  
  95. return new Rational (newN, newD);
  96.  
  97. }
  98.  
  99. /**
  100. * Subtract a rational number t from this one r
  101. *
  102. * @param other the second argument of subtract
  103. * @return a new rational number that is r-t
  104. */
  105. public Rational subtract(Rational other)
  106. {
  107. int newN = (this.n*other.d-this.d*other.n);
  108. int newD = (this.d*other.d);
  109.  
  110. return new Rational (newN, newD);
  111.  
  112. }
  113.  
  114. /**
  115. * Multiply two rational numbers
  116. *
  117. * @param other the second argument of multiply
  118. * @return a new rational number that is the sum of this object and the other rational.
  119. */
  120. public Rational multiply(Rational other)
  121. {
  122. int newN = (this.n*other.n);
  123. int newD = (this.d*other.d);
  124.  
  125.  
  126. return new Rational (newN, newD);
  127.  
  128.  
  129. }
  130.  
  131.  
  132. /**
  133. * Divide this rational number r by another one t
  134. *
  135. * @param other the second argument of divide
  136. * @return a new rational number that is r/t
  137. */
  138. public Rational divide(Rational other)
  139. {
  140. int newN = (this.n*other.d);
  141. int newD = (this.d*other.n);
  142.  
  143.  
  144. return new Rational(newN, newD);
  145.  
  146. }
  147.  
  148.  
  149.  
  150. /**
  151. * Put the rational number in normal form where the numerator
  152. * and the denominator share no common factors. Guarantee that only the numerator
  153. * is negative.
  154. *
  155. */
  156. private void normalize()
  157. {
  158. int absN = Math.abs(n);
  159. int absD= Math.abs(d);
  160. int signD = d/absD;
  161.  
  162. int GCD = gcd(absN, absD);
  163. n = (n / GCD) * signD;
  164. d = (d / GCD) * signD;
  165.  
  166. }
  167.  
  168. /**
  169. * Recursively compute the greatest common divisor of two positive integers
  170. *
  171. * @param a the first argument of gcd
  172. * @param b the second argument of gcd
  173. * @return the gcd of the two arguments
  174. */
  175. private int gcd(int a, int b)
  176. {
  177. int result = 0;
  178. if(a<b)
  179. result = gcd(b,a);
  180. else if(b==0)
  181. result = a;
  182. else
  183. {
  184. int remainder = a % b;
  185. result = gcd(b, remainder);
  186. }
  187. return result;
  188. }
  189. }
  190.  
  191. -------------------------------------------------------------------------------------------------------------------------------------
  192. public class ZeroDenominatorException extends RuntimeException
  193. {
  194.  
  195. public ZeroDenominatorException(String reason)
  196. {
  197. super(reason);
  198. }
  199. }
  200.  
  201.  
  202.  
  203.  
  204. ---------------------------------------------------------------------------------------------------------------------------------------
  205.  
  206. public class RationalTest
  207. {
  208. public static void main (String args[])
  209. {
  210. testConstructor();
  211. testNegate();
  212. testInvert();
  213. testAddSubtract();
  214. testMultiplyDivide();
  215. }
  216.  
  217.  
  218. public static void testConstructor()
  219. {
  220. System.out.println("TESTING the constructor, getNumerator, getDenominator");
  221.  
  222. System.out.println("Trying default constructor");
  223. Rational r0 = new Rational();
  224.  
  225. if(r0.getNumerator() == 1)
  226. {
  227. System.out.println(" Passes");
  228. }
  229. else
  230. {
  231. System.out.println("**** Fails - numerator not 1");
  232. }
  233.  
  234. if(r0.getDenominator() == 1)
  235. {
  236. System.out.println(" Passes");
  237. }
  238. else
  239. {
  240. System.out.println("**** Fails - denominator not 1");
  241. }
  242.  
  243. System.out.println("Constructing 2/5");
  244. try{
  245. Rational r1 = new Rational(2, 5);
  246. System.out.println(" Passes");
  247.  
  248. if(r1.getNumerator() == 2)
  249. {
  250. System.out.println(" Passes");
  251. }
  252. else
  253. {
  254. System.out.println("**** Fails - numerator not 2");
  255. }
  256.  
  257.  
  258. if(r1.getDenominator() == 5)
  259. {
  260. System.out.println(" Passes");
  261. }
  262. else
  263. {
  264. System.out.println("**** Fails - denominator not 5");
  265. }
  266. }
  267. catch(ZeroDenominatorException e)
  268. {
  269. System.out.println("**** Fails - exception thrown");
  270. }
  271.  
  272.  
  273. System.out.println("Trying 2/0");
  274. try{
  275. Rational r1 = new Rational(2, 0);
  276. System.out.println("**** Fails - no exception thrown");
  277. }
  278. catch(ZeroDenominatorException e)
  279. {
  280. System.out.println(" Passes");
  281. }
  282.  
  283.  
  284. System.out.println("Trying 42/30");
  285. Rational r2 = new Rational(42, 30);
  286.  
  287. if(r2.getNumerator() == 7)
  288. {
  289. System.out.println(" Passes");
  290. }
  291. else
  292. {
  293. System.out.println("**** Fails - numerator not 7");
  294. }
  295.  
  296. if(r2.getDenominator() == 5)
  297. {
  298. System.out.println(" Passes");
  299. }
  300. else
  301. {
  302. System.out.println("**** Fails - denominator not 5");
  303. }
  304.  
  305.  
  306.  
  307. System.out.println("Trying 6/-3");
  308. Rational r3 = new Rational(6, -3);
  309.  
  310. if(r3.getNumerator() == -2)
  311. {
  312. System.out.println(" Passes");
  313. }
  314. else
  315. {
  316. System.out.println("**** Fails - numerator not -2");
  317. }
  318.  
  319. if(r3.getDenominator() == 1)
  320. {
  321. System.out.println(" Passes");
  322. }
  323. else
  324. {
  325. System.out.println("**** Fails - denominator not 1");
  326. }
  327.  
  328.  
  329. System.out.println("Trying -6/-3");
  330. Rational r4 = new Rational(-6, -3);
  331.  
  332. if(r4.getNumerator() == 2)
  333. {
  334. System.out.println(" Passes");
  335. }
  336. else
  337. {
  338. System.out.println("**** Fails - numerator not 2");
  339. }
  340.  
  341. if(r4.getDenominator() == 1)
  342. {
  343. System.out.println(" Passes");
  344. }
  345. else
  346. {
  347. System.out.println("**** Fails - denominator not 1");
  348. }
  349.  
  350. System.out.println("Trying -6/3");
  351. Rational r5 = new Rational(-6, 3);
  352.  
  353. if(r5.getNumerator() == -2)
  354. {
  355. System.out.println(" Passes");
  356. }
  357. else
  358. {
  359. System.out.println("**** Fails - numerator not -2");
  360. }
  361.  
  362. if(r5.getDenominator() == 1)
  363. {
  364. System.out.println(" Passes");
  365. }
  366. else
  367. {
  368. System.out.println("**** Fails - denominator not 1");
  369. }
  370.  
  371. System.out.println("Trying 0/3");
  372. Rational r6 = new Rational(0, 3);
  373.  
  374. if(r6.getNumerator() == 0)
  375. {
  376. System.out.println(" Passes");
  377. }
  378. else
  379. {
  380. System.out.println("**** Fails - numerator not 0");
  381. }
  382.  
  383. if(r6.getDenominator() == 1)
  384. {
  385. System.out.println(" Passes");
  386. }
  387. else
  388. {
  389. System.out.println("**** Fails - denominator not 1");
  390. }
  391. System.out.println("Constructor tests finished");
  392.  
  393. }
  394.  
  395.  
  396. public static void testNegate()
  397. {
  398. System.out.println();
  399. System.out.println();
  400. System.out.println("TESTING the negate method");
  401.  
  402. System.out.println("Negate 1/2");
  403. Rational r1 = new Rational(1, 2);
  404. Rational r2 = r1.negate();
  405.  
  406. if(r1.getNumerator() == 1)
  407. {
  408. System.out.println(" Passes");
  409. }
  410. else
  411. {
  412. System.out.println("**** Fails - changed numerator of negate argument");
  413. }
  414.  
  415. if(r1.getDenominator() == 2)
  416. {
  417. System.out.println(" Passes");
  418. }
  419. else
  420. {
  421. System.out.println("**** Fails - changed denominator of negate argument");
  422. }
  423.  
  424. if(r2.getNumerator() == -1)
  425. {
  426. System.out.println(" Passes");
  427. }
  428. else
  429. {
  430. System.out.println("**** Fails - numerator not -1");
  431. }
  432. if(r2.getDenominator() == 2)
  433. {
  434. System.out.println(" Passes");
  435. }
  436. else
  437. {
  438. System.out.println("**** Fails - denominator not 2");
  439. }
  440.  
  441.  
  442. System.out.println("Negate -2/3");
  443. r1 = new Rational(-2, 3);
  444. r2 = r1.negate();
  445.  
  446. if(r1.getNumerator() == -2)
  447. {
  448. System.out.println(" Passes");
  449. }
  450. else
  451. {
  452. System.out.println("**** Fails - changed numerator of negate argument");
  453. }
  454.  
  455. if(r1.getDenominator() == 3)
  456. {
  457. System.out.println(" Passes");
  458. }
  459. else
  460. {
  461. System.out.println("**** Fails - changed denominator of negate argument");
  462. }
  463.  
  464. if(r2.getNumerator() == 2)
  465. {
  466. System.out.println(" Passes");
  467. }
  468. else
  469. {
  470. System.out.println("**** Fails - numerator not 2");
  471. }
  472.  
  473. if(r2.getDenominator() == 3)
  474. {
  475. System.out.println(" Passes");
  476. }
  477. else
  478. {
  479. System.out.println("**** Fails - denominator not 3");
  480. }
  481. System.out.println("Negate tests finished");
  482. }
  483.  
  484.  
  485.  
  486. public static void testInvert()
  487. {
  488. System.out.println();
  489. System.out.println();
  490. System.out.println("TESTING the invert method");
  491.  
  492. System.out.println("Invert 1/2");
  493. Rational r1 = new Rational(1, 2);
  494. Rational r2 = r1.invert();
  495.  
  496. if(r1.getNumerator() == 1)
  497. {
  498. System.out.println(" Passes");
  499. }
  500. else
  501. {
  502. System.out.println("**** Fails - changed numerator of negate argument");
  503. }
  504.  
  505. if(r1.getDenominator() == 2)
  506. {
  507. System.out.println(" Passes");
  508. }
  509. else
  510. {
  511. System.out.println("**** Fails - changed denominator of negate argument");
  512. }
  513.  
  514. if(r2.getNumerator() == 2)
  515. {
  516. System.out.println(" Passes");
  517. }
  518. else
  519. {
  520. System.out.println("**** Fails - numerator not 2");
  521. }
  522.  
  523. if(r2.getDenominator() == 1)
  524. {
  525. System.out.println(" Passes");
  526. }
  527. else
  528. {
  529. System.out.println("**** Fails - denominator not 1");
  530. }
  531.  
  532.  
  533. System.out.println("Invert -2/3");
  534. r1 = new Rational(-2, 3);
  535. r2 = r1.invert();
  536.  
  537. if(r1.getNumerator() == -2)
  538. {
  539. System.out.println(" Passes");
  540. }
  541. else
  542. {
  543. System.out.println("**** Fails - changed numerator of negate argument");
  544. }
  545.  
  546. if(r1.getDenominator() == 3)
  547. {
  548. System.out.println(" Passes");
  549. }
  550. else
  551. {
  552. System.out.println("**** Fails - changed denominator of negate argument");
  553. }
  554.  
  555. if(r2.getNumerator() == -3)
  556. {
  557. System.out.println(" Passes");
  558. }
  559. else
  560. {
  561. System.out.println("**** Fails - numerator not -3");
  562. }
  563.  
  564. if(r2.getDenominator() == 2)
  565. {
  566. System.out.println(" Passes");
  567. }
  568. else
  569. {
  570. System.out.println("**** Fails - denominator not 2");
  571. }
  572.  
  573. System.out.println("Invert 0/5");
  574. r1 = new Rational(0, 5);
  575. try
  576. {
  577. r2 = r1.invert();
  578. System.out.println("**** Fails - did not throw zero denominator exception");
  579. }
  580. catch(ZeroDenominatorException e)
  581. {
  582. System.out.println(" Passes");
  583. }
  584. System.out.println("Invert tests finished");
  585. }
  586.  
  587. public static void testAddSubtract()
  588. {
  589. System.out.println();
  590. System.out.println();
  591. System.out.println("TESTING the add and subtract methods");
  592.  
  593. System.out.println("Adding 1/2 and 1/2");
  594. Rational r1 = new Rational(1, 2);
  595. Rational r2 = r1.add(r1);
  596.  
  597. if(r1.getNumerator() == 1)
  598. {
  599. System.out.println(" Passes");
  600. }
  601. else
  602. {
  603. System.out.println("**** Fails - changed numerator of add argument");
  604. }
  605.  
  606. if(r1.getDenominator() == 2)
  607. {
  608. System.out.println(" Passes");
  609. }
  610. else
  611. {
  612. System.out.println("**** Fails - changed denominator of add argument");
  613. }
  614.  
  615. if(r2.getNumerator() == 1)
  616. {
  617. System.out.println(" Passes");
  618. }
  619. else
  620. {
  621. System.out.println("**** Fails - numerator not 1");
  622. }
  623.  
  624. if(r2.getDenominator() == 1)
  625. {
  626. System.out.println(" Passes");
  627. }
  628. else
  629. {
  630. System.out.println("**** Fails - denominator not 1");
  631. }
  632.  
  633.  
  634. System.out.println("Adding 4/7 and 3/5");
  635. r1 = new Rational(4, 7);
  636. r2 = new Rational(3, 5);
  637. Rational r3 = r1.add(r2);
  638.  
  639. if(r1.getNumerator() == 4)
  640. {
  641. System.out.println(" Passes");
  642. }
  643. else
  644. {
  645. System.out.println("**** Fails - changed numerator of first add argument");
  646. }
  647.  
  648. if(r1.getDenominator() == 7)
  649. {
  650. System.out.println(" Passes");
  651. }
  652. else
  653. {
  654. System.out.println("**** Fails - changed denominator of first add argument");
  655. }
  656.  
  657.  
  658. if(r2.getNumerator() == 3)
  659. {
  660. System.out.println(" Passes");
  661. }
  662. else
  663. {
  664. System.out.println("**** Fails - changed numerator of second add argument");
  665. }
  666.  
  667. if(r2.getDenominator() == 5)
  668. {
  669. System.out.println(" Passes");
  670. }
  671. else
  672. {
  673. System.out.println("**** Fails - changed denominator of second add argument");
  674. }
  675.  
  676. if(r3.getNumerator() == 41)
  677. {
  678. System.out.println(" Passes");
  679. }
  680. else
  681. {
  682. System.out.println("**** Fails - numerator not 41");
  683. }
  684.  
  685. if(r3.getDenominator() == 35)
  686. {
  687. System.out.println(" Passes");
  688. }
  689. else
  690. {
  691. System.out.println("**** Fails - denominator not 35");
  692. }
  693.  
  694. System.out.println("Adding 1/2 and 1/6");
  695. r1 = new Rational(1, 2);
  696. r2 = new Rational(1, 6);
  697. r3 = r1.add(r2);
  698.  
  699. if(r3.getNumerator() == 2)
  700. {
  701. System.out.println(" Passes");
  702. }
  703. else
  704. {
  705. System.out.println("**** Fails - numerator not 2");
  706. }
  707.  
  708. if(r3.getDenominator() == 3)
  709. {
  710. System.out.println(" Passes");
  711. }
  712. else
  713. {
  714. System.out.println("**** Fails - denominator not 3");
  715. }
  716.  
  717.  
  718.  
  719. System.out.println("Subtracting 1/2 and 1/2");
  720. r1 = new Rational(1, 2);
  721. r2 = r1.subtract(r1);
  722.  
  723. if(r1.getNumerator() == 1)
  724. {
  725. System.out.println(" Passes");
  726. }
  727. else
  728. {
  729. System.out.println("**** Fails - changed numerator of subtract argument");
  730. }
  731.  
  732. if(r1.getDenominator() == 2)
  733. {
  734. System.out.println(" Passes");
  735. }
  736. else
  737. {
  738. System.out.println("**** Fails - changed denominator of subtract argument");
  739. }
  740.  
  741. if(r2.getNumerator() == 0)
  742. {
  743. System.out.println(" Passes");
  744. }
  745. else
  746. {
  747. System.out.println("**** Fails - numerator not 0");
  748. }
  749.  
  750. if(r2.getDenominator() == 1)
  751. {
  752. System.out.println(" Passes");
  753. }
  754. else
  755. {
  756. System.out.println("**** Fails - denominator not 1");
  757. }
  758.  
  759.  
  760. System.out.println("Subtracting 4/7 and 3/5");
  761. r1 = new Rational(4, 7);
  762. r2 = new Rational(3, 5);
  763. r3 = r1.subtract(r2);
  764.  
  765. if(r1.getNumerator() == 4)
  766. {
  767. System.out.println(" Passes");
  768. }
  769. else
  770. {
  771. System.out.println("**** Fails - changed numerator of first subtract argument");
  772. }
  773.  
  774. if(r1.getDenominator() == 7)
  775. {
  776. System.out.println(" Passes");
  777. }
  778. else
  779. {
  780. System.out.println("**** Fails - changed denominator of first subtract argument");
  781. }
  782.  
  783.  
  784. if(r2.getNumerator() == 3)
  785. {
  786. System.out.println(" Passes");
  787. }
  788. else
  789. {
  790. System.out.println("**** Fails - changed numerator of second subtract argument");
  791. }
  792.  
  793. if(r2.getDenominator() == 5)
  794. {
  795. System.out.println(" Passes");
  796. }
  797. else
  798. {
  799. System.out.println("**** Fails - changed denominator of second subtract argument");
  800. }
  801.  
  802. if(r3.getNumerator() == -1)
  803. {
  804. System.out.println(" Passes");
  805. }
  806. else
  807. {
  808. System.out.println("**** Fails - numerator not -1");
  809. }
  810.  
  811. if(r3.getDenominator() == 35)
  812. {
  813. System.out.println(" Passes");
  814. }
  815. else
  816. {
  817. System.out.println("**** Fails - denominator not 35");
  818. }
  819.  
  820. System.out.println("Subtracting 1/2 and 1/6");
  821. r1 = new Rational(1, 2);
  822. r2 = new Rational(1, 6);
  823. r3 = r1.subtract(r2);
  824.  
  825. if(r3.getNumerator() == 1)
  826. {
  827. System.out.println(" Passes");
  828. }
  829. else
  830. {
  831. System.out.println("**** Fails - numerator not 1");
  832. }
  833.  
  834. if(r3.getDenominator() == 3)
  835. {
  836. System.out.println(" Passes");
  837. }
  838. else
  839. {
  840. System.out.println("**** Fails - denominator not 3");
  841. }
  842. System.out.println("Add/Subtract tests finished");
  843. }
  844.  
  845.  
  846. public static void testMultiplyDivide()
  847. {
  848. System.out.println();
  849. System.out.println();
  850. System.out.println("TESTING the multiply and divide methods");
  851.  
  852. System.out.println("Multiply 1/2 and 1/2");
  853. Rational r1 = new Rational(1, 2);
  854. Rational r2 = r1.multiply(r1);
  855.  
  856. if(r1.getNumerator() == 1)
  857. {
  858. System.out.println(" Passes");
  859. }
  860. else
  861. {
  862. System.out.println("**** Fails - changed numerator of add argument");
  863. }
  864.  
  865. if(r1.getDenominator() == 2)
  866. {
  867. System.out.println(" Passes");
  868. }
  869. else
  870. {
  871. System.out.println("**** Fails - changed denominator of add argument");
  872. }
  873.  
  874. if(r2.getNumerator() == 1)
  875. {
  876. System.out.println(" Passes");
  877. }
  878. else
  879. {
  880. System.out.println("**** Fails - numerator not 1");
  881. }
  882.  
  883. if(r2.getDenominator() == 4)
  884. {
  885. System.out.println(" Passes");
  886. }
  887. else
  888. {
  889. System.out.println("**** Fails - denominator not 4");
  890. }
  891.  
  892.  
  893. System.out.println("Multiply 5/7 and 3/5");
  894. r1 = new Rational(5, 7);
  895. r2 = new Rational(3, 5);
  896. Rational r3 = r1.multiply(r2);
  897.  
  898. if(r1.getNumerator() == 5)
  899. {
  900. System.out.println(" Passes");
  901. }
  902. else
  903. {
  904. System.out.println("**** Fails - changed numerator of first add argument");
  905. }
  906.  
  907. if(r1.getDenominator() == 7)
  908. {
  909. System.out.println(" Passes");
  910. }
  911. else
  912. {
  913. System.out.println("**** Fails - changed denominator of first add argument");
  914. }
  915.  
  916.  
  917. if(r2.getNumerator() == 3)
  918. {
  919. System.out.println(" Passes");
  920. }
  921. else
  922. {
  923. System.out.println("**** Fails - changed numerator of second add argument");
  924. }
  925.  
  926. if(r2.getDenominator() == 5)
  927. {
  928. System.out.println(" Passes");
  929. }
  930. else
  931. {
  932. System.out.println("**** Fails - changed denominator of second add argument");
  933. }
  934.  
  935. if(r3.getNumerator() == 3)
  936. {
  937. System.out.println(" Passes");
  938. }
  939. else
  940. {
  941. System.out.println("**** Fails - numerator not 3");
  942. }
  943.  
  944. if(r3.getDenominator() == 7)
  945. {
  946. System.out.println(" Passes");
  947. }
  948. else
  949. {
  950. System.out.println("**** Fails - denominator not 7");
  951. }
  952.  
  953. System.out.println("Multiply 1/2 and 0/1");
  954. r1 = new Rational(1, 2);
  955. r2 = new Rational(0, 1);
  956. r3 = r1.multiply(r2);
  957.  
  958. if(r3.getNumerator() == 0)
  959. {
  960. System.out.println(" Passes");
  961. }
  962. else
  963. {
  964. System.out.println("**** Fails - numerator not 0");
  965. }
  966.  
  967. if(r3.getDenominator() == 1)
  968. {
  969. System.out.println(" Passes");
  970. }
  971. else
  972. {
  973. System.out.println("**** Fails - denominator not 1");
  974. }
  975.  
  976.  
  977.  
  978. System.out.println("Dividing 1/2 by 1/2");
  979. r1 = new Rational(1, 2);
  980. r2 = r1.divide(r1);
  981.  
  982. if(r1.getNumerator() == 1)
  983. {
  984. System.out.println(" Passes");
  985. }
  986. else
  987. {
  988. System.out.println("**** Fails - changed numerator of subtract argument");
  989. }
  990.  
  991. if(r1.getDenominator() == 2)
  992. {
  993. System.out.println(" Passes");
  994. }
  995. else
  996. {
  997. System.out.println("**** Fails - changed denominator of subtract argument");
  998. }
  999.  
  1000. if(r2.getNumerator() == 1)
  1001. {
  1002. System.out.println(" Passes");
  1003. }
  1004. else
  1005. {
  1006. System.out.println("**** Fails - numerator not 1");
  1007. }
  1008.  
  1009. if(r2.getDenominator() == 1)
  1010. {
  1011. System.out.println(" Passes");
  1012. }
  1013. else
  1014. {
  1015. System.out.println("**** Fails - denominator not 1");
  1016. }
  1017.  
  1018.  
  1019. System.out.println("Dividing 4/7 by 3/28");
  1020. r1 = new Rational(4, 7);
  1021. r2 = new Rational(3, 28);
  1022. r3 = r1.divide(r2);
  1023.  
  1024. if(r1.getNumerator() == 4)
  1025. {
  1026. System.out.println(" Passes");
  1027. }
  1028. else
  1029. {
  1030. System.out.println("**** Fails - changed numerator of first subtract argument");
  1031. }
  1032.  
  1033. if(r1.getDenominator() == 7)
  1034. {
  1035. System.out.println(" Passes");
  1036. }
  1037. else
  1038. {
  1039. System.out.println("**** Fails - changed denominator of first subtract argument");
  1040. }
  1041.  
  1042.  
  1043. if(r2.getNumerator() == 3)
  1044. {
  1045. System.out.println(" Passes");
  1046. }
  1047. else
  1048. {
  1049. System.out.println("**** Fails - changed numerator of second subtract argument");
  1050. }
  1051.  
  1052. if(r2.getDenominator() == 28)
  1053. {
  1054. System.out.println(" Passes");
  1055. }
  1056. else
  1057. {
  1058. System.out.println("**** Fails - changed denominator of second subtract argument");
  1059. }
  1060.  
  1061. if(r3.getNumerator() == 16)
  1062. {
  1063. System.out.println(" Passes");
  1064. }
  1065. else
  1066. {
  1067. System.out.println("**** Fails - numerator not 16");
  1068. }
  1069.  
  1070. if(r3.getDenominator() == 3)
  1071. {
  1072. System.out.println(" Passes");
  1073. }
  1074. else
  1075. {
  1076. System.out.println("**** Fails - denominator not 3");
  1077. }
  1078.  
  1079. System.out.println("Dividing 1/2 by 1/6");
  1080. r1 = new Rational(1, 2);
  1081. r2 = new Rational(1, 6);
  1082. r3 = r1.divide(r2);
  1083.  
  1084. if(r3.getNumerator() == 3)
  1085. {
  1086. System.out.println(" Passes");
  1087. }
  1088. else
  1089. {
  1090. System.out.println("**** Fails - numerator not 3");
  1091. }
  1092.  
  1093. if(r3.getDenominator() == 1)
  1094. {
  1095. System.out.println(" Passes");
  1096. }
  1097. else
  1098. {
  1099. System.out.println("**** Fails - denominator not 1");
  1100. }
  1101.  
  1102.  
  1103. System.out.println("Dividing 1/2 by 0/1");
  1104. r1 = new Rational(1, 2);
  1105. r2 = new Rational(0, 1);
  1106. try
  1107. {
  1108. r3 = r1.divide(r2);
  1109. System.out.println("**** Fails - did not throw zero denominator exception");
  1110. }
  1111. catch(ZeroDenominatorException e)
  1112. {
  1113. System.out.println(" Passes");
  1114. }
  1115. System.out.println("Multiply/Divide tests finished");
  1116. }
  1117.  
  1118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement