Guest User

Untitled

a guest
Jan 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class Fraction {
  4. /* private for numerator and denominator so that they cannot be directly accessed outside Fraction */
  5. private int numerator;
  6. private int denominator;
  7. static int count = 0; //To get count of total Fractions in the programme, by default public (Even though not specified).
  8.  
  9. /* Default constructor that does not initialize this Fraction */
  10. public Fraction() {
  11.  
  12. }
  13.  
  14. public Fraction(int n , int d) {
  15. this.numerator = n;
  16. this.denominator = d;
  17. count++;
  18. }
  19.  
  20. /* To get the value of denominator outside Fraction */
  21. public int getDenominator() {
  22. return denominator;
  23. }
  24.  
  25. /* To get the value of numerator outside Fraction */
  26. public int getNumerator() {
  27. return numerator;
  28. }
  29.  
  30. /* To set the value of numerator from outside Fraction */
  31. public void setNumerator(int n) {
  32. this.numerator = n;
  33. }
  34.  
  35. /* To set the value of denominator from outside Fraction */
  36. public void setDenominator(int d) {
  37. this.denominator = d;
  38. }
  39.  
  40. /* Gets inherited by MixedFraction */
  41. public void printFraction() {
  42. System.out.println(numerator + "/" + denominator);
  43. }
  44.  
  45. public static void printCount() {
  46. System.out.println(count);
  47. }
  48.  
  49. public static Fraction add(Fraction a , Fraction b) {
  50. Fraction fraction = new Fraction();
  51. if (a.getDenominator() == b.getDenominator()) {
  52. fraction.setDenominator(a.getDenominator()); //Any one of a or b will work
  53. fraction.setNumerator(a.getNumerator() + b.getNumerator());
  54. }
  55. else {
  56. fraction.setDenominator(a.getDenominator() * b.getDenominator());
  57. fraction.setNumerator(a.getNumerator() * b.getDenominator() + b.getNumerator() * a.getDenominator());
  58. }
  59. return fraction;
  60. }
  61. }
  62.  
  63. class MixedFraction extends Fraction {
  64. private int wholenumber;
  65.  
  66. public MixedFraction(int n , int d , int w) {
  67. //super(n,d); This way we can initialize the Fraction inside the MixedFraction by simply calling the constructor of Fraction(n,d)
  68. this.wholenumber = w;
  69. setNumerator(n);
  70. setDenominator(d);
  71. }
  72.  
  73. // Overriden function. When you call printFraction() from object of MixedFraction, it will use only this one, and not the one set in Fraction.
  74. public void printFraction() {
  75. System.out.println(this.wholenumber + " " + getNumerator() + "/" + getDenominator());
  76. }
  77. }
  78.  
  79. public class FractionProgram {
  80. public static void main(String[] args) {
  81. int n , d , w;
  82. Scanner scan = new Scanner(System.in);
  83. System.out.println("Enter Numerator, Denominator and Whole Number: ");
  84. n = scan.nextInt();
  85. d = scan.nextInt();
  86. w = scan.nextInt();
  87. MixedFraction fraction = new MixedFraction(n , d, w);
  88. fraction.printFraction(); //Overriden function
  89. Fraction.add(new Fraction(4,5) , new Fraction(3,4)).printFraction(); //Takes two fractions 4/5 and 3/4, adds them and prints them. Static function
  90. }
  91. }
Add Comment
Please, Sign In to add comment