forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariables.java
More file actions
69 lines (50 loc) · 2.57 KB
/
Copy pathVariables.java
File metadata and controls
69 lines (50 loc) · 2.57 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
public class Variables {
public static void main(String[] args) {
String message;
int hour, minute;
// System.out.println(message); // not allowed, we cant use variables before anything is stored in them!
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
// We will improve this tomorrow!
System.out.print("The current time is ");
System.out.print(hour);
System.out.print(":");
System.out.print(minute);
System.out.println(".");
int a = 5;
int b = a; // a and b are now equal: a = 5, b = 5
a = 3; // a and b are no longer equal, a = 3, b = 5!
System.out.println(a);
System.out.println(b);
String firstLine = "Hello, again!";
System.out.println(firstLine);
System.out.println("The value of firstLine is " + firstLine);
System.out.println(1 + 2 + "Hello"); // the output is 3Hello
System.out.println("Hello" + 1 + 2); // the output is Hello12
System.out.print("Number of minutes since midnight: ");
System.out.println(hour * 60 + minute); // asumes hour is in the 24 hour format
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);
// using the modulo (%) operator to separate total inches to feet and inches
int totalInches = 76;
int feet = 76 / 12; // yields 6, remember that integer division rounds down!
int inches = 76 % 12; // yields 4 because 76 = 6 * 12 + 4. In other words, 4 is the remainder when dividing
System.out.println("In " + totalInches + " inches there are " + feet + " feet with " + inches + " inches left over.");
double pi;
pi = 3.14159;
double minuteDouble = 59.0;
System.out.print("Fraction of the hour that has passed: ");
System.out.println(minuteDouble / 60.0);
// double y = 1 / 3; // incorrect
double y = 1.0 / 3.0; // correct
// these next two print lines should produce the same output, but they don't becase of the rounding errors in floating point numbers
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);
}
}