Skip to content

Commit ab4279c

Browse files
authored
Merge branch 'master' into issue_301_comment
2 parents cac7e73 + 55d59bb commit ab4279c

2 files changed

Lines changed: 123 additions & 116 deletions

File tree

source/ch4_conditionals.ptx

Lines changed: 93 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
<chapter xml:id="conditionals">
55
<title>Conditionals</title>
66

7-
<section xml:id="simple-if">
8-
<title>Using the Simple <c>if</c> Statement</title>
7+
<section xml:id="using-conditional-in-java"> <title>Using Conditional Statements in Java</title>
98
<p><idx>conditional statements</idx>
109
Conditional statements in Python and Java are very similar.
1110
In Python we have three patterns:
@@ -50,9 +49,9 @@ if score &gt;= 90: # Note the colon at the end of the line
5049
Once again you can see that in Java the curly braces define a block rather than indentation.
5150
In Java, the parentheses around the condition are required because it is technically a function that evaluates to <c>True</c> or <c>False</c>.
5251
</p>
53-
</section>
52+
5453

55-
<section xml:id="use-the-if-else-statement">
54+
<subsection xml:id="use-the-if-else-statement">
5655
<title>Using the <c>if</c> - <c>else</c> Statement</title>
5756

5857
<p><xref ref= "python-if-else" text="type-global"/> shows how the <c>if</c> - <c>else</c>statement is written in Python.</p>
@@ -85,9 +84,9 @@ if score &gt;= 90: # Note the colon at the end of the line
8584
</code>
8685
</program>
8786
</listing>
88-
</section>
87+
</subsection>
8988

90-
<section xml:id="elif">
89+
<subsection xml:id="elif">
9190
<title>Can we use <c>elif</c>?</title>
9291

9392
<p><idx><c>elif</c> statement</idx>
@@ -172,9 +171,9 @@ public class ElseIf {
172171
</code> <tests> </tests>
173172
</program>
174173
</listing>
175-
</section>
174+
</subsection>
176175

177-
<section xml:id="switch">
176+
<subsection xml:id="switch">
178177
<title>Using the <c>switch</c> Statement</title>
179178

180179
<p>
@@ -255,9 +254,94 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
255254
</p>
256255
<p>
257256
Finally, the <c>switch</c> statement does not support relational expressions such as greater than or less than. So you cannot use it to completely replace the <c>elif</c>. Even with the new features of Java 14+ the <c>switch</c> statement is still limited to constant comparisons using equality.</p>
257+
</subsection>
258258
</section>
259259

260-
<section xml:id="exception-handling">
260+
<section xml:id="ternary-operator">
261+
<title>The Ternary Operator</title>
262+
263+
<p><idx>Boolean operators</idx> <idx>simple comparisons</idx> <idx>compound Boolean expressions</idx>
264+
The conditionals used in the if statement can be <term>Boolean variables</term>, <term>simple comparisons</term>, and <term>compound Boolean expressions</term>.
265+
</p>
266+
267+
<p><idx>ternary operator</idx>
268+
Java also provides the ternary operator <c>condition ? valueIfTrue : valueIfFalse</c>, which lets you use a <c>boolean</c> test directly inside an assignment. If the condition is true, the first value is chosen; otherwise, the second value is used. <xref ref="ternary-operator-table" text="type-global"/> summarizes how it works.
269+
</p>
270+
271+
<table xml:id="ternary-operator-table">
272+
<title>Ternary Operator in Java</title>
273+
<tabular>
274+
<row>
275+
<cell><term>Component</term></cell>
276+
<cell><term>Description</term></cell>
277+
</row>
278+
<row>
279+
<cell><c>condition</c></cell>
280+
<cell>The <c>boolean</c> expression that is evaluated (e.g., <c>a % 2 == 0</c>).</cell>
281+
</row>
282+
<row>
283+
<cell><c>?</c></cell>
284+
<cell> This is the ternary operator that separates the condition from the <c>trueValue</c>.</cell>
285+
</row>
286+
<row>
287+
<cell><c>trueValue</c></cell>
288+
<cell>The value assigned if the condition is true (e.g., <c>a * a</c>).</cell>
289+
</row>
290+
<row>
291+
<cell><c>:</c></cell>
292+
<cell> This is the ternary operator that separates the <c>trueValue</c> from the <c>falseValue</c>.</cell>
293+
</row>
294+
<row>
295+
<cell><c>falseValue</c></cell>
296+
<cell>The value assigned if the condition is false (e.g., <c>3 * x - 1</c>).</cell>
297+
</row>
298+
<row>
299+
<cell>Example Usage</cell>
300+
<cell><c>a = a % 2 == 0 ? a * a : 3 * x - 1</c></cell>
301+
</row>
302+
<row>
303+
<cell>Equivalent if-else Code</cell>
304+
<cell>Can also be written with a regular <c>if-else</c> statement, but the ternary form is more concise.</cell>
305+
</row>
306+
</tabular>
307+
</table>
308+
309+
<p>
310+
Using this operator can make code shorter and more readable in cases where a simple conditional assignment is needed. <xref ref="java-ternary" text="type-global"/> shows an example where we see the same logic implemented in two different ways.
311+
</p>
312+
<listing xml:id="java-ternary">
313+
<program interactive="activecode" language ="java">
314+
<code>
315+
public class Ternary {
316+
public static void main(String[] args) {
317+
int a = 4;
318+
int x = 2;
319+
int outp;
320+
321+
// ternary:
322+
outp = (a % 2 == 0) ? (a * a) : (3 * x - 1);
323+
System.out.println("ternary result: " + outp);
324+
325+
// Equivalent using if/else
326+
if (a % 2 == 0) {
327+
outp = a * a;
328+
} else {
329+
outp = 3 * x - 1;
330+
}
331+
332+
System.out.println("if/else result: " + outp);
333+
}
334+
}
335+
</code>
336+
</program>
337+
</listing>
338+
339+
<p>
340+
In <xref ref="java-ternary" text="type-global"/>, we are using this ternary operator to assign a value to <c>a</c> based on whether <c>a</c> is even or odd. If <c>a</c> is even, it will be squared; if odd, it will be instead be calculated as <c>3 * x - 1</c>. This is a concise way to write conditional assignments in Java. However, you might want to use it sparingly, as it can make code less readable if overused or used with complex expressions.
341+
</p>
342+
</section>
343+
344+
<section xml:id="exception-handling">
261345
<title>Exception Handling</title>
262346

263347
<p>
@@ -411,92 +495,6 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
411495
Note that as with other structures in Java, <c>try-catch</c> blocks blocks must be encased with braces <c>{}</c>. The most important part of this code is, after <c>catch</c>, there is a set of parenthesis with an exception type and a variable name <c>catch (InputMismatchException e)</c>. This is where we declare a <c>InputMismatchException</c> exception and name it with the variable name <c>e</c>. It is common practice, though not a requirement, to name exception variables <c>e</c> in this manner.
412496
</p>
413497

414-
</section>
415-
416-
<section xml:id="ternary-operator">
417-
<title>The Ternary Operator</title>
418-
419-
<p><idx>Boolean operators</idx> <idx>simple comparisons</idx> <idx>compound Boolean expressions</idx>
420-
The conditionals used in the if statement can be <term>Boolean variables</term>, <term>simple comparisons</term>, and <term>compound Boolean expressions</term>.
421-
</p>
422-
423-
<p><idx>ternary operator</idx>
424-
Java also provides the ternary operator <c>condition ? valueIfTrue : valueIfFalse</c>, which lets you use a <c>boolean</c> test directly inside an assignment. If the condition is true, the first value is chosen; otherwise, the second value is used. <xref ref="ternary-operator-table" text="type-global"/> summarizes how it works.
425-
</p>
426-
427-
<table xml:id="ternary-operator-table">
428-
<title>Ternary Operator in Java</title>
429-
<tabular>
430-
<row>
431-
<cell><term>Component</term></cell>
432-
<cell><term>Description</term></cell>
433-
</row>
434-
<row>
435-
<cell><c>condition</c></cell>
436-
<cell>The <c>boolean</c> expression that is evaluated (e.g., <c>a % 2 == 0</c>).</cell>
437-
</row>
438-
<row>
439-
<cell><c>?</c></cell>
440-
<cell> This is the ternary operator that separates the condition from the <c>trueValue</c>.</cell>
441-
</row>
442-
<row>
443-
<cell><c>trueValue</c></cell>
444-
<cell>The value assigned if the condition is true (e.g., <c>a * a</c>).</cell>
445-
</row>
446-
<row>
447-
<cell><c>:</c></cell>
448-
<cell> This is the ternary operator that separates the <c>trueValue</c> from the <c>falseValue</c>.</cell>
449-
</row>
450-
<row>
451-
<cell><c>falseValue</c></cell>
452-
<cell>The value assigned if the condition is false (e.g., <c>3 * x - 1</c>).</cell>
453-
</row>
454-
<row>
455-
<cell>Example Usage</cell>
456-
<cell><c>a = a % 2 == 0 ? a * a : 3 * x - 1</c></cell>
457-
</row>
458-
<row>
459-
<cell>Equivalent if-else Code</cell>
460-
<cell>Can also be written with a regular <c>if-else</c> statement, but the ternary form is more concise.</cell>
461-
</row>
462-
</tabular>
463-
</table>
464-
465-
<p>
466-
Using this operator can make code shorter and more readable in cases where a simple conditional assignment is needed. <xref ref="java-ternary" text="type-global"/> shows an example where we see the same logic implemented in two different ways.
467-
</p>
468-
<listing xml:id="java-ternary">
469-
<program interactive="activecode" language ="java">
470-
<code>
471-
public class Ternary {
472-
public static void main(String[] args) {
473-
int a = 4;
474-
int x = 2;
475-
int outp;
476-
477-
// ternary:
478-
outp = (a % 2 == 0) ? (a * a) : (3 * x - 1);
479-
System.out.println("ternary result: " + outp);
480-
481-
// Equivalent using if/else
482-
if (a % 2 == 0) {
483-
outp = a * a;
484-
} else {
485-
outp = 3 * x - 1;
486-
}
487-
488-
System.out.println("if/else result: " + outp);
489-
}
490-
}
491-
</code>
492-
</program>
493-
</listing>
494-
495-
<p>
496-
In <xref ref="java-ternary" text="type-global"/>, we are using this ternary operator to assign a value to <c>a</c> based on whether <c>a</c> is even or odd. If <c>a</c> is even, it will be squared; if odd, it will be instead be calculated as <c>3 * x - 1</c>. This is a concise way to write conditional assignments in Java. However, you might want to use it sparingly, as it can make code less readable if overused or used with complex expressions.
497-
</p>
498-
499-
500498
</section>
501499
<section xml:id="chapter4_summary">
502500
<title>Summary &amp; Reading Questions</title>

source/ch6_definingclasses.ptx

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -215,23 +215,26 @@ public void setDenominator(Integer denominator) { // setter method
215215
<section xml:id="writing-a-constructor">
216216
<title>Writing a constructor</title>
217217

218-
<p><idx><h>constructor</h></idx>
218+
<p>
219+
<idx>constructor</idx>
220+
219221
Once you have identified the instance variables for your class the next thing to consider is the constructor.
220222
In Java, <term>constructors</term> have the same name as the class and are declared public.
221223
They are declared without a return type.
222224
So any method that is named the same as the class and has no return type is a constructor.
223-
Our constructor will take two parameters: the numerator and the denominator.
225+
Our constructor will take two parameters: the numerator and the denominator. <xref ref="java-fraction-constructor" text="type-global"/> shows the constructor for the <c>Fraction</c> class.
224226
</p>
225227

226-
227-
<program xml:id="java-fraction-constructor" language="java">
228+
<listing xml:id="java-fraction-constructor">
229+
<program interactive="activecode" language="java">
228230
<code>
229231
public Fraction(Integer top, Integer bottom) {
230-
num = top;
232+
num = top; // notice the use of num instead of top
231233
den = bottom;
232234
}
233235
</code>
234236
</program>
237+
</listing>
235238

236239
<p>
237240
<idx><c>this</c></idx>
@@ -241,18 +244,19 @@ public Fraction(Integer top, Integer bottom) {
241244
This allows the Java compiler to do the work of dereferencing the current Java object.
242245
Java does provide a special variable called <c>this</c> that works like the <c>self</c> variable.
243246
In Java, <c>this</c> is typically only used when it is needed to differentiate between a parameter or local variable and an instance variable.
244-
For example this alternate definition of the the Fraction constructor uses <c>this</c> to differentiate between parameters and instance variables.
247+
For example,<xref ref="java-fraction-constructor" text="type-global"/> shows an alternate definition of the the Fraction constructor that uses <c>this</c> to differentiate between parameters and instance variables.
245248
</p>
246249

247-
248-
<program xml:id="java-fraction-class-this" language="java">
250+
<listing xml:id="java-fraction-class-this">
251+
<program interactive="activecode" language="java">
249252
<code>
250253
public Fraction(Integer num, Integer den) {
251-
this.num = num;
254+
this.num = num; // notice how we use this.num instead of num
252255
this.den = den;
253256
}
254257
</code>
255258
</program>
259+
</listing>
256260
</section>
257261

258262
<section xml:id="methods">
@@ -297,11 +301,11 @@ public Fraction(Integer num, Integer den) {
297301
However, if you reassign the parameter to a completely new object inside the method (e.g., <c>otherFrac = new Fraction(0,1);</c>), it would <em>not</em> affect the original variable outside the method, because you are only changing the local copy of the reference.
298302
</p>
299303
<p>
300-
Let&#x2019;s begin by implementing addition in Java:
304+
<xref ref="java-fraction-class-addition-initial" text="type-global"/> shows the first part of the Fraction class definition.
301305
</p>
302306

303-
304-
<program xml:id="java-fraction-class-addition" language="java">
307+
<listing xml:id="java-fraction-class-addition-initial">
308+
<program>
305309
<code>
306310
public Fraction add(Fraction otherFrac) {
307311
Integer newNum = otherFrac.getDenominator() * this.numerator +
@@ -312,6 +316,7 @@ public Fraction add(Fraction otherFrac) {
312316
}
313317
</code>
314318
</program>
319+
</listing>
315320

316321
<p>
317322
First you will notice that the <c>add</c> method is declared as <c>public Fraction</c> The <c>public</c> part means that any other method may call the <c>add</c> method.
@@ -321,11 +326,11 @@ public Fraction add(Fraction otherFrac) {
321326
<p>
322327
Second, you will notice that the method makes use of the <c>this</c> variable.
323328
In this method, <c>this</c> is not necessary, because there is no ambiguity about the <c>numerator</c> and <c>denominator</c> variables.
324-
So the following version of the code is equivalent:
329+
<xref ref="java-fraction-class-addition-equivalent" text="type-global"/> is an equivalent version of <xref ref="java-fraction-class-addition-initial" text="type-global"/>.
325330
</p>
326331

327-
328-
<program xml:id="java-fraction-class-addition2" language="java">
332+
<listing xml:id="java-fraction-class-addition-equivalent">
333+
<program>
329334
<code>
330335
public Fraction add(Fraction otherFrac) {
331336
Integer newNum = otherFrac.getDenominator() * numerator +
@@ -336,6 +341,7 @@ public Fraction add(Fraction otherFrac) {
336341
}
337342
</code>
338343
</program>
344+
</listing>
339345

340346
<p>
341347
The addition takes place by multiplying each numerator by the opposite denominator before adding.
@@ -382,11 +388,11 @@ public Fraction add(Fraction otherFrac) {
382388
To solve the problem of adding an <c>Integer</c> and a <c>Fraction</c> in Java we will overload both the constructor and the <c>add</c> method.
383389
We will overload the constructor so that if it only receives a single <c>Integer</c> it will convert the <c>Integer</c> into a <c>Fraction</c>.
384390
We will also overload the <c>add</c> method so that if it receives an <c>Integer</c> as a parameter it will first construct a <c>Fraction</c> from that integer and then add the two <c>Fractions</c> together.
385-
The new methods that accomplish this task are as follows:
391+
<xref ref="java-method-overloading" text="type-global"/> shows the new methods that accomplish this task.
386392
</p>
387393

388-
389-
<program xml:id="java-method-overloading" language="java">
394+
<listing xml:id="java-method-overloading">
395+
<program>
390396
<code>
391397
public Fraction(Integer num) {
392398
this.numerator = num; // set the numerator to the Integer
@@ -397,6 +403,7 @@ public Fraction add(Integer other) { // overload the add method when the paramet
397403
}
398404
</code>
399405
</program>
406+
</listing>
400407

401408
<p>
402409
Notice that the overloading approach can provide us with a certain elegance to our code.
@@ -405,12 +412,12 @@ public Fraction add(Integer other) { // overload the add method when the paramet
405412
</p>
406413

407414
<p>
408-
Our full <c>Fraction</c> class to this point would look like the following.
415+
Our full <c>Fraction</c> class to this point would look <xref ref="java-full-fraction-class" text="type-global"/>.
409416
You should compile and run the program to see what happens.
410417
</p>
411418

412-
413-
<program xml:id="java-full-fraction-class" interactive="activecode" language="java">
419+
<listing xml:id="java-full-fraction-class">
420+
<program interactive="activecode" language="java">
414421
<code>
415422
public class Fraction {
416423
private Integer numerator;
@@ -454,6 +461,8 @@ public class Fraction {
454461
}
455462
</code> <tests> </tests>
456463
</program>
464+
</listing>
465+
457466
</subsection>
458467
</section>
459468

0 commit comments

Comments
 (0)