Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f48f47e

Browse filesBrowse files
committed
ch03 revisions
1 parent 01f9e08 commit f48f47e
Copy full SHA for f48f47e

File tree

Expand file treeCollapse file tree

4 files changed

+58
-16
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+58
-16
lines changed
Open diff view settings
Collapse file

‎ch03/Formatting.java‎

Copy file name to clipboard
+33Lines 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+
}
Collapse file

‎ch03/Input.java‎

Copy file name to clipboardExpand all lines: ch03/Input.java
-16Lines changed: 0 additions & 16 deletions
This file was deleted.
Collapse file

‎ch03/Literals.java‎

Copy file name to clipboard
+22Lines 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+
}
Collapse file

‎ch03/ScannerBug.java‎

Copy file name to clipboardExpand all lines: ch03/ScannerBug.java
+3Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.