@@ -201,5 +201,4 @@
-
\ No newline at end of file
diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx
index a08b796..6c69c36 100644
--- a/source/ch3_javadatatypes.ptx
+++ b/source/ch3_javadatatypes.ptx
@@ -867,7 +867,7 @@ public class Histo {
-
+
for i in range(2, 102, 2)
@@ -904,7 +904,7 @@ public class Histo {
for (int x = 0, int y = 0; x < 10; x++, y += 2)
-
+
diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx
index 1f99114..623587a 100644
--- a/source/ch4_conditionals.ptx
+++ b/source/ch4_conditionals.ptx
@@ -5,12 +5,15 @@
Conditionals
Using Conditional Statements in Java
+
conditional statements
Conditional statements in Python and Java are very similar.
- In Python we have three patterns:
+ In Python we have three patterns.
-
+
+
+ Using the if Statement
shows how the simple if statement is written in Python.
@@ -50,7 +53,8 @@ if score >= 90: # Note the colon at the end of the line
In Java, the parentheses around the condition are required because it is technically a function that evaluates to True or False.
-
+
+
Using the if - else Statement
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index a610f37..d9656b0 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -172,6 +172,48 @@ public class CreateFile {
You may have noticed the use of another method from the File class; getName(). This method returns a string containing the name of the file.
+
+
+
+
+ Construct a short Java program that creates a File object for "myfile.txt"
+ and prints it. Drag the blocks into the correct order on the right.
+
+
+
+
+
+ import java.io.File;
+
+
+ import java.io.Scanner;
+
+
+
+
+ public class CreateFile {
+ public static void main(String[] args) {
+
+
+
+
+ File myFile = new File("myfile.txt");
+
+
+ File myFile = new File();
+
+
+
+
+ System.out.println(myFile);
+
+
+
+ }
+ }
+
+
+
@@ -245,6 +287,44 @@ public class CreateFile {
You may have noticed that there are some new methods you haven't seen yet. The hasNextLine() method checks if there is a next line in the file, and returns false if there isn't. This method allows us to iterate over every line till there is no next line. The nextLine() method of the Scanner object returns the next line in the file as a string.
+
+
+
+
+ Construct the part of a Java program that opens a file with a Scanner and
+ prints each line until there are no more lines. Drag the blocks into the correct order on the right.
+
+
+
+
+
+ try (Scanner fileReader = new Scanner(new File(filename))) {
+
+
+ try (Scanner fileReader = new Scanner(filename)) {
+
+
+
+
+
+ while (fileReader.hasNextLine()) {
+
+
+ while (fileReader.nextLine()) {
+
+
+
+
+ String data = fileReader.nextLine();
+ System.out.println(data);
+
+
+
+ }
+ }
+
+
+
diff --git a/source/ch9_commonmistakes.ptx b/source/ch9_commonmistakes.ptx
index 64b59cd..4dca391 100644
--- a/source/ch9_commonmistakes.ptx
+++ b/source/ch9_commonmistakes.ptx
@@ -115,6 +115,56 @@
The 'cannot find symbol' error for the variable count on line 6 indicates that count was used before it was declared within the Histo class. In Java, all variables must be explicitly declared with a data type (e.g., int, String, ArrayList<Integer>) before they can be assigned a value or referenced in any way. The arrow in the error message points to where the undeclared variable count was first encountered. To resolve this, count needs to be declared with its appropriate type (e.g., ArrayList<Integer> count;) before any attempt to initialize or use it.
+
+
+
+ Based on , select all of the
+ statements that are true about declaring variables in Java.
+
+
+
+
+
+ Every variable must be declared with a data type before it is used.
+
+
+ Correct! Java requires a variable to be declared with its type before it can be assigned or referenced.
+
+
+
+
+ The "cannot find symbol" error happens because count was used before being declared.
+
+
+ Correct! That error means the compiler reached a variable it has no declaration for.
+
+
+
+
+ Writing ArrayList<Integer> count; before using count would fix the "cannot find symbol" error.
+
+
+ Correct! Declaring count with its type resolves the error.
+
+
+
+
+ Java lets you use a variable without declaring it first, just like Python.
+
+
+ Incorrect. Unlike Python, Java requires all variables to be declared before use.
+
+
+
+
+ The error can be fixed by adding an import statement.
+
+
+ Incorrect. The import is already present; the problem is the missing variable declaration, not a missing import.
+
+
+
+