Skip to content

Commit b170694

Browse files
committed
ch 13 Ex: 17
1 parent fa5389c commit b170694

File tree

4 files changed

+154
-12
lines changed

4 files changed

+154
-12
lines changed

src/ch_13/exercise13_16/Exercise13_16.java

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package ch_13.exercise13_16;
22

3+
import java.util.Map;
4+
import java.util.Set;
5+
36
/**
47
* 13.16 (Create a rational-number calculator) Write a program similar to Listing 7.9,
58
* Calculator.java. Instead of using integers, use rationals, as shown in Figure 13.10a.
@@ -11,5 +14,8 @@
1114
public class Exercise13_16 {
1215
public static void main(String[] args) {
1316
System.out.println("See RationalNumCalculator.java....");
17+
System.out.println("javac RationalNumCalculator.java");
18+
System.out.print("Usage: java RationalNumCalculator \"11/453 +-*/ 7/44\" ");
19+
System.out.print(" or: java RationalNumCalculator \"112 +-*/ 9/17\" ");
1420
}
1521
}

src/ch_13/exercise13_16/RationalNumCalculator.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
* Section 10.10.3, Replacing and Splitting Strings, to retrieve the numerator string and
1212
* denominator string, and convert strings into integers using the Integer.parseInt
1313
* method.
14-
*
15-
* Usage: java RationalNumCalculator 11/453 * 7/44
14+
* <p>
15+
* Usage: java RationalNumCalculator "1/5 * 7/8"
1616
*/
1717
public class RationalNumCalculator {
1818

1919
public static void main(String[] args) {
2020

2121
if (args.length != 3) {
2222
System.out.println(
23-
"Usage: java RNumCalculator operand1 operator operand2");
23+
"Usage: java RNumCalculator \"operand1 operator operand2\" ");
2424
System.exit(0);
2525
}
2626
if (args[0].contains("/") || args[2].contains("/")) {

src/ch_13/exercise13_17/Complex.java

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package ch_13.exercise13_17;
2+
3+
/**
4+
* 13.17 (Math: The Complex class) A complex number is a number in the form a + bi,
5+
* -where a and b are real numbers and i is SQRT(-1). The numbers a and b are known
6+
* as the real part and imaginary part of the complex number, respectively. You can
7+
* perform addition, subtraction, multiplication, and division for complex numbers
8+
* using the following formulas:
9+
* a + bi + c + di = (a + c) + (b + d)i
10+
* a + bi - (c + di) = (a - c) + (b - d)i
11+
* (a + bi)*(c + di) = (ac - bd) + (bc + ad)i
12+
* (a + bi)/(c + di) = (ac + bd)/(c2 + d2) + (bc - ad)i/(c2 + d2)
13+
* <p>
14+
* You can also obtain the absolute value for a complex number using the following
15+
* formula:
16+
* |a + bi| = SQRT(a2 + b2)
17+
* (A complex number can be interpreted as a point on a plane by identifying the (a,b)
18+
* values as the coordinates of the point. The absolute value of the complex number
19+
* corresponds to the distance of the point to the origin, as shown in Figure 13.10b.)
20+
* <p>
21+
* Design a class named Complex for representing complex numbers and the
22+
* methods add, subtract, multiply, divide, and abs for performing complex number operations,
23+
* and override toString method for returning a string representation for a complex number.
24+
* The toString method returns (a + bi) as a string. If b is 0, it simply returns a.
25+
* Your Complex class should also implement the Cloneable interface.
26+
* <p>
27+
* Provide three constructors Complex(a, b), Complex(a), and Complex().
28+
* Complex() creates a Complex object for number 0 and Complex(a) creates a Complex object with 0 for b.
29+
* Also provide the getRealPart() and getImaginaryPart() methods for returning the real and imaginary part
30+
* of the complex number, respectively.
31+
*/
32+
public class Complex extends Number implements Cloneable {
33+
34+
private double a;
35+
private double b;
36+
private static double i = Math.sqrt(-1);
37+
38+
public Complex() {
39+
this(0);
40+
}
41+
42+
public Complex(double a) {
43+
this(a, 0);
44+
}
45+
46+
public Complex(double a, double b) {
47+
this.a = a;
48+
this.b = b;
49+
}
50+
51+
/**
52+
* Example addition: (a + bi) + (c + di) = (a + c) + (b + d)i
53+
*
54+
* @param complex Complex number to add
55+
* @return the addition result
56+
*/
57+
public double add(Complex complex) {
58+
return (this.a + complex.a) + (this.b + complex.b) * i;
59+
}
60+
61+
/**
62+
* Subtract example:
63+
* a + bi - (c + di) = (a - c) + (b - d)i
64+
*
65+
* @param complex
66+
* @return
67+
*/
68+
public double subtract(Complex complex) {
69+
return (this.a - complex.a) + (this.b - complex.b) * i;
70+
71+
}
72+
73+
/**
74+
* (a + bi)*(c + di) = (ac - bd) + (bc + ad)i
75+
*
76+
* @param complex
77+
* @return
78+
*/
79+
public double multiply(Complex complex) {
80+
return (this.a * complex.a - this.b * complex.b) + ((this.b * complex.a + this.a * complex.b) * i);
81+
}
82+
83+
/**
84+
* (a + bi)/(c + di) = (ac + bd)/(c2 + d2) + (bc - ad)i/(c2 + d2)
85+
*
86+
* @param complex
87+
* @return
88+
*/
89+
public double divide(Complex complex) {
90+
return (this.a * complex.a + this.b * complex.b) / (Math.pow(complex.a, 2) + Math.pow(complex.b, 2)) + ((this.b * complex.a - this.a * complex.b) * i) / (Math.pow(complex.a, 2) + Math.pow(complex.b, 2));
91+
}
92+
93+
public double abs(Complex complex) {
94+
return Math.sqrt(Math.pow(this.a, 2) + Math.pow(this.b, 2));
95+
}
96+
97+
public double getRealPart() {
98+
return a;
99+
}
100+
101+
public double getImaginaryPart() {
102+
return b * i;
103+
}
104+
105+
@Override
106+
public Complex clone() throws CloneNotSupportedException {
107+
return (Complex) super.clone();
108+
109+
}
110+
111+
@Override
112+
public int intValue() {
113+
return (int) Math.round(a + b * i);
114+
}
115+
116+
@Override
117+
public long longValue() {
118+
return Math.round(a + b * i);
119+
}
120+
121+
@Override
122+
public float floatValue() {
123+
return (float) (a + b * i);
124+
}
125+
126+
@Override
127+
public double doubleValue() {
128+
return a + b * i;
129+
}
130+
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
package ch_13;
1+
package ch_13.exercise13_17;
22

33
/**
44
* 13.17 (Math: The Complex class) A complex number is a number in the form a + bi,
5-
* where a and b are real numbers and i is 2-1. The numbers a and b are known
5+
* where a and b are real numbers and i is SQRT(-1). The numbers a and b are known
66
* as the real part and imaginary part of the complex number, respectively. You can
77
* perform addition, subtraction, multiplication, and division for complex numbers
88
* using the following formulas:
9-
* a + bi + c + di = (a + c) + (b + d)i
10-
* a + bi - (c + di) = (a - c) + (b - d)i
11-
* (a + bi)*(c + di) = (ac - bd) + (bc + ad)i
12-
* (a + bi)/(c + di) = (ac + bd)/(c2 + d2) + (bc - ad)i/(c2 + d2)
9+
* a + bi + c + di = (a + c) + (b + d)i
10+
* a + bi - (c + di) = (a - c) + (b - d)i
11+
* (a + bi)*(c + di) = (ac - bd) + (bc + ad)i
12+
* (a + bi)/(c + di) = (ac + bd)/(c2 + d2) + (bc - ad)i/(c2 + d2)
1313
* <p>
1414
* You can also obtain the absolute value for a complex number using the following
1515
* formula:
16-
* a + bi = 2a2 + b2
16+
* a + bi = 2a2 + b2
1717
* (A complex number can be interpreted as a point on a plane by identifying the (a,b)
1818
* values as the coordinates of the point. The absolute value of the complex number
1919
* corresponds to the distance of the point to the origin, as shown in Figure 13.10b.)
@@ -26,9 +26,12 @@
2626
* Provide three constructors Complex(a, b), Complex(a), and Complex().
2727
* Complex() creates a Complex object for number 0 and Complex(a) creates a Complex object with 0 for b.
2828
* Also provide the getRealPart() and getImaginaryPart() methods for returning the real and imaginary part
29-
* of the complex number, respectively. Write a test program that prompts the user to enter two complex numbers and
29+
* of the complex number, respectively.
30+
* <p>
31+
* <p>
32+
* Write a test program that prompts the user to enter two complex numbers and
3033
* displays the result of their addition, subtraction, multiplication, division, and absolute value.
31-
*
34+
* <p>
3235
* Here is a sample run:
3336
* Enter the first complex number: 3.5 5.5
3437
* Enter the second complex number: -3.5 1
@@ -39,4 +42,7 @@
3942
* |(3.5 + 5.5i)| = 6.519202405202649
4043
*/
4144
public class Exercise13_17 {
45+
public static void main(String[] args) {
46+
System.out.println("See -> exercise13_17.Complex.class");
47+
}
4248
}

0 commit comments

Comments
 (0)