Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8e04a1f

Browse files
committedAug 4, 2017
added appendix files
1 parent 7d41dad commit 8e04a1f

File tree

8 files changed

+129
-1
lines changed

8 files changed

+129
-1
lines changed
 

‎ap01/Series.java renamed to ‎appa/Series.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Example method from Chapter 6.
2+
* Example method from Chapter 8.
33
*/
44
public class Series {
55

File renamed without changes.

‎appb/Convert.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2017 Allen Downey and Chris Mayfield
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
import java.util.Scanner;
24+
25+
/**
26+
* Utility class for converting to/from the metric system.
27+
*
28+
* @author Allen Downey
29+
* @author Chris Mayfield
30+
* @version 6.1.5
31+
*/
32+
public class Convert {
33+
34+
public static final double CM_PER_INCH = 2.54;
35+
36+
public static final int IN_PER_FOOT = 12;
37+
38+
/**
39+
* Converts a measurement in centimeters to inches.
40+
*
41+
* @param cm length in centimeters
42+
* @return length in inches
43+
*/
44+
public static double toImperial(double cm) {
45+
return cm / CM_PER_INCH;
46+
}
47+
48+
/**
49+
* Converts a length in feet and inches to centimeters.
50+
*
51+
* @param feet how many feet
52+
* @param inches how many inches
53+
* @return length in centimeters
54+
*/
55+
public static double toMetric(int feet, int inches) {
56+
int total = feet * IN_PER_FOOT + inches;
57+
return total * CM_PER_INCH;
58+
}
59+
60+
/**
61+
* Tests the conversion methods.
62+
*
63+
* @param args command-line arguments
64+
*/
65+
public static void main(String[] args) {
66+
double cm, result;
67+
int feet, inches;
68+
Scanner in = new Scanner(System.in);
69+
70+
// test the Imperial conversion
71+
System.out.print("Exactly how many cm? ");
72+
cm = in.nextDouble();
73+
result = toImperial(cm);
74+
System.out.printf("That's %.2f inches\n", result);
75+
System.out.println();
76+
77+
// test the Metric conversion
78+
System.out.print("Now how many feet? ");
79+
feet = in.nextInt();
80+
System.out.print("And how many inches? ");
81+
inches = in.nextInt();
82+
result = toMetric(feet, inches);
83+
System.out.printf("That's %.2f cm\n", result);
84+
}
85+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

‎appd/Complex.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.awt.Rectangle;
2+
3+
/**
4+
* Example from "My method doesn't return what I expect."
5+
*/
6+
public class Complex {
7+
8+
public static Rectangle intersection(Rectangle a, Rectangle b) {
9+
int x1 = Math.min(a.x, b.x);
10+
int y1 = Math.min(a.y, b.y);
11+
int x2 = Math.max(a.x + a.width, b.x + b.width);
12+
int y2 = Math.max(a.y + a.height, b.y + b.height);
13+
Rectangle rect = new Rectangle(x1, y1, x2 - x1, y2 - y1);
14+
return rect;
15+
}
16+
17+
public static void main(String[] args) {
18+
Rectangle x = new Rectangle(0, 0, 10, 10);
19+
Rectangle y = new Rectangle(-5, -5, -5, -5);
20+
System.out.println(intersection(x, y));
21+
}
22+
23+
}

‎appd/Infinite.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Infinite loop example.
3+
*/
4+
public class Infinite {
5+
6+
public static void main(String[] args) {
7+
int x = 1;
8+
int y = -1;
9+
10+
while (x > 0 && y < 0) {
11+
// do something to x
12+
// do something to y
13+
14+
System.out.println("x: " + x);
15+
System.out.println("y: " + y);
16+
System.out.println("condition: " + (x > 0 && y < 0));
17+
}
18+
}
19+
20+
}

0 commit comments

Comments
 (0)
Please sign in to comment.