Skip to content

Commit e0da126

Browse files
committed
Added summary and reading questions in chapter 4
1 parent a9487c3 commit e0da126

1 file changed

Lines changed: 145 additions & 0 deletions

File tree

‎source/ch_4_javadatatypes.ptx‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,4 +722,149 @@ public class HistoMap {
722722
Improve the program above to remove the punctuation.
723723
</p>
724724
</section>
725+
<section xml:id="chapter4_summary">
726+
<title>Summary &amp; Reading Questions</title>
727+
<p><ol label="1">
728+
<li>
729+
<p>All variables must be declared with their type before use in Java due to static typing.</p>
730+
</li>
731+
<li>
732+
<p>Java has primitive types (<c>int</c>, <c>float</c>, <c>double</c>, <c>char</c>, <c>boolean</c>) and their corresponding Object wrapper classes (<c>Integer</c>, <c>Float</c>, <c>Double</c>, <c>Character</c>, <c>Boolean</c>) with automatic conversion between them called autoboxing.</p>
733+
</li>
734+
<li>
735+
<p>The <c>import</c> statement in Java allows you to use shortened class names instead of fully qualified names like <c>java.util.Scanner</c>.</p>
736+
</li>
737+
<li>
738+
<p><c>Scanner</c> objects are used to read input from various sources, including <c>System.in</c> for keyboard input.</p>
739+
</li>
740+
<li>
741+
<p>Java Strings are immutable like Python, but use methods like <c>charAt()</c> and <c>substring()</c> instead of indexing operators.</p>
742+
</li>
743+
<li>
744+
<p><c>ArrayList</c> is Java's dynamic array implementation that is similar to Python lists, but must declare the type of objects it contains using generics like <c>ArrayList&lt;Integer&gt;</c>.</p>
745+
</li>
746+
<li>
747+
<p>Maps (<c>HashMap</c> and <c>TreeMap</c>) are Java's equivalent to Python dictionaries for storing key-value pairs.</p>
748+
</li>
749+
</ol></p>
750+
<reading-questions xml:id="rqs-summary4">
751+
<exercise label = "summary-arraylist">
752+
<statement>
753+
<p>What is the correct way to declare an ArrayList that will hold String objects in Java?</p>
754+
</statement>
755+
<choices>
756+
<choice correct="yes">
757+
<statement>
758+
<p>ArrayList&lt;String&gt; list;</p>
759+
</statement>
760+
<feedback>
761+
<p>Good job!</p>
762+
</feedback>
763+
</choice>
764+
<choice>
765+
<statement>
766+
<p>ArrayList list;</p>
767+
</statement>
768+
<feedback>
769+
<p>No, you must specify the type of objects it will hold using generics.</p>
770+
</feedback>
771+
</choice>
772+
<choice>
773+
<statement>
774+
<p>ArrayList[String] list;</p>
775+
</statement>
776+
<feedback>
777+
<p>No, the syntax for generics uses &lt; &gt; brackets, not [ ].</p>
778+
</feedback>
779+
</choice>
780+
<choice>
781+
<statement>
782+
<p>ArrayList() list;</p>
783+
</statement>
784+
<feedback>
785+
<p>No, this is not a valid declaration.</p>
786+
</feedback>
787+
</choice>
788+
</choices>
789+
</exercise>
790+
<exercise label = "summary-autoboxing">
791+
<statement>
792+
<p>The process of automatically converting between primitive types and their Object wrapper classes in Java is called:</p>
793+
</statement>
794+
<choices>
795+
<choice correct="yes">
796+
<statement>
797+
<p>Autoboxing</p>
798+
</statement>
799+
<feedback>
800+
<p>Right! Good job!</p>
801+
</feedback>
802+
</choice>
803+
<choice>
804+
<statement>
805+
<p>Casting</p>
806+
</statement>
807+
<feedback>
808+
<p>No, casting is manually converting a type.</p>
809+
</feedback>
810+
</choice>
811+
<choice>
812+
<statement>
813+
<p>Overloading</p>
814+
</statement>
815+
<feedback>
816+
<p>No, overloading refers to methods, not type conversion.</p>
817+
</feedback>
818+
</choice>
819+
<choice>
820+
<statement>
821+
<p>Unboxing</p>
822+
</statement>
823+
<feedback>
824+
<p>No, unboxing is the reverse of autoboxing.</p>
825+
</feedback>
826+
</choice>
827+
</choices>
828+
</exercise>
829+
<exercise label = "summary-string-methods">
830+
<statement>
831+
<p>Which method would you use to get the character at position 3 in a Java String called str?</p>
832+
</statement>
833+
<choices>
834+
<choice correct="yes">
835+
<statement>
836+
<p>str.charAt(3)</p>
837+
</statement>
838+
<feedback>
839+
<p>Correct!</p>
840+
</feedback>
841+
</choice>
842+
<choice>
843+
<statement>
844+
<p>str[3]</p>
845+
</statement>
846+
<feedback>
847+
<p>No, Java does not support this type of indexing.</p>
848+
</feedback>
849+
</choice>
850+
<choice>
851+
<statement>
852+
<p>str.get(3)</p>
853+
</statement>
854+
<feedback>
855+
<p>No, this method does not exist in Java.</p>
856+
</feedback>
857+
</choice>
858+
<choice>
859+
<statement>
860+
<p>str.substring(3)</p>
861+
</statement>
862+
<feedback>
863+
<p>No, this will return a substring starting at position 3, not a single character.</p>
864+
</feedback>
865+
</choice>
866+
</choices>
867+
</exercise>
868+
</reading-questions>
869+
</section>
725870
</chapter>

0 commit comments

Comments
 (0)