diff --git a/source/ch2_firstjavaprogram.ptx b/source/ch2_firstjavaprogram.ptx index 7f1669d..035df2c 100644 --- a/source/ch2_firstjavaprogram.ptx +++ b/source/ch2_firstjavaprogram.ptx @@ -81,7 +81,7 @@ $ ls -l Hello.*

The command javac compiles our java source code into compiled byte code and saves it in a file called Hello.class. Hello.class is a binary file so you won’t learn much if you try to examine the class file with an editor. - Hopefully you didn’t make any mistakes, but if you did you may want to consult the section for helpful hints on compiler errors. + Hopefully you didn’t make any mistakes, but if you did you may want to consult the section for helpful hints on compiler errors.

@@ -374,7 +374,7 @@ Hello World!

Summary & Reading Questions -

    +

    1. Java programs must be compiled before execution, unlike Python which is interpreted directly.

    2. @@ -398,7 +398,7 @@ Hello World!

    - +

    Which of the following must be true of every Java program?

    @@ -421,7 +421,7 @@ Hello World!
    - +

    What is the purpose of the javac command?

    @@ -444,7 +444,7 @@ Hello World!
    - +

    What symbol does Java use to indicate the end of a statement?

    diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx index 6c69c36..a1943ff 100644 --- a/source/ch3_javadatatypes.ptx +++ b/source/ch3_javadatatypes.ptx @@ -7,6 +7,8 @@
    Numeric + + Primitive and Object Data Types

    One of the great things about Python is that all of the basic data types are objects. Integers are objects, floating point numbers are objects, lists are objects, everything. @@ -156,7 +158,7 @@ public class TempConv {

    - +
    Import @@ -274,7 +276,7 @@ public class TempConv {

    - When you see the first kind of error in , where the symbol is on the left side of the equals sign, it usually means that you have not declared the variable. If you have ever tried to use a Python variable that you have not initialized the second error message will be familiar to you. The difference here is that we see the message before we ever try to test our program. More common error messages are discussed in the section . + When you see the first kind of error in , where the symbol is on the left side of the equals sign, it usually means that you have not declared the variable. If you have ever tried to use a Python variable that you have not initialized the second error message will be familiar to you. The difference here is that we see the message before we ever try to test our program. More common error messages are discussed in the section .

    @@ -1170,9 +1172,9 @@ public class HistoMap { -

    +
    Summary & Reading Questions -

      +

      1. All variables must be declared with their type before use in Java due to static typing.

      2. @@ -1195,7 +1197,7 @@ public class HistoMap {

        Maps (HashMap and TreeMap) are Java's equivalent to Python dictionaries for storing key-value pairs.

      - +

      What is the correct way to declare an ArrayList that will hold String objects in Java?

      @@ -1235,7 +1237,7 @@ public class HistoMap {
      - +

      The process of automatically converting between primitive types and their Object wrapper classes in Java is called:

      diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 623587a..085ab4b 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -613,9 +613,9 @@ public class Ternary {

    -
    +
    Summary & Reading Questions -

      +

      1. Java requires parentheses around the condition and curly braces for code blocks in if statements, unlike Python which uses indentation alone.

      2. @@ -634,7 +634,7 @@ public class Ternary {

      - +

      Which is a correct Java if statement syntax?

      diff --git a/source/ch5_loopsanditeration.ptx b/source/ch5_loopsanditeration.ptx index c0e87a1..769a358 100644 --- a/source/ch5_loopsanditeration.ptx +++ b/source/ch5_loopsanditeration.ptx @@ -351,10 +351,10 @@ public class DoWhileExample {
    -
    +
    Summary & Reading Questions

    -

      +
      1. Java’s for loop syntax allows you to control loop initialization, condition, and update in one line using the format for (init; condition; update).

      2. @@ -376,7 +376,7 @@ public class DoWhileExample {

      - + diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx index 5042038..d83094e 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -423,6 +423,58 @@ public Fraction(Integer num, Integer den) { + + + +

      + Rearrange the blocks to create a Player class with a private int health instance variable. Implement a getter and a setter that validates health points to remain within 0 and 100. +

      +
      + + + + public class Player { + private int health; + + + public class Player { + public int health; + + + + + public int getHealth() { + return health; + } + + + public void getHealth() { + return health; + } + + + + + public void setHealth(int health) { + if (health >= 0 && health <= 100) { + this.health = health; + } + } + + + public void setHealth(int health) { + if (health >= 0 && health <= 100) { + health = health; + } + } + + + + } + + +
      +
    @@ -1067,6 +1119,49 @@ public int compareTo(Fraction other) { // compare this fraction with another fra + + +

    + Rearrange the blocks to create a Bicycle class that implements Comparable<Bicycle>. The class should feature a private double weight field and a compareTo method that compares bicycles based on their weight. +

    +
    + + + + public class Bicycle implements Comparable<Bicycle> { + private double weight; + + + public class Bicycle extends Comparable<Bicycle> { + public double weight; + + + + + public Bicycle(double weight) { + this.weight = weight; + } + + + + + public int compareTo(Bicycle other) { + return Double.compare(this.weight, other.weight); + } + + + public boolean compareTo(Bicycle other) { + return this.weight - other.weight; + } + + + + + } + + +
    +
    diff --git a/source/ch7_recursion.ptx b/source/ch7_recursion.ptx index f04f71c..47facf1 100644 --- a/source/ch7_recursion.ptx +++ b/source/ch7_recursion.ptx @@ -366,7 +366,7 @@ public class ArrayProcessor {
    Summary & Reading Questions -

      +

      1. Recursion solves problems by defining a base case and a recursive step; each call reduces the problem size until the base case is reached.

      2. diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx index d9656b0..424ff47 100644 --- a/source/ch8_filehandling.ptx +++ b/source/ch8_filehandling.ptx @@ -653,9 +653,9 @@ public class DeleteFile {

    -
    +
    Summary & Reading Questions -

      +

      1. To work with files in Java, you must import specific classes like java.io.File, java.io.FileWriter, and handle exceptions such as IOException.

      2. @@ -672,7 +672,7 @@ public class DeleteFile {

        You can delete a file using the delete() method on a File object, which returns true if successful.

      - +

      Which import is needed to create and manipulate files in Java?

      diff --git a/source/ch9_commonmistakes.ptx b/source/ch9_commonmistakes.ptx index 4dca391..dff1336 100644 --- a/source/ch9_commonmistakes.ptx +++ b/source/ch9_commonmistakes.ptx @@ -1,7 +1,7 @@ - + Common Mistakes @@ -228,6 +228,57 @@

      + + +

      + Based on , select all of the + statements that are true about creating objects in Java. +

      +
      + + + +

      Java requires the new keyword to create a new object.

      +
      + +

      Correct! Unlike Python, Java needs the new keyword to instantiate an object.

      +
      +
      + + +

      Writing data = new Scanner(new File("test.dat")); would fix the error.

      +
      + +

      Correct! Adding new before Scanner properly creates the object and resolves the error.

      +
      +
      + + +

      Scanner is a constructor of the Scanner class, not a standalone method.

      +
      + +

      Correct! Because Scanner is a constructor, it must be called with new.

      +
      +
      + + +

      Java can create objects without a keyword, just like Python.

      +
      + +

      Incorrect. Unlike Python, Java requires the new keyword to instantiate objects.

      +
      +
      + + +

      The "cannot find symbol" error here means the Scanner class was not imported.

      +
      + +

      Incorrect. The Scanner class is imported; the error is caused by the missing new keyword.

      +
      +
      +
      +
      +
    @@ -269,7 +320,7 @@
    Summary & Reading Questions -

      +

      1. In Java, every variable must be declared with its type before use; undeclared variables cause compilation errors.

      2. @@ -290,7 +341,7 @@

      - +

      What happens if you use a variable in Java without declaring it first?

      @@ -313,7 +364,7 @@
      - +

      Why must you include import statements for classes like Scanner?

      @@ -336,7 +387,7 @@
      - +

      What warning occurs when you use an ArrayList without specifying a type?

      diff --git a/source/main.ptx b/source/main.ptx index 6f4f823..713553a 100644 --- a/source/main.ptx +++ b/source/main.ptx @@ -10,7 +10,7 @@ - git +