Programming 1, fall 2007 -- Demonstration 9

Recap. The block and statement structure of Java follows this kind of syntax:

Block = (
        "{" BlockStatements "}"
);

BlockStatements = (
        { BlockStatement }
);

BlockStatement = (
        LocalVariableDeclarationStatement
      | ClassOrInterfaceDeclaration
      | [Identifier ":"] Statement
);

Statement = (
        Block
      | "assert" Expression [ ":" Expression] ";"
      | "if" ParExpression Statement ["else" Statement]
      | "for" "(" ForControl ")" Statement
      | "while" ParExpression Statement
      | "do" Statement "while" ParExpression   ";"
      | "try" Block ( Catches | [Catches] "finally" Block )
      | "switch" ParExpression "{" SwitchBlockStatementGroups "}"
      | "synchronized" ParExpression Block
      | "return" [Expression] ";"
      | "throw" Expression ";"
      | "break" [Identifier]
      | "continue" [Identifier]
      | ";"
      | StatementExpression ";"
      | Identifier ":" Statement
);

The above notation is a dialect of BNF. Quotation marks denote terminal symbols (written literally); other notations should be familiar from previous demonstrations. Blocks and statements are the basic, operational building blocks of programs. On the more abstract level, statements are grouped within methods, classes, and packets. On the microlevel, many of the statements have their own internal syntax. For example, you must write ForControl within the parentheses in the for -statement.

You have to remember that the purpose of the syntax is just to allow you to describe an algorithm for computation. Each statement (and, more generally, each syntactic construct) therefore has its own semantics -- the way how it translates to the flow of computation, and the use of the memory of the computer. Most of the time, your statements include expressions that compute new values for variables. An important kind of expression is the method call (subroutine call), which allows a very powerful way of abstracting computer operation. The power comes from using parameters and return values, and, as we'll see later, the internal state of objects.

Postponed from previous sessions:

For the first three, we'll look at example answers to the questions in demonstration 8. For the last one, we look at reading integer input from the console. Later we'll look at file input/output in Java.

Preview of the exam

You will find the structure of the exam of this course as a PDF file there:

http://www.cc.jyu.fi/~nieminen/ohj1/english/examples/ohj1_exam.pdf

The exam questions outline the most important skills that I want people to aquire from this course.

Exercise 1: Exam preview

Try to provide answers to the exam questions. Do not guess, if you are unsure whether your answer is the correct one or not. You must be sure, and if you are not, you must gain more skill, and ask help from others on the mailing list!

It is very important that you work with the exam questions before next Friday. We will go through example answers then.

Exercise 2: A small project work

In order to make the English course fully parallel to the Finnish one, we need to make a "capstone" project work -- a small program that works as a meaningful application.

Alas, I've had no time to translate the project work assignments, so we might have to wait a couple of days. If you can think of your own topic, you can make a suggestion. The program needs to work from the command line, using only console interaction.