diff --git a/.classpath b/.classpath
new file mode 100644
index 0000000..085a2e0
--- /dev/null
+++ b/.classpath
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/.gitignore b/.gitignore
index 32858aa..a031ac2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,3 +10,4 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
+/bin/
diff --git a/.project b/.project
new file mode 100644
index 0000000..2f869f5
--- /dev/null
+++ b/.project
@@ -0,0 +1,17 @@
+
+
+ ThinkJavaCode
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/ch03/Convert.java b/ch03/Convert.java
index d31fe77..5a4d4f2 100644
--- a/ch03/Convert.java
+++ b/ch03/Convert.java
@@ -22,6 +22,9 @@ public static void main(String[] args) {
remainder = inches % IN_PER_FOOT;
System.out.printf("%.2f cm = %d ft, %d in\n",
cm, feet, remainder);
+
+ //close the scanner
+ in.close();
}
}
diff --git a/ch03/ConvertSeconds.java b/ch03/ConvertSeconds.java
new file mode 100644
index 0000000..ca7266e
--- /dev/null
+++ b/ch03/ConvertSeconds.java
@@ -0,0 +1,28 @@
+import java.util.Scanner;
+
+/**
+ * Converts number of seconds to hour, minutes, seconds.
+ */
+public class ConvertSeconds {
+ public static void main(String[] args) {
+
+ int enteredSeconds;
+ int numHours;
+ int numMinutes;
+ int numSeconds;
+
+ Scanner scannerIn = new Scanner(System.in);
+
+ System.out.print("How many seconds? ");
+ enteredSeconds = scannerIn.nextInt();
+
+ numHours = enteredSeconds / 3600;
+ numMinutes = (enteredSeconds % 3600) / 60;
+ numSeconds = enteredSeconds % 60;
+
+ System.out.printf("%d seconds is %d hour(s), %d minutes, and %d seconds",
+ enteredSeconds, numHours, numMinutes, numSeconds);
+ scannerIn.close();
+ }
+
+}
diff --git a/ch03/Degrees.java b/ch03/Degrees.java
new file mode 100644
index 0000000..9d81c66
--- /dev/null
+++ b/ch03/Degrees.java
@@ -0,0 +1,21 @@
+/**
+ * Converts a temperature from Celsius to Fahrenheit
+ */
+
+import java.util.Scanner;
+
+public class Degrees {
+ public static void main(String[] args) {
+ double tempCelsius = 0.0;
+
+ Scanner scannerIn = new Scanner(System.in);
+
+ System.out.print("What is the temperature in Celsius? ");
+ tempCelsius = scannerIn.nextDouble();
+
+ System.out.printf("%.1f Celsius is %.1f Fahrenheit.", tempCelsius,
+ tempCelsius * 9.0 / 5.0 + 32 );
+ scannerIn.close();
+ }
+
+}
diff --git a/ch03/Echo.java b/ch03/Echo.java
index 500fd9f..59469a1 100644
--- a/ch03/Echo.java
+++ b/ch03/Echo.java
@@ -13,6 +13,9 @@ public static void main(String[] args) {
System.out.print("Type something else: ");
line = in.nextLine();
System.out.println("You also said: " + line);
+
+ // close the scanner
+ in.close();
}
}
diff --git a/ch03/GuessStarter.java b/ch03/GuessStarter.java
index 64984df..aeafa1f 100644
--- a/ch03/GuessStarter.java
+++ b/ch03/GuessStarter.java
@@ -1,15 +1,39 @@
-import java.util.Random;
-
/**
- * Starter code for the "Guess My Number" exercise.
+ * "Guess My Number" exercise.
*/
+
+import java.util.Random;
+import java.util.Scanner;
+
public class GuessStarter {
public static void main(String[] args) {
- // pick a random number
+
+ int difference;
+
+ // pick a random number
Random random = new Random();
int number = random.nextInt(100) + 1;
- System.out.println(number);
+
+ System.out.println("I'm thinking of a number between 1 and 100\n(including both)"
+ + ". Can you guess what it is?");
+ System.out.println("Type a number: ");
+
+ Scanner scannerIn = new Scanner(System.in);
+ int guess = scannerIn.nextInt();
+
+ System.out.println("Your guess is: " + guess);
+ System.out.println("The number I was thinking of is: " + number);
+
+ if (guess > number) {
+ difference = guess - number;
+ }
+ else {
+ difference = number - guess;
+ }
+ System.out.println("You were off by: " + difference);
+
+ scannerIn.close();
}
}
diff --git a/ch03/ScannerBug.java b/ch03/ScannerBug.java
index 353affb..8aa3738 100644
--- a/ch03/ScannerBug.java
+++ b/ch03/ScannerBug.java
@@ -18,9 +18,13 @@ public static void main(String[] args) {
System.out.print("What is your age? ");
age = in.nextInt();
+ in.nextLine(); // read the newline
System.out.print("What is your name? ");
name = in.nextLine();
System.out.printf("Hello %s, age %d\n", name, age);
+
+ // close the scanner
+ in.close();
}
}