Skip to content

Commit f48f47e

Browse files
committed
ch03 revisions
1 parent 01f9e08 commit f48f47e

File tree

4 files changed

+58
-16
lines changed

4 files changed

+58
-16
lines changed

ch03/Formatting.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Examples from the middle of the chapter.
3+
*/
4+
public class Formatting {
5+
6+
public static void main(String[] args) {
7+
final double CM_PER_INCH = 2.54;
8+
9+
System.out.println(System.out);
10+
11+
// formatting output
12+
13+
System.out.print(4.0 / 3.0);
14+
System.out.printf("Four thirds = %.3f", 4.0 / 3.0);
15+
16+
int inch = 100;
17+
double cm = inch * CM_PER_INCH;
18+
System.out.printf("%d in = %f cm\n", inch, cm);
19+
20+
// type cast operators
21+
22+
double pi = 3.14159;
23+
int xi = (int) pi;
24+
25+
double x = (int) pi * 20.0; // result is 60.0, not 62.0
26+
27+
inch = (int) (cm / CM_PER_INCH);
28+
System.out.printf("%f cm = %d in\n", cm, inch);
29+
30+
System.out.printf("inches = %d" + inch); // error
31+
}
32+
33+
}

ch03/Input.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

ch03/Literals.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* Converts inches to centimeters.
5+
*/
6+
public class Literals {
7+
8+
public static void main(String[] args) {
9+
int inch;
10+
double cm;
11+
Scanner in = new Scanner(System.in);
12+
13+
System.out.print("How many inches? ");
14+
inch = in.nextInt();
15+
16+
final double CM_PER_INCH = 2.54;
17+
cm = inch * CM_PER_INCH;
18+
System.out.print(inch + " in = ");
19+
System.out.println(cm + " cm");
20+
}
21+
22+
}

ch03/ScannerBug.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public static void main(String[] args) {
1616
age = in.nextInt();
1717
System.out.printf("Hello %s, age %d\n", name, age);
1818

19+
in.reset();
20+
System.out.println();
21+
1922
System.out.print("What is your age? ");
2023
age = in.nextInt();
2124
System.out.print("What is your name? ");

0 commit comments

Comments
 (0)