Advertisement
Crenox

Rational Java Program

Sep 22nd, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.00 KB | None | 0 0
  1. // By: Sammy Samkough
  2. // Rational
  3. // Spec: to create a menu driven client program that utilizes the Rational Class
  4.  
  5. public class Rational
  6. {
  7. private int numerator, denominator;
  8.  
  9. // Sets up the rational number by ensuring a nonzero denominator and making only the number signed
  10. public Rational(int n, int d)
  11. {
  12. if (d == 0)
  13. {
  14. d = 1;
  15. }
  16.  
  17. // Make the numerator "store" the sign
  18. if (d < 0)
  19. {
  20. n = n * -1;
  21. d = d * -1;
  22. }
  23.  
  24. numerator = n;
  25. denominator = d;
  26.  
  27. reduce();
  28. }
  29.  
  30. // sets the fraction to 1/1
  31. public Rational()
  32. {
  33. numerator = 1;
  34. denominator = 1;
  35. }
  36.  
  37. // returns the numerator of this rational number
  38. public int getNumerator()
  39. {
  40. return numerator;
  41. }
  42.  
  43. // sets the numerator of this rational number
  44. public void setNumerator(int n)
  45. {
  46. numerator = n;
  47. }
  48.  
  49. // returns the denominator of this rational number
  50. public int getDenominator()
  51. {
  52. return denominator;
  53. }
  54.  
  55. // sets the denominator of this rational number
  56. public void setDenominator(int d)
  57. {
  58. denominator = d;
  59. }
  60.  
  61. // sets the rational number
  62. public void setRational(int n, int d)
  63. {
  64. numerator = n;
  65. denominator = d;
  66. }
  67.  
  68. // returns the reciprocal
  69. public Rational reciprocal()
  70. {
  71. return new Rational (denominator, numerator);
  72. }
  73.  
  74. // adds the rational number to the one passed as a parameter. A common denominator is found by multiplying the individual
  75. // denominators
  76. public Rational add (Rational op2)
  77. {
  78. Rational result;
  79. int cd = denominator * op2.getDenominator();
  80. int n1 = numerator * op2.getDenominator();
  81. int n2 = op2.getNumerator() * denominator;
  82. int sum = n1 + n2;
  83. result = new Rational (sum, cd);
  84. result.reduce();
  85.  
  86. return result;
  87. }
  88.  
  89. // subtacts the rational number passed as a parameter from this rational number
  90. public Rational subtract (Rational op2)
  91. {
  92. Rational result;
  93. int cd = denominator * op2.getDenominator();
  94. int n1 = numerator * op2.getDenominator();
  95. int n2 = op2.getNumerator() * denominator;
  96. int difference = n1 - n2;
  97. result = new Rational (difference, cd);
  98. result.reduce();
  99.  
  100. return result;
  101. }
  102.  
  103. // multiplies this rational number by the one assed as a parameter
  104. public Rational multiply (Rational op2)
  105. {
  106. Rational result;
  107. int n = numerator * op2.getNumerator();
  108. int d = denominator * op2.getDenominator();
  109. result = new Rational (n, d);
  110. result.reduce();
  111.  
  112. return result;
  113. }
  114.  
  115. // divides this rational number by the one passed as a paremeter by multiplying by the recirprocal of the second rational
  116. public Rational divide (Rational op2)
  117. {
  118. return multiply (op2.reciprocal());
  119. }
  120.  
  121. // determines if this rational number is equal to the one passed as a parameter. Assumes they are both reduced
  122. public boolean equals (Rational op2)
  123. {
  124. return (numerator == op2.getNumerator() && denominator == op2.getDenominator());
  125. }
  126.  
  127. // returns this rational number as a string
  128. public String toString()
  129. {
  130. String result;
  131.  
  132. if (numerator == 0)
  133. {
  134. result = "0";
  135. }
  136. else
  137. {
  138. if (denominator == 1)
  139. {
  140. result = numerator + "";
  141. }
  142. else
  143. {
  144. result = numerator + "/" + denominator;
  145. }
  146. }
  147.  
  148. return result;
  149. }
  150.  
  151. // reduces this rational number by dividing both the numerator and the denominator by their greatest common divisor
  152. private void reduce()
  153. {
  154. if (numerator != 0)
  155. {
  156. int common = gcd(Math.abs(numerator), denominator);
  157.  
  158. numerator = numerator / common;
  159. denominator = denominator / common;
  160. }
  161. }
  162.  
  163. // computes and returns the greatest common divisor of the two positive parameters. Use Euclid's algorithm
  164. private int gcd (int n1, int n2)
  165. {
  166. while (n1 != n2)
  167. {
  168. if (n1 > n2)
  169. {
  170. n1 = n1 - n2;
  171. }
  172. else
  173. {
  174. n2 = n2 - 1;
  175. }
  176. }
  177.  
  178. return n1;
  179. }
  180.  
  181.  
  182. // calculates the exponent of this Rational and returns it as a double
  183. public double powDoub(Rational exp)
  184. {
  185. double a = exp.getNumerator() / exp.getDenominator();
  186. return (Math.pow(numerator, a) / Math.pow(denominator, a));
  187.  
  188. }
  189.  
  190. // caluculates the exponent of this Rational and returns it as a Rational
  191. public Rational powRat(int exp)
  192. {
  193. Rational result = new Rational();
  194.  
  195. numerator = (int)Math.pow(numerator, exp);
  196. denominator = (int)Math.pow(denominator, exp);
  197. result.setRational(numerator, denominator);
  198.  
  199. return result;
  200. }
  201. }
  202. -----------------------------------------------------------------------------------------------------------------------------
  203. // By: Sammy Samkough
  204. // Rational
  205. // Spec: to create a menu driven client program that utilizes the Rational Class
  206.  
  207. import java.util.Scanner;
  208.  
  209. public class RationalClient
  210. {
  211. public static void main(String args[])
  212. {
  213. Rational r1 = new Rational();
  214. Rational r2 = new Rational();
  215. Scanner sc = new Scanner(System.in);
  216.  
  217. // we create this so we can use getChoice()
  218. int choice;
  219. int n, d;
  220. boolean running = true;
  221.  
  222. // prompting the users
  223. System.out.println("* Hello and welcome to the Rational Program!");
  224. // telling the users to first put the numerator and then the denominator
  225. System.out.println("* When entering fractions, first put the numerator and then the denominator!");
  226. // explaining how to exit
  227. System.out.println("* And if you want to quit, just put in 8!");
  228. System.out.println("________________________________________________________________________________");
  229. // we initialize it to getChoice() so we can use it
  230. choice = getChoice();
  231.  
  232. // if we they enter in
  233. while(running)
  234. {
  235. // entering the fractions
  236. if(choice == 1)
  237. {
  238. System.out.println("Please enter your 1st fraction (numerator then denominator):");
  239. System.out.print("Numerator: ");
  240. n = sc.nextInt();
  241. System.out.print("Denominator: ");
  242. d = sc.nextInt();
  243. r1.setRational(n, d);
  244.  
  245. System.out.println("");
  246.  
  247. System.out.println("Please enter your 2nd fraction (numerator then denominator):");
  248. System.out.print("Numerator: ");
  249. n = sc.nextInt();
  250. System.out.print("Denominator: ");
  251. d = sc.nextInt();
  252. r2.setRational(n, d);
  253.  
  254. System.out.println("What would you like to do next (on the menu)?");
  255. choice = sc.nextInt();
  256. }
  257. // displaying the fractions
  258. else if (choice == 2)
  259. {
  260. System.out.print("Your 1st fraction is: " + r1 + "\n");
  261. System.out.print("Your 2nd fraction is: " + r2 + "\n");
  262.  
  263. System.out.println("What would you like to do next (on the menu)?");
  264. choice = sc.nextInt();
  265. }
  266. // adding the fractions
  267. else if (choice == 3)
  268. {
  269. System.out.println("The sum of the fractions are: " + r1.add(r2));
  270.  
  271. System.out.println("What would you like to do next (on the menu)?");
  272. choice = sc.nextInt();
  273. }
  274. // subtracting the fractions
  275. else if (choice == 4)
  276. {
  277. System.out.println("The difference of the fractions are: " + r1.subtract(r2));
  278.  
  279. System.out.println("What would you like to do next (on the menu)?");
  280. choice = sc.nextInt();
  281. }
  282. // multiply the fractions
  283. else if (choice == 5)
  284. {
  285. System.out.println("The product of the fractions are: " + r1.multiply(r2));
  286.  
  287. System.out.println("What would you like to do next (on the menu)?");
  288. choice = sc.nextInt();
  289. }
  290. // dividing the fractions
  291. else if (choice == 6)
  292. {
  293. System.out.println("The quotient of the fractions are: " + r1.divide(r2));
  294.  
  295. System.out.println("What would you like to do next (on the menu)?");
  296. choice = sc.nextInt();
  297. }
  298. // powering the fractions
  299. else if (choice == 7)
  300. {
  301. System.out.println("The power double of the fractions are: " + r1.powDoub(r2));
  302. System.out.println("What would you like the power to be for the power rational?");
  303. int p = sc.nextInt();
  304. System.out.println("The power rational of the fractions are: " + r1.powRat(p));
  305.  
  306. System.out.println("What would you like to do next (on the menu)?");
  307. choice = sc.nextInt();
  308. }
  309. // goodbye message
  310. else if (choice == 8)
  311. {
  312. System.out.println("Bye bye!");
  313. running = false;
  314. }
  315. // just in case they put something besides 1-8
  316. else
  317. {
  318. System.out.println("Please enter a number from 1-8.");
  319. choice = sc.nextInt();
  320. }
  321. }
  322. }
  323.  
  324. public static int getChoice()
  325. {
  326. int choice;
  327. Scanner sc = new Scanner(System.in);
  328.  
  329. System.out.println("1. Input Fractions");
  330. System.out.println("2. Display Fractions");
  331. System.out.println("3. Add Fractions");
  332. System.out.println("4. Subtract Fractions");
  333. System.out.println("5. Multiply Fractions");
  334. System.out.println("6. Divide Fractions");
  335. System.out.println("7. Power (A^B)");
  336. System.out.println("8. Quit");
  337.  
  338. System.out.print("Enter choice: ");
  339. choice = sc.nextInt();
  340.  
  341. return choice;
  342. }
  343.  
  344. }
  345. /*
  346. * Hello and welcome to the Rational Program!
  347. * When entering fractions, first put the numerator and then the denominator!
  348. * And if you want to quit, just put in 8!
  349. ________________________________________________________________________________
  350.  
  351. 1. Input Fractions
  352. 2. Display Fractions
  353. 3. Add Fractions
  354. 4. Subtract Fractions
  355. 5. Multiply Fractions
  356. 6. Divide Fractions
  357. 7. Power (A^B)
  358. 8. Quit
  359. Enter choice: 1
  360. Please enter your 1st fraction (numerator then denominator):
  361. Numerator: 1
  362. Denominator: 4
  363.  
  364. Please enter your 2nd fraction (numerator then denominator):
  365. Numerator: 5
  366. Denominator: 8
  367. What would you like to do next (on the menu)?
  368. 2
  369. Your 1st fraction is: 1/4
  370. Your 2nd fraction is: 5/8
  371. What would you like to do next (on the menu)?
  372. 3
  373. The sum of the fractions are: 1
  374. What would you like to do next (on the menu)?
  375. 4
  376. The difference of the fractions are: -1/2
  377. What would you like to do next (on the menu)?
  378. 5
  379. The product of the fractions are: 1/6
  380. What would you like to do next (on the menu)?
  381. 6
  382. The quotient of the fractions are: 1/2
  383. What would you like to do next (on the menu)?
  384. 7
  385. The power double of the fractions are: 1.0
  386. What would you like the power to be for the power rational?
  387. 8
  388. The power rational of the fractions are: 1/65536
  389. What would you like to do next (on the menu)?
  390. 8
  391. Bye bye!
  392. Press any key to continue . . .
  393. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement