Skip to content

Commit 5574265

Browse files
committed
Chapter 13 ex. 14 - readme update
1 parent 63c67f6 commit 5574265

File tree

3 files changed

+172
-14
lines changed

3 files changed

+172
-14
lines changed

README.md

+25-12
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,33 @@ _Please feel free to open new Issues._
2525
____
2626
# Quick Solution Navigator:
2727
<ul>
28-
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_01"><strong>Chapter 1</strong></a> - Introduction to Computers, Programs, and Java</li><br>
29-
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_02"><strong>Chapter 2</strong></a> - Elementary Programming</li><br>
30-
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_03"><strong>Chapter 3</strong></a> - Selections</li><br>
31-
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_04"><strong>Chapter 4</strong></a> - Mathematical Functions, Characters, and Strings</li><br>
32-
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_05"><strong>Chapter 5</strong></a> - Loops</li><br>
33-
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_06"><strong>Chapter 6</strong></a> - Methods</li><br>
34-
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_07"><strong>Chapter 7</strong></a> - Single-Dimensional Arrays</li><br>
35-
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_08"><strong>Chapter 8</strong></a> - MultiDimensional Arrays</li><br>
36-
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_09"><strong>Chapter 9</strong></a> - Objects and Classes</li><br>
37-
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_10"><strong>Chapter 10</strong></a> - Object-Oriented Thinking</li><br>
38-
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_11"><strong>Chapter 11</strong></a> - Inheritance and Polymorphism</li><br>
39-
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_12"><strong>Chapter 12</strong></a> - Exception Handling and Text I/O</li><br>
28+
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_01"><strong
29+
>Chapter 1</strong></a> - Introduction to Computers, Programs, and Java (Complete)</li><br>
30+
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_02"><strong
31+
>Chapter 2</strong></a> - Elementary Programming (Complete)</li><br>
32+
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_03"><strong
33+
>Chapter 3</strong></a> - Selections (Complete)</li><br>
34+
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_04"><strong
35+
>Chapter 4</strong></a> - Mathematical Functions, Characters, and Strings (Complete)</li><br>
36+
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_05"><strong
37+
>Chapter 5</strong></a> - Loops (Complete)</li><br>
38+
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_06"><strong
39+
>Chapter 6</strong></a> - Methods (Complete)</li><br>
40+
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_07"><strong
41+
>Chapter 7</strong></a> - Single-Dimensional Arrays (Complete)</li><br>
42+
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_08"><strong
43+
>Chapter 8</strong></a> - MultiDimensional Arrays (Complete)</li><br>
44+
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_09"><strong
45+
>Chapter 9</strong></a> - Objects and Classes (Complete)</li><br>
46+
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_10"><strong
47+
>Chapter 10</strong></a> - Object-Oriented Thinking (Complete)</li><br>
48+
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_11"><strong
49+
>Chapter 11</strong></a> - Inheritance and Polymorphism (Complete)</li><br>
50+
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_12"><strong
51+
>Chapter 12</strong></a> - Exception Handling and Text I/O (Complete)</li><br>
4052
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_13"><strong>Chapter 13</strong></a> - Abstract Classes and Interfaces</li><br>
4153
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_14"><strong>Chapter 14</strong></a> - JavaFx Basics</li><br>
54+
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_14"><strong>Chapter 14</strong></a> - JavaFx Basics</li><br>
4255
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_15"><strong>Chapter 15</strong></a> - Event-Driven Programming and Animations</li><br>
4356
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_16"><strong>Chapter
4457
16</strong></a> - JavaFx UI Controls and Multimedia</li><br>
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
package ch_13;
1+
package ch_13.exercise13_14;
22

33
/**
44
* 13.14 (Demonstrate the benefits of encapsulation) Rewrite the Rational class in
5-
* Listing 13.13 using a new internal representation for the numerator and denominator. Create an array of two integers as follows:
5+
* Listing 13.13 using a new internal representation for the numerator and denominator.
6+
* Create an array of two integers as follows:
67
* private long[] r = new long[2];
78
* Use r[0] to represent the numerator and r[1] to represent the denominator.
89
* The signatures of the methods in the Rational class are not changed, so a client
910
* application that uses the previous Rational class can continue to use this new
1011
* Rational class without being recompiled.
1112
*/
1213
public class Exercise13_14 {
14+
static Rational rational = new Rational();
15+
16+
public static void main(String[] args) {
17+
System.out.println("Using the Rational class..");
18+
System.out.println(rational.toString());
19+
}
1320
}
21+
22+

src/ch_13/exercise13_14/Rational.java

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package ch_13.exercise13_14;
2+
3+
public class Rational extends Number implements Comparable<Rational> {
4+
// Data fields for numerator and denominator
5+
private long numerator = 0;
6+
private long denominator = 1;
7+
8+
/**
9+
* Construct a rational with default properties
10+
*/
11+
public Rational() {
12+
this(0, 1);
13+
}
14+
15+
/**
16+
* Construct a rational with specified numerator and denominator
17+
*/
18+
public Rational(long numerator, long denominator) {
19+
long gcd = gcd(numerator, denominator);
20+
this.numerator = ((denominator > 0) ? 1 : -1) * numerator / gcd;
21+
this.denominator = Math.abs(denominator) / gcd;
22+
}
23+
24+
/**
25+
* Find GCD of two numbers
26+
*/
27+
private static long gcd(long n, long d) {
28+
long n1 = Math.abs(n);
29+
long n2 = Math.abs(d);
30+
int gcd = 1;
31+
for (int k = 1; k <= n1 && k <= n2; k++) {
32+
if (n1 % k == 0 && n2 % k == 0)
33+
gcd = k;
34+
}
35+
return gcd;
36+
37+
}
38+
39+
/**
40+
* Return numerator
41+
*/
42+
public long getNumerator() {
43+
return numerator;
44+
}
45+
46+
/**
47+
* Return denominator
48+
*/
49+
public long getDenominator() {
50+
return denominator;
51+
}
52+
53+
/**
54+
* Add a rational number to this rational
55+
*/
56+
public Rational add(Rational secondRational) {
57+
long n = numerator * secondRational.getDenominator() +
58+
denominator * secondRational.getNumerator();
59+
long d = denominator * secondRational.getDenominator();
60+
return new Rational(n, d);
61+
}
62+
63+
/**
64+
* Subtract a rational number from this rational
65+
*/
66+
public Rational subtract(Rational secondRational) {
67+
long n = numerator * secondRational.getDenominator()
68+
- denominator * secondRational.getNumerator();
69+
long d = denominator * secondRational.getDenominator();
70+
return new Rational(n, d);
71+
}
72+
73+
/**
74+
* Multiply a rational number by this rational
75+
*/
76+
public Rational multiply(Rational secondRational) {
77+
long n = numerator * secondRational.getNumerator();
78+
long d = denominator * secondRational.getDenominator();
79+
return new Rational(n, d);
80+
}
81+
82+
/**
83+
* Divide a rational number by this rational
84+
*/
85+
public Rational divide(Rational secondRational) {
86+
long n = numerator * secondRational.getDenominator();
87+
long d = denominator * secondRational.numerator;
88+
return new Rational(n, d);
89+
}
90+
91+
@Override
92+
public String toString() {
93+
if (denominator == 1)
94+
return numerator + "";
95+
else
96+
return numerator + "/" + denominator;
97+
}
98+
99+
@Override // Override the equals method in the Object class
100+
public boolean equals(Object other) {
101+
if ((this.subtract((Rational) (other))).getNumerator() == 0)
102+
return true;
103+
else
104+
return false;
105+
}
106+
107+
@Override // Implement the abstract intValue method in Number
108+
public int intValue() {
109+
return (int) doubleValue();
110+
}
111+
112+
@Override // Implement the abstract floatValue method in Number
113+
public float floatValue() {
114+
return (float) doubleValue();
115+
}
116+
117+
@Override // Implement the doubleValue method in Number
118+
public double doubleValue() {
119+
return numerator * 1.0 / denominator;
120+
}
121+
122+
@Override // Implement the abstract longValue method in Number
123+
public long longValue() {
124+
return (long) doubleValue();
125+
}
126+
127+
@Override // Implement the compareTo method in Comparable
128+
public int compareTo(Rational o) {
129+
if (this.subtract(o).getNumerator() > 0)
130+
return 1;
131+
else if (this.subtract(o).getNumerator() < 0)
132+
return -1;
133+
else
134+
return 0;
135+
}
136+
}

0 commit comments

Comments
 (0)