Document Sample
IM_Chapter_02 Powered By Docstoc
					Fundamentals of Java                                                   Chapter 1: Background

       Discuss why Java is an important programming language.
       Explain the Java virtual machine and byte code.
       Choose a user interface style.
       Describe the structure of a simple Java program.
       Write a simple program.
       Edit, compile, and run a program using a Java development environment.
       Format a program to give a pleasing, consistent appearance.
       Understand compile-time errors.
       Write a simple graphics program.

Teaching Materials
       Student text
       PowerPoint presentation from the PowerPoint Presentations drop-down menu
        on the Instructor Resources CD
       Solutions to exercise questions, review questions, and programming projects from
        the Solutions to Exercises drop-down menu on the Instructor Resources CD
       ExamView test questions from the Test Bank & Test Engine drop-down menu
        on the Instructor Resources CD

       Focus students’ attention on the objectives for the chapter.
       Set up a projection system and show the PowerPoint presentation for the chapter,
        if desired.
       Make sure students know how to access the example program files for this
       Prepare questions from ExamView.

Technical Notes
Make sure that Java 5.0 is installed on all computers.

Lecture Notes and Teaching Tips
The short, simple programs in this chapter are easy to present, demonstrate, and discuss.
The best way to teach them is to take students through each step of the process — edit,
compile, and run. First, run them off the shelf to show the results, and then edit one to
insert a syntax error to demonstrate what happens during syntax checking. Fix the error,
compile, and run again.

Program code consists of data and operations. Call attention to these elements in each
program. The programs use strings and numbers as data and perform input, arithmetic,

Fundamentals of Java                                                  Chapter 1: Background

and output as operations. Using short, simple programs that produce immediate results
dispels any mystery and motivates students to create their own programs.

Why Java?
Discuss with students what, if any, programming languages they are familiar with. This
could lead into a discussion about the ―pros‖ of using Java – in particular, that it is
secure, robust, and portable.

Quick Quiz
1. True or False? A Java program can be written and compiled on one type of computer
   and then run on an entirely different type of computer.
   Answer: True

The Java Virtual Machine and Byte Code
Discuss the advantages and disadvantages of using an interpreter. Highlight portability
and the ability to run programs in Web browsers.

Quick Quiz
1. True or False? Programs that run with interpreters typically run more slowly than
   those that have been compiled to machine code.
   Answer: True
2. What kind of Java program runs in a Web browser?
   A. application                                   B. applet
   Answer: B

Choosing a User Interface Style
A program with a GUI may look more appealing, but there are reasons for beginning with
terminal I/O. Discuss the differences between these two interfaces and why one would
use each.

Quick Quiz
1. The term GUI stands for __________.
   Answer: graphical user interface
2. A terminal-based user interface uses which device for input?
   A. mouse                                        C. keyboard
   B. microphone
   Answer: C

Hello World
The first Java program that students see just performs output. Show the output first and
then the program code. Call attention to the portion of the code that produces the output.
Examine in detail the syntax of the println statement. It consists of an object
(System.out), a period (.), a message (println), and a parameter (a string). Java
programs typically send messages with parameters to objects. Finally, mention that the
surrounding framework is the same for most of the short programs at the beginning of the

Fundamentals of Java                                                    Chapter 1: Background

Quick Quiz
1. What kind of data gets displayed as text in a terminal window?
   A. message                                        C. object
   B. string
   Answer: C
2. The message __________ is used to send output to the terminal window.
   Answer: println

Edit, Compile, and Execute
This is a detailed presentation of the coding phase mentioned in Chapter 1. It will help to
show a command line (DOS, Unix, or Linux) environment with a text editor to highlight
the edit/compile/run cycle before using another IDE. Students need to see compile-time
errors right away and be aware of code-formatting conventions. They need to understand
the role of each software tool plus the difference between a source code file and a byte
code file. Show them a byte code file that’s generated from a successful compilation.

Quick Quiz
1. When a Java program compiles without errors, the compiler places a(n) __________
   in the current directory.
   A. source code file                            C. byte code file
   B. object
   Answer: C
2. True or false? A Java source program can appear on a single long line of text.
   Answer: True

Temperature Conversion
This example program illustrates the use of variables, input, arithmetic, assignment
statements, and output to solve a problem. Many of the simple early programs follow this
pattern. Focus attention on each of these concepts in the context of the example program.
Students also see how to create a new object, a Scanner, to read input from the keyboard.
Call attention to the concepts of class, object, instantiation, message, and parameter that
were mentioned in Chapter 1. Finally, introduce the distinction between numbers and
objects, highlighting the idea of an object as a resource with its own operations, as shown
in Figure 2-11. This is an overview; all of these concepts are revisited in more depth in
Chapter 3.

Quick Quiz
1. Symbols that refer to values that can change are called __________.
   A. variables                                      B. methods
   Answer: A
2. True or false? The Scanner class is used to create an object that can read input from
   the keyboard.
   Answer: True

Graphics and GUIs: Windows and Panels

Fundamentals of Java                                                   Chapter 1: Background

Students can explore this section for fun and an extra challenge. GUI programs are easy
to get up and running, but can involve complexities that are absent from terminal-based
programs. Focus on the services that the JFrame class provides for the user who sees and
manipulates a window and for the programmer who must write the code. Again, show the
output windows before showing the code for the examples. Call attention to the actions of
the methods in Table 2-1. When discussing Java’s way of representing colors (page 48),
refer back to the discussion of RGB in Chapter 1. Finally, explain how frames, panels,
and layout managers divide the responsibilities in collaborating to produce a GUI.

Quick Quiz
1. A __________ represents a rectangular area in a window.
   A. frame                                        B. panel
   Answer: B
2. The __________ of a window can be set by the methods of the JFrame class.
   Answer: attributes

Discussion Questions
1. Jack has never written and run his own Java program. Give detailed instructions that
   will allow him to accomplish this.
2. There are three basic operations in a simple interactive program: input, computations,
   and output. Describe the purpose of each of these types of operations.
3. If Jill makes a syntax error in her Java program, the compiler will detect it and
   display an error message. Why should Java programs not run with syntax errors?

Key Terms
       Applet: A Java program that can be downloaded and run on a Web browser.
       Assignment Statement: A method of putting values into memory locations.
       Byte Code: The kind of object code generated by a Java compiler and interpreted
        by a Java virtual machine. Byte code is platform independent.
       DOS development environment: A development environment that uses Notepad
        for the editor with command line activation of the compiler and the JVM from
        inside a command or DOS window.
       Graphical user interface (GUI): A means of communication between human
        beings and computers that uses a pointing device for input and a bitmapped screen
        for output. The bitmap displays images of windows and window objects such as
        buttons, text fields, and pull-down menus. The user interacts with the interface by
        using the mouse to manipulate the window objects directly.
       Hacking: A style of computer programming that uses clever methods to solve
        difficult programs with little apparent effort, usually to impress peer
       Integrated Development Environment (IDE): A development environment that
        combines an editor, a Java compiler, a debugger, and a JVM in a manner intended
        to increase programmer productivity.
       Java Virtual Machine (JVM): A program that behaves like a computer. Some
        JVMs translate byte code instructions into machine language when they are first

Fundamentals of Java                                                     Chapter 1: Background

        encountered so that the next time the instruction is encountered, it is executed as
        fast machine code.
       Just-in-time Compilation (JIT): The act of translating byte code instructions
        into machine language when they are first encountered.
       Parameter: A value or expression passed in a call to a method.
       Source code: The code written by the programmer.
       Statement: A unit of programming code that performs a task.
       Terminal I/O interface: An interface style that displays prompts.
       Variable: A memory location, referenced by an identifier, whose value can be
        changed during a program.

Projects to Assign
       In Project 2-1, students will write a program that displays their name, address, and
        telephone number.
       In Project 2-2, students will write a program that displays a yield sign.
       In Project 2-3, students will write a program that takes as input a number of
        kilometers and prints the corresponding number of nautical miles.
       In Project 2-4, students will write a program that calculates and prints the number
        of minutes in a year.
       In Project 2-5, students will write a program that expects an object’s mass (in
        kilograms) and velocity (in meters per second) as inputs and prints its momentum.
       In Project 2-6, students will write a program that displays images of national
       In Project 2-7, students will write a program that displays a 3 by 3 grid of black
        and white rectangles. The rectangles should be positioned so that no two
        rectangles of the same color are adjacent to each other.
       In the Critical Thinking Activity, students will discuss an interview with the
        owner of a pizza shop to develop requirements for a program to help her process
        takeout orders.

Administer the ExamView test for Chapter 2.