1: Getting Started

What We Will Cover


Lab Exercise 1.1

Instructions

Use the next 5 minutes to complete the following.

  1. Join a group of 3-4 people and prepare an answer to one of the following questions.
  2. Select one member from your group as a representative to describe your answer to the class.

Exercise Questions

  1. What "burning questions" do you have about the course outline?
  2. What do you hope to learn by the time you complete this course?
  3. How do you plan to balance the work required by this course with the other activities in your life?

1.2: Succeeding in This Course

Objectives

At the end of the lesson the student will be able to:

  • Describe how to study effectively for this course
  • Use computers in the CTC for homework
  • Install software used in this course at home

1.2.1: Elements of Success

  • This course has a simple instructional design:
    1. You learn the material by reading the textbook and attending lectures
    2. You master the material by writing programs
    3. You measure your mastery by taking tests

Learning the Material

  • You learn the material by reading the book and attending lectures
  • When you attend class, you need to pay attention and participate in discussions
    • Volunteer answers
    • Respond to direct questions
  • When you miss a class, you need to catch up before the next class meeting
    • Lecture notes are posted online
  • This is a fast-paced course and you need to keep up

Programming Assignments

  • Programming assignments have you analyze problems and apply what you have learned in new ways
  • Since you are applying your learning in new ways, you may get stuck
  • If you get stuck, then here are some things you can do:
    • Reread the assignment instructions
    • Review the lecture notes
    • Read the textbook
    • Discuss it with your pair-programming partner
    • Post a message in the discussion board.

    • Contact the instructor
  • You should submit homework to WebCT at least the day before it is due
    • Even if you are not done, turn it in
    • You get some credit for what you did complete
    • Make sure to state an accurate status in the README.txt file
  • Note that homework deadlines are real
    • Remember that late homework is not accepted
    • What is the average of 0 and 100?
    • Instead, most homework has extra credit possibilities

Preparing for Exams

  • Most students do not have perfect memories
  • You need to refresh your memory about programming details before an exam
  • Plan on spending time studying for exams
  • I will discuss study techniques before the exams

1.2.2: Time Estimates

  • Students often ask how much times is needed for a course
  • Estimated average course time per week:
    • 1-2 hours reading the lecture notes and textbook before class
    • 3 hours in the classroom
    • 8-10 hours for programming assignments (excluding extra credit)
  • I collect time estimates from students for programming assignments

1.2.3: Using the CTC for Homework Assignments

  • First you will need a login and password
  • You can use either Linux, Macintosh or Windows computers for your assignments
  • For more information, follow the link to the CTC homepage

1.2.4: Can I Run This On My Own Computer?

  • All the technology we use runs on most any computer
  • Instructions on How To Install Java available from the HowTo's page
  • Also has instructions for installing jEdit and TextPad

1.2.5: Summary

  • Elements of success in this course:
    1. Learn the materials by reading the textbook and attending lectures
      • When you attend lectures -- participate!
    2. Do your programming assignments and turn them in on time
    3. Prepare for and attend exams
  • You can complete homework assignments in the CTC
  • Also, you can install the necessary software at home or work

Exercise 1.2

Take one minute to prepare an answer to the following question:

Why is the weekly time estimate for programming assignments longer than the time estimates for reading the book and attending class combined?

1.3: Introduction to Java

Objectives

At the end of the lesson the student will be able to:

  • Describe how Java compares with C++
  • Name and describe three types of programs you can create with Java

1.3.1: History of Java

  • 1990 - "Green" project started at Sun
    • Java originally called Oak
    • Developed for digitally controlled consumer devices and computers
  • 1991 - Java created by James Gosling, Sun Microsystems, Inc.
  • 1992 - "Green" project finished
  • 1992 - 1994 - Tried to turn "Green" technology into products
    • Failed
    • But then the Internet happened...
  • 1996 - Official launch of JDK 1.0
    • Primary focus was Applets
  • 1997 - Sun releases JDK 1.1
  • 1998 - Sun releases SDK 1.2
    • Now called the Java Platform
  • 2000 - Sun releases SDK 1.3
  • 2002 - Sun releases SDK 1.4
  • 2004 - Sun releases JDK 1.5
  • Today a general-purpose programming language used extensively

More information

1.3.2: Applets, Applications and the Internet

  • What is java used for today?

Applets

  • Applet: a Java program that runs in a browser
  • Original impetus for Java was applets
  • Two famous applets captured the attention of Netscape
  • Applets can show 2D and 3D images, play sound, and show video

Desktop Applications

  • Applications are programs that perform specific functions for users
    • Word processors
    • Spreadsheets
    • Drawing programs
    • Games
  • Examples:
    • GTGE: Game programming library for 2D game development
    • jEditText editor written in Java
    • NetBeans IDE: integrated development environment for programming appications
    • Thinkfree Office: Wordprocessing, spreadsheet and graphics suite written in Java
  • Most modern applications use a Graphical User Interface (GUI)
  • Java provides a complete set of APIs for programming GUIs
    • Will use GUIs in second half of the course
  • Further information: Java Desktop Development

Mobile Applications

  • Java installed on many cell phones provides new opportunities for:
    • Mobile games
    • Productivity applications like expense trackers
    • Messaging applications like auction tracking
    • Information applications like news and stock info
  • A lot of development work going on in this area

Web-Based Applications

  • Web applications use the Web as a user interface for more traditional applications
  • Examples: online banking, online shopping, online (your term here)
  • Java is the dominant programming language for developing these types of applications
  • Java provides special server-side tools known as a servlets and JSP
  • A similar "hot" area is Web services, where different Web applications communicate with each other over the Web
  • May Web-based games are written in Java
  • We will look at web applications more closely toward the end of the course

More Information

1.3.3: Java Compared to C++

  • Syntax intentionally similar to C++
    • Easy for C++ programmers to move to Java
  • Has several important advances over C++
  • All programs are encapsulated in classes
    • "Pure" object-oriented language
    • OOP improves programmer productivity (>2X)
  • Automatic memory management ("garbage" collection)
    • Improves memory management and reliability
  • More intelligent use of pointers (references)
    • No pointer arithmetic
    • Simpler model
    • Less error prone and more reliable
  • Build-in bounds checking of arrays
    • Helps prevent buffer overflow
    • Buffer overflows are one of the leading causes of computer vulnerabilities
  • Java has a much better security model
    • Java environment is not friendly to viruses or to Trojan horses
    • Very few Java viruses because of the security model
    • Always possible to get viruses due to errors in the JVM
    • For example, Microsoft's JVM has a known bug (see here)
  • Built-in support for multithreading
  • Large class-libraries
    • Known as APIs (Application Programming Interfaces)
  • Runs without recompiling on many operating systems and hardware platforms
    • Uses a virtual machine
  • Fastest Java runs slower than fastest C++
    • Originally about 10x slower, now about 2x with "Hotspot" compiler
      • "Hotspot" server compiler even faster
    • Compilers exist to translate directly to native machine code (GCJ)
    • Lose portability when compiled
    • Moore's law makes this issue less important every day

What About C#?

  • Despite the hype, it has about the same performance as Java

1.3.4: Summary

  • Java used to develop many types of programs including:
    • Applications: programs that perform specific functions for users
    • Applets: a Java program that runs in a browser
    • Servlet: server-side application
  • Java often compared to C++
    • Similar syntax
    • Bounds-checking of arrays
    • Automatic memory management (not present in C++)
    • Better model for using pointers (references)
    • Built-in support for multithreading
    • Runs slightly slower than C++

Exercise 1.3

Take one minute to prepare an answer the following question.

What are the pros and cons of using Java versus C++?

1.4: Java Programming Process

Objectives

At the end of the lesson the student will be able to:

  • Compile and run Java programs, given the source code
  • Display program output to the command line
  • Explain how a virtual machine helps Java achieve platform independence

1.4.1: Writing, Editing and Saving Source Code

  • Developing Java code consists of three steps
    1. Writing the source code
    2. Compiling the source code to bytecode
    3. Executing (running) the program
  • This process is repeated many times during development
  • You can use any text editor to write, edit and save Java source code
    • Windows: jEdit, Notepad, WordPad, TextPad, ...
    • Linux/UNIX: jEdit, VIM, gedit, pico, ...
    • Mac: jEdit, SimpleCap, ...
  • Make sure to save the file using the four-letter extension: java

About Using IDEs

  • Many Integrated Development Environments (IDEs) available for Java
    • Typically provide visual tools for designing forms and debugging code
  • I do not recommend using an IDE while learning Java
    • IDEs will generate code for you, which does not help you learn
    • IDEs are complex tools that themselves are difficult to learn
    • IDEs can distract you from your goal of learning Java
  • I do recommend using IDEs once you have a good understanding of Java
    • Easier to develop user interfaces
    • Improves programmer productivity
  • Any program you submit must run from the command line

1.4.2: Examining a Sample Program

  • Here is an example program we can save as a source code file
  • public class HelloWorld {
         public static void main(String[] args) {
            System.out.println("Hello, world!");
        }
    }
    
  • Copy this into a text editor and save it as HelloWorld.java
  • Note each line of code -- what purpose do they have?

1.4.3: Compiling and Running Programs

  • Our next step is to compile and run the sample program

Compiling Source Code

  • Open a window with a command prompt
  • Change directory (cd) to where program source code is stored
  • Type: javac HelloWorld.java
  • If no errors, creates HelloWorld.class

Running a Program

  • Type: java HelloWorld
    • Interpreter loads .class file for HelloWorld
    • Note that .class suffix is not typed
  • Interpreter calls the main method by default

Using Text Editors

  • Many text editors have provision for compiling within the editor
  • TextPad: Tools menu
  • jEdit: Click the java and javac tabs

Running Java as a Windows .exe

  • Windows users are used to double-clicking an icon to launch an application
  • Java does not support this type of action directly
  • However, you can use the Marner Java Launcher to achieve this effect

More Information

1.4.4: How Java Compiles and Interprets Code

Three steps to creating a Java program:

  1. Editing
    • Write program source code using any text editor
    • Save source code on file system or network
    • Source code files have .java suffix
  2. Compilation
    • Use a compiler to translate the source code to bytecodes
    • Files with translated bytecodes have .class suffix
  3. Execution
    • Use java interpreter program to run the bytecodes
    • Class Loader: moves bytecodes from file system or network into memory
    • Bytecode Verifier: tests bytecodes to establish safety and security
    • Interpreter (JVM): translates bytecodes into machine language

Java Virtual Machine (JVM) makes "write once, run anywhere" possible

More Information

1.4.5: Summary

  • Java program development uses these three steps:
    1. Write and edit code with an editor (or IDE)
    2. Compile to bytecode using a compiler
    3. Execute using a JVM
  • You can use most any text editor for writing and editing source code
  • To compile an application, use the javac command
  • To run an application, use the java command
  • Many text editors provide features to make it easier to compile and run programs
  • Once you have mastered the basics of Java, you should use an IDE
  • While you are learning, it is better to use a text editor

Exercise 1.4

Take one minute to prepare an answer the following question:

What commands do you type at the command line to compile and run a class named Foo.java?

1.5: Basic Coding Skills

Objectives

At the end of the lesson the student will be able to:

  • Code comments in programs and describe the uses of each type of comment
  • List the rules for creating an identifier and the conventions used in creating class and method names
  • Explain the term keyword

1.5.1: Coding Comments

Comments

  • Comments are ... comments -- notes to people reading the code
  • Comments are ignored by compiler so you can put them anywhere in your program
  • You should use comments sparingly but whenever needed to understand your code
  • Comments can start with // and last to end of the line
  • // this is a comment
  • Comments can span multiple lines: /* ... */
  • /* This is a multi-line comment
          which can be split
       over many lines or a portion of one line. */
    

Javadoc Comments

  • Javadoc comment is a subset of a multiline comment
    • Starts with the begin-comment symbol: /**
    • Last line begins with the end-comment symbol */
    • Has a single asterisk "*" at the start of every line
    • Notice that the asterisks line up (by convention)
    /**
     * Description part of a Javadoc comment
     *
     * @tag Comment for the tag
     */
    
  • Javadoc comments have two parts:
    1. Description of the code
    2. Followed by zero or more tags
  • You use Javadoc comments before every class and method of your program
  • Javadoc utility uses these types of comments to automatically create program documentation
  • More information: How to Write Doc Comments for the JavadocTM Tool
  • For a list of tags: Tag Conventions
  • Important in your Java assignments: How To Document and Organize Your Java Code

1.5.2: Coding Statements and Using Whitespace

Statements

  • Statements direct the operation of the program
  • You can start a statement anywhere on a line and continue on the next line
  • Most statements end in a semicolon (;)
  • However, statements using braces {} end with a right brace

Whitespace

  • Whitespace: blank lines, spaces, and tabs
  • Whitespace ignored by compiler
  • Use whitespace to make programs more readable

Programming Style: Line Length

  • Limit your line length to 80 characters
  • Longer lines can cause problems with many text editors and other tools

1.5.3: Creating Identifiers

    Identifier - the name of something (such as a variable, object, method or file) used in a program.

  • Purpose of an identifier is to provide a name for something
    • Should always try to make the name meaningful
  • Programmers create identifiers for the names of many things in a program:
    • classes
    • methods
    • variables
  • Identifiers are made up of a sequence of characters
  • Each character can be a:
    • Letter
    • Digit
    • Underscore: _
    • Currency symbol (such as $, ¢, £, ¥)
  • Can not start with a digit
  • Can not contain any periods or spaces
  • Can not be a keyword (such as "if, "for", etc.: see Java Language Keywords)
  • Are cAsE sEnSiTiVe!
    • id, ID, iD and Id are all valid but different identifiers
  • Identifiers have naming conventions:
    • Use meaningful names
    • Use only letters and digits
    • Start class names with an uppercase letter
    • Start variable and method names with a lowercase letter
    • Capitalize interior words in multi-word names (e.g. myAge)

Keywords

  • Keywords are reserved words with predefined meanings
  • We used keywords in our HelloWorld program
    • public
    • class
    • static
    • void
    public class HelloWorld {
         public static void main(String[] args) {
            System.out.println("Hello, world!");
        }
    }
    
  • All keywords have special meaning in Java
    • Can only be used for their specified purpose
    • Attempting to use for any other purpose will generate a compiler error
    • Thus cannot use keywords for identifiers
  • For a current list, see Java Language Keywords

1.5.4: Declaring a Class

  • Referring to our example program we look at the first line
public class HelloWorld {
     public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

Declaring a class

  • When you develop a Java program, you code one or more classes for it
  • Each class starts with a declaration like the following:
  • public class HelloWorld {
        // class definition goes here
    }
    
  • Keyword public means all parts of the program can use this class
  • Keyword class tells compiler that a class is being defined
  • Left brace { begins body of every class
  • Likewise, right brace } ends body of every class
  • Any code between the curly braces is called the class definition

1.5.5: Declaring a main() Method

  • Again referring to our example program, we look at the main() method
public class HelloWorld {
     public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

Declaring a main Method

  • Every Java program has one or more methods
  • Methods are blocks of code that perform a task
  • Similar to functions in other programming languages
  • public static void main(String[] args) {
    
  • Every Java application must have a method called main
    • Applications begin executing at main method
  • Keyword public allows any program to use the main method in this class
  • void means main has no return value
  • args is a parameter of type String[] -- cover later
  • For now, mimic the first line of main
  • Left brace { begins body of every method
  • Right brace } ends body of every method

1.5.6: Summary

  • Comments help document what a program does
    • Useful for humans to read
  • Java has three styles of comments
    • //
    • /* ... */
    • /** ... */
  • Statements direct what a program does
  • Use whitespace to make programs more readable
  • Identifiers are any name you create in a Java program
  • Keyword is a special word reserved by Java
  • Literals are strings of characters the represent constant data values
  • You develop Java programs writing one or more classes
  • Every Java application starts with a main method

Exercise 1.5

Take one minute to prepare an answer the following problem.

List all the identifiers in the HelloWorld program.
public class HelloWorld {
     public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

Wrap Up

Home | WebCT | Announcements | Schedule | Room Policies | Course Info
Help | FAQ's | HowTo's | Links

Last Updated: February 09 2005 @12:38:20