%%% SECTION %%%
\section{Unit One Activities}
\begin{quote}
    Activities in the first chapter (How to use ProgramLive)\\
    Activities on Lesson page 2-1 (Hardware and software)\\
    Activities on Lesson page 2-2 (Some simple Java programs)\\
    Activities on Lesson page 2-3 (Components of a Java program)\\
    Activities on Lesson page 2-4 (Three Statements: assignment, conditional, block)
\end{quote}

%%% SECTION %%%
\section{Overview}

In this first unit on programming, you will encounter some basic
terminology of computing and programming, look at a few Java programs, and
study the basic components of Java programs.

There are fewer activities in this unit compared to all the other
units. This is intentional: we expect you to spend roughly 12-15 hours
per unit, and most new programmers spend several hours learning how to
compile and run programs. You should also spend about half an hour in
the first chapter of \pl, learning about all the features of the
livetext. In addition to the activities, we provide a plethora of
instructional tools such as the glossary, index, exercises and labs.

%%% SECTION %%%
\section{Lesson Page 2-1: Basics}

The two Activities on this page use many phrases with which you are
probably already familiar. The important ones to learn are:
\begin{quote}hardware, software, program, CPU, I/O (input/output),
memory, memory location, address of a memory location, byte, and bit.
\end{quote}

It is useful to know that the bits in a memory location can be used to
represent many things, like integers, characters, and computer
instructions. Also, there are different representations of integers ---e.g.
decimal, binary, and octal. You don't have to master these ideas right now
---you can learn a lot about programming without them--- but they are
fundamental concepts that every programmer knows.

%%% SECTION %%%
\section{Lesson Page 2-2: Simple Java programs}

Lesson page 2-2 gives you an overview of Java programs and their execution.
The details will be explained in later Lessons, so don't expect to or try
to remember everything you see and hear about Java programs on this Lesson
page. Pay attention to the filing cabinet metaphor, because we use it
throughout \pl.
\begin{description}
\item\textbf{Activity 2-2-1: A do-nothing Java program.} 

%% Definitions and phrase completions
  \defn{keyword}

  \defn{method}

  \phrase{Execution of a program starts by calling:}

  \phrase{Every program has a method called:}
  
  \phrase[0in]{The \blankfill{1in} is the basic building block in Java.}

  \phrase[0in]{The body of every class begins with this symbol:
    \blankfill{.3in} and ends with this symbol: \blankfill{.3in}.}

  \truefalse{In programming, the terms \textit{instruction} and
    \textit{statement} mean the same thing.}
  
    
  \pagebreak
  
  \begin{exercise}
    What is wrong with the following Java program?
  
    \begin{verbatim}
      // This program won't even compile.
      public class Garble (
          public static void start(String[] args) {
          }
      )
    \end{verbatim}
  \end{exercise}

  \begin{enumerate}
    \item Type it in exactly as shown and see what happens when you
    compile it. You will get an \emph{error message}, which indicates
    that the computer couldn't understand something about your program
    while trying to compile. Read the error message carefully, review
    your notes from this section, and fix the two errors. When you try
    to compile again, it will work.

  \item Now try to run it. You will get another error message; if you read
    it carefully it will seem to have something to do with method
    \code{main} being missing. And it is ---the method in this program is
    called \code{start}, not \code{main}. Change the name, recompile, and
    run it again. This time, nothing will happen: 0 statements were
    executed.  Nothing was printed, and so there is no output.
  \end{enumerate}

\item\textbf{Activity 2-2-2: A program that prints.}

%% Definitions and phrase completions
  \phrase{What is the \emph{Java console}?}

  \phrase{What is the difference between \texttt{print} and
    \texttt{println}?}

  \phrase{What happens when the following statement is executed?
  $$\code{System.out.print(``Hello, everyone'');}$$}
  
\begin{exercise}
  Write a program that prints your name on the first line, then prints
  a blank line, and then prints your address. (Hint: use the following
  code to print a blank line: \code{System.out.println();} )
\end{exercise}  


\pagebreak

\item\textbf{Activity 2-2-3: A program that creates a window.}

%% Definitions and phrase completions
  \phrase{What does API stand for?}

  \phrase{What does GUI stand for?}

  \phrase{What is a package?}

  \phrase{Which package deals with input and output?}

  \phrase{Which package deals with graphical user interface components?}

  \phrase{What must you put at the top of a class to announce that you want
    to use the graphical user interface package?}

  \phrase{Which class deals with windows?}

  \begin{exercise}
    Write a program that creates and shows three windows.  (Hint: you'll
    need three statements inside method \code{main}, instead of one.)
  \end{exercise}

       
\item\textbf{Activity 2-2-4: A program that draws.}

%% Definitions and phrase completions
  \phrase{Write down the two \code{import} statements that are shown in the
    code window:}

  \phrase{What is the name of the method that redraws the graphics window?}

  \phrase{When does method \code{paint} get called?}

  \phrase{Where is the origin (the (0,0) pixel) for drawing?}

  \phrase{What statement sets the drawing color?}

  \phrase{When talking about pixel (a,b), which coordinate comes first, the
    horizontal one or the vertical one?}

  \truefalse{Pixel (10,10) is closer to the bottom of the drawing window
    than pixel (40,10)).}

  \truefalse{Pixel (10,10) is closer to the bottom of the drawing window
    than pixel (10,40)).}

  \phrase{What statement would you use to draw the \code{String}
    \code{``Wahoo!''} at pixel (40,60)?}

  \begin{exercise}
    Make a copy of the program from this activity.  (Look on the lesson
    page for instructions on how to find it on the livetext, and be sure to
    get both of the files.) Modify method \code{paint} in class
    \code{Draw1} to draw a face: add \code{setColor}, \code{drawRect} and
    \code{drawOval} statements to the end of method \code{paint} to draw an
    oval face with blue eyes, a red rectangular nose, and a red, wide and
    thin oval mouth.

    You will find it very useful to sketch the face on paper first, in
    order to figure out roughly which pixel values to use for the various
    pieces of the face.

    For now, don't worry about drawing your face on top of the circles and
    rectangles that we drew: just get your face on the screen.
  \end{exercise}

\item\textbf{Activity 2-2-5: Exercises on simple programs.}

  The last Activity on Lesson page 2-2, an interactive exercise, will help
  you check whether you have digested the terminology that was introduced
  on this page. Be sure to take part in this Activity!


\end{description}




\pagebreak

%%% SECTION %%%
\section{Components of a Java program}

Lesson page 2-3 begins a detailed explanation of Java. This is the time to
begin learning terminology in earnest. When you finish this page, check to
make sure that you understand fully the terms that are discussed below.

\begin{description}

\item\textbf{Activity 2-3-1: Comments and whitespace.}

%% Definitions and phrase completions
\phrase{Describe the purpose of comments.}

\phrase[0in]{The form of comment that can span several lines begins with
  this: \blankfill{.3in} and ends with this: \blankfill{.3in}.}

\phrase{Write an example of a one-line comment:}

\phrase[0in]{Describe the five forms of whitespace:
\begin{enumerate}
  \item {}
  \item {}
  \item {}
  \item {}
  \item {}
\end{enumerate}
}

\truefalse{Comments are not a form of whitespace.}

\phrase{Where can whitespace appear?}

\phrase{Where can't whitespace appear?}

\begin{exercise}
  Go back to the face exercise from Activity 2-2-4 and ``comment out''
  the lines of code that produce the rectangle and oval and the two
  \code{String}s. Add comments above the lines of code you added to
  describe what the code you added does. Use both types of commenting
  syntax. Compile and run the code to make sure that your face is
  still being drawn correctly.
\end{exercise}


\pagebreak


\item\textbf{Activity 2-3-3: Keywords and indentifiers.}

\defn{keyword}

\phrase{Write the six keywords used in this activity:}

\defn{identifier}

\phrase[0in]{Write the rules for forming an identifier:
\begin{enumerate}
  \item {}
  \item {}
  \item {}
  \item {}
\end{enumerate}
}

\phrase{What are the two non-alphanumeric characters (characters that
are not letters or numbers) that can appear in identifiers?}

\truefalse{The identifiers \code{Dingbat} and \code{dingbat} are really the
  same, because Java ignores capitalization.}

\phrase[0in]{Write the two conflicting guidelines for identifiers:
\begin{enumerate}
  \item {}
  \item {}
\end{enumerate}
}

\item\textbf{Activity 2-3-5: Variables and types.}

  You don't have to memorize every little aspect of each type that you run
  across, because references are just a few mouse clicks away. Visit
  the glossary on the livetext when you see a word you don't understand.

  \defn{variable}

  \truefalse{Any keywords can be used as the name of a variable.}

  \defn{type}
  
  \defn{variable declaration}

  \phrase[0in]{Describe the three parts of a variable declaration:
    \begin{enumerate}
    \item {}
    \item {}
    \item {}
    \end{enumerate}
    }
  
  \phrase[0in]{The range of an \code{int} variable is \blankfill{1in} to
    \blankfill{1in} inclusive.}
  
  \phrase[0in]{The range of a \code{byte} variable is \blankfill{1in} to
    \blankfill{1in} inclusive.}

  You don't have to memorize the previous two items, but you should
  recognize that an \code{int} can hold a much, much wider range of values
  than a \code{byte}.

  \phrase{How is type \code{String} different from \code{char}, \code{int},
    and \code{byte?}}
  
\item\textbf{Activity 2-3-6: Expressions.}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                                %
%     \phrase[0in]{Write the rules for forming expressions of type \code{int}:   %
%       \begin{enumerate}                                                        %
%       \item {}                                                                 %
%       \item {}                                                                 %
%       \item {}                                                                 %
%       \item {}                                                                 %
%       \item {}                                                                 %
%       \item {}                                                                 %
%       \item {}                                                                 %
%       \item {}                                                                 %
%       \item {}                                                                 %
%       \item {}                                                                 %
%       \end{enumerate}                                                          %
%       }                                                                        %
%                                                                                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  \phrase{What is weird about division when both operands are of type
    \code{int}?}

  \phrase[0in]{Figure out the results of the following when calculated as
  part of a Java program. Assume all numbers are of type \code{int}:
  \begin{enumerate}
    \item {5 + 2 * 3 =}
    \item {5 + (2 * 3) =}
    \item {(5 + 2) * 3 =}
    \item {5 / 2 =}
    \item {7 / 2 * 4 =}
    \item {7 / (3 * - 2) =}
    \end{enumerate}
    Write a program that prints the value of each of these to verify 
    your answers.
  }
  
  \phrase[0in]{Given \code{x= 5; y= 2; z= 3;} what is the value of \code{z}
  after execution of each line of code? (Assume \code{z= 3;} at the
  start of each line.)
  \begin{enumerate}
      \item {\code{z= 4;}}
      \item {\code{z= y;}}
      \item {\code{z= 22 / x;}}
      \item {\code{z= x + z;}}
      \item {\code{z= x + (z * y);}}
      \item {\code{z= y + z * x;}}
      \item {\code{z= (3 * (- (z)));}}
      \end{enumerate}
    Write a program that prints the value of each of these to verify 
    your answers.
  }

\end{description}


%%% SECTION %%%
\section{Three statements: assignment, condition, block}

\begin{description}
  
\item\textbf{Activity 2-4-1: The assignment statement.}
  
  %% Definitions and phrase completions
                                % 
  \phrase{Write the form of an \textit{assignment}:\footnote{Look in
      the Glossary.}}
  
  \phrase{Write the form of a \textit{variable declaration}:}
  
  \phrase[0in]{Write the steps to executing an assignment statement:
    \begin{enumerate}
    \item {}
    \item {}
    \end{enumerate}}
  
  \truefalse{A variable may only be assigned a value once during
    execution of a program.}
  
  \begin{exercise}
    The statement $$\code{int numberOfFeet= 2;}$$ stores the value
    \code{2} in variable \code{numberOfFeet}, and the code
    $$\code{System.out.println(numberOfFeet);}$$ prints the value stored in
    \code{numberOfFeet}.
    
    You should all recognize the pythagorean formula $a^2 + b^2 =
    c^2$ for calculating the length of a hypotenuse of a right
    triangle.  Write a program that declares an \code{int} variable
    called \code{cSquared}. Variable \code{cSquared} should store the
    square of the length of the hypotenuse of a triangle with side
    $a$ of length 3 and side $b$ of length 5. The last statement of
    the program should print the value of \code{cSquared}.
  \end{exercise}
  
  
\item\textbf{Activity 2-4-2: Example: swapping the values of two
    variables.}
  
  %% Definitions and phrase completions
                                % 
  \begin{exercise}
    Write a program that first declares three variables, \code{a}, \code{b}
    and \code{c}. Next, assign \code{a} the value 1, \code{b} the value 2,
    and \code{c} the value 3.
    
    Now write a sequence of statements to rotate those values, so that
    afterward \code{a} has the value that \code{b} had, \code{b} has the
    value that \code{c} had, and \code{c} has the value that \code{a} had.

    Hint: you will need to use a temporary variable.
   \end{exercise}
   
  
\item\textbf{Activity 2-4-3: The if-statement.}
  
  %% Definitions and phrase completions
                                % 
  \phrase{Write the form of an if-statement:}

  \phrase{What are the possible values that a boolean expression can
    evaluate to?}

  \phrase{What happens if the boolean expression evaluates to
    \code{true}?}
  
  \phrase{What happens if the boolean expression evaluates to
    \code{false}?}
  
  \begin{exercise}
    Complete the following program:
    \\\textsf{Make up something that declares
      some variables and needs and if-statement.}
  \end{exercise}

\item\textbf{Activity 2-4-4: The if-else-statement.}
  
  %% Definitions and phrase completions
                                % 
  \phrase{Write the form of an if-else-statement:}
  
  \phrase{What happens if the boolean expression evaluates to
    \code{true}?}
  
  \phrase{What happens if the boolean expression evaluates to
    \code{false}?}
  
  \begin{exercise}
    \\\textsf{Make up something that declares
      some variables and needs and if-else-statement.}
  \end{exercise}


\item\textbf{Activity 2-4-5: Boolean expressions.}
  
  %% Definitions and phrase completions

  \phrase{List all the elements in the set of \code{boolean} values:}

  \defn{boolean expression}

  \defn{proposition}

  \phrase{List six mathematical expressions that produce a boolean value.}

  \phrase{List three boolean operators.}

  \begin{exercise}
    Write a program that prints the values of the following expressions.
    (Make sure you understand the output!)

    \begin{verbatim}
    true && false
    true || false
    false || false
    false && false
    false && true
    false || true
    false && !true
    !false
    !true
    \end{verbatim}
  \end{exercise}

                                % 
\item\textbf{Activity 2-4-6: Blocks.}
  
  %% Definitions and phrase completions
                                % 
  \phrase{Write the form of a block:}

  \truefalse{Because all of the statements in a block get executed, it
    doesn't matter what order they are in.}

  \phrase{Write the indentation rule:}

  \begin{exercise}
    There are errors (both with the uses of blocks and with indentation) in
    this program. Fix them.
\begin{verbatim}
    public class ErrorRiddled {
        public static void main(String[] args) {
            
        }
    }
\end{verbatim}
  \end{exercise}

  
\item\textbf{Activity 2-4-7: Assignment, conditional and block statements.}
  
  %% Definitions and phrase completions
                                % 
  \begin{exercise}
  \end{exercise}

  
\end{description}

