Question
The following class contains several errors that violate the rules of Java: 1. public class Fraction { 2. private int numerator; 3. private int denominator;
The following class contains several errors that violate the rules of Java:
1. public class Fraction {
2. private int numerator;
3. private int denominator;
4. public Fraction(int num, int denom) {
5. numerator = num;
6. denominator = denom;
7. reduce();
8. }
9. public Fraction(int num) {
10. this(num, 1);
11. }
12. public Fraction() {
13. this(0);
14. }
15. public int getdenominator() {
16. return denominator;
17. }
18. pulbic int getNumerator() {
19. return numerator;
20. }
21. public double toDoulbe() {
22. return (double) numerator / (double) denominator;
23. }
24. public float toFloat() {
25. return (float) numerator / denominator;
26. }
27. public string toString() {
28. if (denominator == 1)
29. return numerator + '";
30. else
31. return numerator + '/' + denominator;
32. }
33. void reduce() {
34. int g = gdc(numerator, denominator);
35. if(g!=0){
36. numerator /= g;
37. denominator /= g;
38. }
39. if (denominator < 0) {
40. numerator = -numerator;
41. denominator =- denominator;
42. }
43. }
44. private static int gcd(int m, int n) {
45. while (n != 0)
46. r = m % n;
47. m = n;
48. n = r;
49. }
50. return m;
51. }
52. }
Describe each error and specify whether it is (a) lexical, (b) syntactic, or (c) semantic. Use the numbers shown to identify the line on which each error occurs. The class may also contain programming errors that do not violate the rules of Java and will not be detected by a Java compiler. You should ignore these errors.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started