Skip to content

Commit 6df51b7

Browse files
committed
unit 1_2 1_3 more activecode practice problems
1 parent 6bf6738 commit 6df51b7

File tree

2 files changed

+136
-28
lines changed

2 files changed

+136
-28
lines changed

_sources/Unit1-Getting-Started/topic-1-2-java-intro.rst

Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,16 @@ and the name of the class must match the file name.
178178
}
179179
}
180180

181-
181+
182+
Most command keywords in Java must be in lowercase,
183+
but class names such as System and String are capitalized.
184+
Commands in Java must end with a semicolon ``;``. Think of the semicolon ``;``
185+
in Java like a period in English. You use a semicolon ``;`` to show the
186+
end of a Java **statement**, just the way you use a period to show the end
187+
of an English sentence. Your programs won't run if you forget the semicolon at the
188+
end of each statement.
189+
190+
182191
Print Commands
183192
-------------------
184193

@@ -297,15 +306,14 @@ Java has two different print commands to print output to the screen:
297306

298307

299308
A print statement can also contain numeric values and arithmetic expressions. Don't use double quotes for
300-
expressions that have a numeric value.
309+
expressions that have a numeric value.
301310

302311

303312
.. activecode:: code1_2_4
304313
:language: java
305314
:autograde: unittest
306315

307316
Run this code to see the output below it.
308-
Notice the calculations in the print statements are not contained in double quotes.
309317
Can you change the last print statement to print the sum of the values from 1 to 10?
310318
~~~~
311319
public class CalculationExample
@@ -380,7 +388,8 @@ expressions that have a numeric value.
380388
:autograde: unittest
381389

382390
Assume you have some bills to pay. The individual bill amounts are 89.50, 14.75, 45.12, and 92.50.
383-
Update the program to add up and print the total bill amount on a separate line.
391+
Add another print statement to sum and print the total bill amount on a separate line. Don't just add the numbers in
392+
your head and print the result. You must write the code to add up the numbers and print the result.
384393

385394
~~~~
386395
public class CalculateBillTotal
@@ -411,23 +420,63 @@ expressions that have a numeric value.
411420
@Test
412421
public void test2() throws IOException
413422
{
414-
String target1 = "89.50 + 14.75 + 45.12 + 92.50";
415-
boolean passed1 = checkCodeContains("bill calculation 89.50 + 14.75 + 45.12 + 92.50", target1);
423+
String target1 = "System.out.println( 89.50 + 14.75 + 45.12 + 92.50";
424+
boolean passed1 = checkCodeContains("bill calculation", target1);
416425

417426
assertTrue(passed1);
418427
}
419428

420429
}
421430

422-
Most command keywords in Java must be in lowercase,
423-
but class names such as System and String are capitalized.
424-
Commands in Java must end with a semicolon ``;``. Think of the semicolon ``;``
425-
in Java like a period in English. You use a semicolon ``;`` to show the
426-
end of a Java **statement**, just the way you use a period to show the end
427-
of an English sentence. Your programs won't run if you forget the semicolon at the
428-
end of each statement.
429431

430432

433+
.. activecode:: code1_2_7
434+
:language: java
435+
:autograde: unittest
436+
437+
A bus starts out with no passengers. Three people get on at the first stop.
438+
Five people get on at the second stop.
439+
One person gets off and eight people get on at the third stop.
440+
Three people get off at the fourth stop. How many people are left on the bus?
441+
Add another print statement to calculate and print the passengers remaining on the bus.
442+
443+
~~~~
444+
public class PassengersOnBus
445+
{
446+
public static void main(String[] args)
447+
{
448+
System.out.print("Passengers remaining : ");
449+
450+
}
451+
}
452+
====
453+
// should pass if/when they run code
454+
import static org.junit.Assert.*;
455+
import org.junit.*;;
456+
import java.io.*;
457+
458+
public class RunestoneTests extends CodeTestHelper
459+
{
460+
@Test
461+
public void testMain() throws IOException
462+
{
463+
String output = getMethodOutput("main");
464+
String expect = "Passengers remaining : 12\n";
465+
boolean passed = getResults(expect, output, "Expected output from main");
466+
assertTrue(passed);
467+
}
468+
469+
@Test
470+
public void test2() throws IOException
471+
{
472+
String target1 = " 3 + 5 - 1 + 8 -3";
473+
boolean passed1 = checkCodeContains("passenger calculation", target1);
474+
475+
assertTrue(passed1);
476+
}
477+
478+
}
479+
431480

432481

433482
Syntax Errors
@@ -541,7 +590,7 @@ Run the following code. Look for an error message after the code. This is call
541590
What is wrong? Can you fix it? The error message will tell you the line number that it thinks is
542591
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**.
543592

544-
.. activecode:: code1_2_6
593+
.. activecode:: code1_2_8
545594
:language: java
546595
:autograde: unittest
547596
:practice: T
@@ -582,7 +631,7 @@ Try and run the following code.
582631
Look for an error message after the code. What is wrong this time? Can you fix it?
583632
One good thing to check is that all ``{`` have a matching ``}`` and all ``(`` have a matching ``)`` and all starting ``"`` have a ending ``"`` as well.
584633

585-
.. activecode:: code1_2_7
634+
.. activecode:: code1_2_9
586635
:language: java
587636
:autograde: unittest
588637
:practice: T
@@ -621,10 +670,9 @@ One good thing to check is that all ``{`` have a matching ``}`` and all ``(`` ha
621670

622671
Try and run the following code.
623672
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!
626674

627-
.. activecode:: code1_2_8
675+
.. activecode:: code1_2_10
628676
:language: java
629677
:autograde: unittest
630678
:practice: T
@@ -666,7 +714,7 @@ Did you remember that System is capitalized in System.out.println? Did you find
666714
|Groupwork| Debugging Challenge
667715
-----------------------------------
668716

669-
.. activecode:: code1_2_9
717+
.. activecode:: code1_2_11
670718
:language: java
671719
:autograde: unittest
672720
:practice: T

_sources/Unit1-Getting-Started/topic-1-3-variables.rst

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ the memory location called score to 4.
295295
}
296296

297297

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
299299
because that will print out the variable
300300
name letter by letter. For example, ``System.out.println("score");`` will print out the string "score",
301301
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
304304

305305
.. note::
306306

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
308308
would print the variable name instead of its value.
309309

310310

@@ -516,13 +516,73 @@ If you want spaces between words and variables when printing, you must put the
516516
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
517517
like "HiJose" instead of "Hi Jose".
518518

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+
519579
Also note that the variable has to be on the
520580
left side of the ``=`` and the value on the right. Switching the two is
521581
called **assignment dyslexia**.
522582

523583
|CodingEx| **Coding Exercise:**
524584

525-
.. activecode:: code1_3_3
585+
.. activecode:: code1_3_4
526586
:language: java
527587
:autograde: unittest
528588

@@ -567,7 +627,7 @@ The keyword **final** can be used in front of a variable declaration to make it
567627
568628
|CodingEx| **Coding Exercise:**
569629

570-
.. activecode:: code1_3_4
630+
.. activecode:: code1_3_5
571631
:language: java
572632
:autograde: unittest
573633

@@ -636,7 +696,7 @@ The convention in Java and many programming languages is to always start a varia
636696
|CodingEx| **Coding Exercise:**
637697

638698

639-
.. activecode:: code1_3_5
699+
.. activecode:: code1_3_6
640700
:language: java
641701
:autograde: unittest
642702

@@ -672,7 +732,7 @@ The convention in Java and many programming languages is to always start a varia
672732

673733
|Exercise| **Check Your Understanding**
674734

675-
.. fillintheblank:: q1_3_16
735+
.. fillintheblank:: q1_3_17
676736

677737
What is the camel case variable name for a variable that represents a shoe size?
678738

@@ -681,14 +741,14 @@ The convention in Java and many programming languages is to always start a varia
681741

682742

683743

684-
.. fillintheblank:: q1_3_17
744+
.. fillintheblank:: q1_3_18
685745

686746
What is the camel case variable name for a variable that represents the top score?
687747

688748
- :^\s*topScore$: Correct.
689749
:.*: In camel case just put the words after each other but uppercase the first letter of each word after the 1st word.
690750

691-
.. .. fillintheblank:: q1_3_18
751+
.. .. fillintheblank:: q1_3_19
692752
693753
What is the camel case variable name for a variable that represents the last score?
694754
@@ -699,7 +759,7 @@ The convention in Java and many programming languages is to always start a varia
699759
|Groupwork| Debugging Challenge : Weather Report
700760
------------------------------------------------
701761

702-
.. activecode:: code1_3_6
762+
.. activecode:: code1_3_7
703763
:language: java
704764
:autograde: unittest
705765
:practice: T

0 commit comments

Comments
 (0)