forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariables.java
More file actions
83 lines (59 loc) · 2.24 KB
/
Variables.java
File metadata and controls
83 lines (59 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Examples from Chapter 2.
*/
public class Variables {
public static void main(String[] args) {
String message;
int x;
String firstName;
String lastName;
int hour, minute;
message = "Hello!"; // give message the value "Hello!"
hour = 11; // assign the value 11 to hour
minute = 59; // set minute to 59
message = "123"; // legal
// message = 123; // not legal
String message2 = "Hello!";
int hour2 = 11;
int minute2 = 59;
int a = 5;
int b = a; // a and b are now equal
a = 3; // a and b are no longer equal
String firstLine = "Hello, again!";
System.out.println(firstLine);
System.out.print("The value of firstLine is ");
System.out.println(firstLine);
System.out.print("The current time is ");
System.out.print(hour);
System.out.print(":");
System.out.print(minute);
System.out.println(".");
System.out.print("Number of minutes since midnight: ");
System.out.println(hour * 60 + minute);
System.out.print("Fraction of the hour that has passed: ");
System.out.println(minute / 60);
System.out.print("Percent of the hour that has passed: ");
System.out.println(minute * 100 / 60);
double pi;
pi = 3.14159;
double minute3 = 59.0;
System.out.print("Fraction of the hour that has passed: ");
System.out.println(minute3 / 60.0);
double y = 1.0 / 3.0; // correct
System.out.println(0.1 * 10);
System.out.println(0.1 + 0.1 + 0.1 + 0.1 + 0.1
+ 0.1 + 0.1 + 0.1 + 0.1 + 0.1);
double balance = 123.45; // potential rounding error
int balance2 = 12345; // total number of cents
System.out.println(1 + 2 + "Hello");
// the output is 3Hello
System.out.println("Hello" + 1 + 2);
// the output is Hello12
System.out.println(17 * 3);
System.out.println(hour * 60 + minute);
int percentage;
percentage = (minute * 100) / 60;
hour = minute + 1; // correct
// minute + 1 = hour; // compiler error
}
}