You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -541,7 +590,7 @@ Run the following code. Look for an error message after the code. This is call
541
590
What is wrong? Can you fix it? The error message will tell you the line number that it thinks is
542
591
causing the error (``Error1.java:5: error: unclosed string literal``). Check line 5 to make sure that everything looks correct. One good thing to check is that all ``{`` have a matching ``}`` and all ``(`` have a matching ``)`` and all starting ``"`` have a ending ``"`` as well. Try putting in the missing symbol and run again. This is called **debugging**.
543
592
544
-
.. activecode:: code1_2_6
593
+
.. activecode:: code1_2_8
545
594
:language: java
546
595
:autograde: unittest
547
596
:practice: T
@@ -582,7 +631,7 @@ Try and run the following code.
582
631
Look for an error message after the code. What is wrong this time? Can you fix it?
583
632
One good thing to check is that all ``{`` have a matching ``}`` and all ``(`` have a matching ``)`` and all starting ``"`` have a ending ``"`` as well.
584
633
585
-
.. activecode:: code1_2_7
634
+
.. activecode:: code1_2_9
586
635
:language: java
587
636
:autograde: unittest
588
637
:practice: T
@@ -621,10 +670,9 @@ One good thing to check is that all ``{`` have a matching ``}`` and all ``(`` ha
621
670
622
671
Try and run the following code.
623
672
What is wrong this time? Can you fix it? After you fix the first error, you may
624
-
encounter a 2nd error! Fix that one too! Hints: How do you end a command in Java?
625
-
Also, check for capitalization.
673
+
encounter a 2nd error! Fix that one too!
626
674
627
-
.. activecode:: code1_2_8
675
+
.. activecode:: code1_2_10
628
676
:language: java
629
677
:autograde: unittest
630
678
:practice: T
@@ -666,7 +714,7 @@ Did you remember that System is capitalized in System.out.println? Did you find
Copy file name to clipboardExpand all lines: _sources/Unit1-Getting-Started/topic-1-3-variables.rst
+69-9Lines changed: 69 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -295,7 +295,7 @@ the memory location called score to 4.
295
295
}
296
296
297
297
298
-
When you are printing the value of a variable, never put quotes "" around the variable
298
+
When you are printing the value of a variable, never put double quotes ``" "`` around the variable
299
299
because that will print out the variable
300
300
name letter by letter. For example, ``System.out.println("score");`` will print out the string "score",
301
301
rather than the value 4 stored in the variable. Normally you do not want to print out the variable name,
@@ -304,7 +304,7 @@ putting quotes around the variables in the print statements above and see what h
304
304
305
305
.. note::
306
306
307
-
Avoid putting a variable inside quotes ("") in a print statement since that
307
+
Avoid putting a variable inside quotes ``" "`` in a print statement since that
308
308
would print the variable name instead of its value.
309
309
310
310
@@ -516,13 +516,73 @@ If you want spaces between words and variables when printing, you must put the
516
516
space within the quoted string. For example, notice the space in the string "Hi " in the last print statement. If you forget to add spaces, you will get smushed output
517
517
like "HiJose" instead of "Hi Jose".
518
518
519
+
.. mchoice:: q1_3_16
520
+
:practice: T
521
+
:answer_a: System.out.println("Price is + price");
522
+
:answer_b: System.out.println("Price is " price);
523
+
:answer_c: System.out.println("Price is " + price);
524
+
:answer_d: System.out.println(Price is + price);
525
+
:answer_e: System.out.println("Price is " + "price");
526
+
:correct: c
527
+
:feedback_a: This will print: Price is + price
528
+
:feedback_b: This results in a compile time error. Missing + for string concatenation
529
+
:feedback_c: Correct!
530
+
:feedback_d: This results in a compile time error. Missing quotes "Price is "
531
+
:feedback_e: This will print: Price is price
532
+
533
+
Assume variable declaration ``double price = 9.50;``. Which print statement will result in the output: ``Price is 9.50``
534
+
535
+
536
+
.. activecode:: code1_3_3
537
+
:language: java
538
+
:autograde: unittest
539
+
540
+
Add a print statement to concatenate the string literal "Favorite color is " with the value stored in the ``color`` variable.
541
+
~~~~
542
+
public class StringConcatenation2
543
+
{
544
+
public static void main(String[] args)
545
+
{
546
+
String color = "red";
547
+
548
+
}
549
+
}
550
+
551
+
====
552
+
// should pass if/when they run code
553
+
import static org.junit.Assert.*;
554
+
import org.junit.*;;
555
+
import java.io.*;
556
+
557
+
public class RunestoneTests extends CodeTestHelper
558
+
{
559
+
@Test
560
+
public void testMain() throws IOException
561
+
{
562
+
String output = getMethodOutput("main");
563
+
String expect = "Favorite color is red\n";
564
+
boolean passed = getResults(expect, output, "Expected output from main", true);
565
+
assertTrue(passed);
566
+
}
567
+
@Test
568
+
public void test2() throws IOException
569
+
{
570
+
String target1 = " + color);";
571
+
boolean passed1 = checkCodeContains("string concatenation for color variable", target1);
572
+
573
+
assertTrue(passed1);
574
+
}
575
+
}
576
+
577
+
578
+
519
579
Also note that the variable has to be on the
520
580
left side of the ``=`` and the value on the right. Switching the two is
521
581
called **assignment dyslexia**.
522
582
523
583
|CodingEx| **Coding Exercise:**
524
584
525
-
.. activecode:: code1_3_3
585
+
.. activecode:: code1_3_4
526
586
:language: java
527
587
:autograde: unittest
528
588
@@ -567,7 +627,7 @@ The keyword **final** can be used in front of a variable declaration to make it
567
627
568
628
|CodingEx| **Coding Exercise:**
569
629
570
-
.. activecode:: code1_3_4
630
+
.. activecode:: code1_3_5
571
631
:language: java
572
632
:autograde: unittest
573
633
@@ -636,7 +696,7 @@ The convention in Java and many programming languages is to always start a varia
636
696
|CodingEx| **Coding Exercise:**
637
697
638
698
639
-
.. activecode:: code1_3_5
699
+
.. activecode:: code1_3_6
640
700
:language: java
641
701
:autograde: unittest
642
702
@@ -672,7 +732,7 @@ The convention in Java and many programming languages is to always start a varia
672
732
673
733
|Exercise| **Check Your Understanding**
674
734
675
-
.. fillintheblank:: q1_3_16
735
+
.. fillintheblank:: q1_3_17
676
736
677
737
What is the camel case variable name for a variable that represents a shoe size?
678
738
@@ -681,14 +741,14 @@ The convention in Java and many programming languages is to always start a varia
681
741
682
742
683
743
684
-
.. fillintheblank:: q1_3_17
744
+
.. fillintheblank:: q1_3_18
685
745
686
746
What is the camel case variable name for a variable that represents the top score?
687
747
688
748
- :^\s*topScore$: Correct.
689
749
:.*: In camel case just put the words after each other but uppercase the first letter of each word after the 1st word.
690
750
691
-
.. .. fillintheblank:: q1_3_18
751
+
.. .. fillintheblank:: q1_3_19
692
752
693
753
What is the camel case variable name for a variable that represents the last score?
694
754
@@ -699,7 +759,7 @@ The convention in Java and many programming languages is to always start a varia
0 commit comments