1. Course Introduction

What We Will Cover


1.1: Java Programming

Learner Outcomes

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

  • Describe what they will learn in this course
  • Discuss how Java got started

1.1.1: Introduction to Java Programming

  • Everyone here should have either a previous programming course or experience in programming
  • However, not everyone has experience with Java programming
  • During the first few weeks we will review the basics of Java and then move on to the more advanced material

Why Java?

  • The most popular programming language today is Java (see: tiobe.com)
  • Where did Java come from? Why is it so popular?
  • To answer the first question, we start with a short history of Java

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 (Java 2)
  • 2000 - Sun releases SDK 1.3
  • 2002 - Sun releases SDK 1.4
  • 2004 - Sun releases JDK 1.5 (Java SE 5)
  • 2006 - Sun releases JDK 1.6 (Java SE 6)
  • Today the most widely-used general-purpose programming language

More information

1.1.2: Why Java is Popular

  • We write programs to solve real world problems with a computer
  • To get started on solving problems, Java provides a huge set of class libraries
    • Known as the Java API (Application Programming Interface)
  • Libraries provide prewritten code that is relatively mature and stable
  • The libraries include code such as:
    • Input/Output: for many devices besides the console
    • Math: usual function plus support for arbitrary precision numbers
    • 2D Graphics (3D is an add on library)
    • Graphical User Interfaces: buttons and widgets galore
    • Database connectivity
    • Security management
    • Multi-threading
    • Sound: sampled and MIDI based
    • XML processing
    • Industrial strength cryptography
    • W3C DOM processing
  • With these core libraries, you do not need to spend time looking for third-party libraries
  • With Java, you can just start "doing stuff"

Other Reasons

  • Automatic memory management ("garbage" collection)
    • Improves dynamic memory usage and reliability
  • More intelligent use of pointers (references)
    • Simpler model that is less error prone and more reliable
    • Automatic dereferencing when needed
  • Built-in bounds checking of arrays
    • Helps prevent buffer overflow
    • Buffer overflows are one of the leading causes of computer vulnerabilities
  • Much better security model
    • Java environment is not friendly to viruses or to Trojan horses
    • Very few Java viruses exist because of the security model
    • Note that it is 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
  • Runs without recompiling on many operating systems and hardware platforms
  • Active and open support network with millions of developers
  • Not dependant on a single supplier (like Microsoft)

Performance

  • In the beginning, Java earned a reputation as a slow language
  • However that situation has changed as Java tools have progressed
  • Today, Java runs about the same speed as C++
    • Java Virtual Machine compiles code to the native machine code while the program is running
    • Compiling while the code is running is called Just in Time (JIT) compiling
    • JIT compiling contrasts with Ahead Of Time (AOT) compiling
    • Thus a Java program may start slower but quickly reaches the same speed, or better, than AOT compiled code
  • Several studies argue that Java can be faster than C++ because:
    • JIT compilers tailor the program optimization to the particular processor, memory and program "hot-spots", rather than a more general optimization
    • C++ pointers make optimization difficult since they may point to arbitrary data or code
    • In Java, newly allocated data is often closer together because the garbage collector knows what parts of memory are available
    • Data that is closer together are more likely to be in the computer's cache memory
  • Note that compilers exist to translate directly to native machine code ahead of time (GCJ)
    • Thus, your computer would run the code like it runs C++
    • However, you lose portability when you compile to native code ahead of time

More Information

1.1.3: Examples of What You Will Learn

  • As we learn how to program, we will solve some interesting problems
  • Let us take a look at some examples of what you will learn in this course
  • First we have some samples from previous homework assignments:
  • I have not decide on all the programming assignments, but these are some possibilities

Course Projects

  • Toward the end of the course you will complete a project
  • The purpose of the project is to utilize what you learned during the course to produce your own unique program
  • In previous semesters students have completed many fascinating projects including:
    • Adventure game
    • Android App: for Google's new cell phone platform
    • Bank account programs
    • Bibliograph program: tracking books and authors
    • Card games
    • Computer-based "board" games
    • Cryptography applications
    • Custom Web browser
    • Database programs (store and retrieve selected data)
    • Flash Card application: study aid
    • Home Automation Control System
    • Protein Viewer
    • Recipe System: with text and images
    • MP3 player
    • Video games
  • You too will be able to create programs like these

1.1.4: Summary

  • Java started development in 1991 and was officially released in 1996
  • Java is now a widely-used general-purpose programming language
  • Java has extensive class libraries covering graphics to threads
  • With these libraries, you do not need to spend time looking for third-party libraries
  • Instead, you can just start "doing stuff"
  • In addition to extensive libraries, Java has automatic garbage collection
  • The usual C/C++ problems with memory leaks are gone
  • Also, Java has many other features that we will explore this semester
  • We looked at some examples of the programming problems you could solve after completing this course

Exercise 1.1: Introductions and Questions

Use the next 10 minutes to complete the following.

  1. Join a group of 3-4 people and introduce yourself to each other.
  2. As a group, develop one "burning question" about the course.
  3. Select one member from your group as a spokesperson to describe your single question to the class.

Note: You do not have to turn in the answers to this exercise.

1.2: Exploring the Course

Learner Outcomes

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

  • Navigate the course web site
  • Discuss how the course is administered
  • Know when assignments are due

1.2.1: Some Useful Information and Resources

Email

  • All students are requested to have an email account
  • Please make sure your email address is correct in WebAdvisor
  • When sending me an email please state your name and course number
  • Please make sure your emails are professional, that words are spelled correctly and that you use good grammar
  • If you expect a reply, make certain your spam filter is not removing my emails
  • If you have not received my reply with a day, then check your spam folder before sending me another email

Open Lab Hours

  • The schedule of classes lists lab times for you to complete your programming assignments

    +6 hr 20 min open lab per week.

  • These open lab hours are at times of your choosing in the Computer Technology Center (CTC)
  • The CTC is open, supervised and available for use as shown here: Computer Technology Center Hours
  • It is your responsibility to make use of these hours every week to work on your exercises and programming projects
  • All students enrolled in the lecture section must fill out and sign the "Student Hours by Arrangement" before you leave today
  • Anyone who does not complete the form will be dropped from the course
  • Your instructor for these hours is listed as Barbara Durland, but other instructors and lab assistants may be available to help you
  • Your instructor will be in the lab as shown here: Instructor's Page

1.2.2: Suggestions for Success in This Course

  • You can learn the course material in many different ways
  • However, by following the guidelines below you will get a clearer understanding of Internet programming
  • Also, you will get the best possible grade

Understanding the Material

  • Note the Learner Outcomes for each lesson and concentrate on learning them
  • Skim the lecture notes to get an overview of what you need to learn
  • Read the assigned pages of the textbook, focusing on the Learner Outcomes
    • Note the relationships between the lecture notes and the textbook
    • Lecture notes may highlight the text or may present information in a new way
  • Attend the lecture, ask questions and participate in the discussions
  • After the lecture, review the summary and answer the Check Yourself questions
    • This helps to reinforce your learning of the material
  • If you cannot answer the questions, then follow up and find the answers
  • If you cannot find the answer in the text, then ask a classmate or the instructor
  • Suggestion: make a note and ask a question at the start of the next class

Completing and Submitting Assignments

  • Read carefully before attempting the assignments
  • If the assignment directs you to web sites, then read that information as well
  • Think about the design of your program and work out the algorithms in advance
  • When you have analyzed the problem and developed a plan, then start writing the code
  • Make sure you submit your assignments before the due date
  • Late assignments are not accepted
  • Submitting anything earns at least partial credit

Setting Up Your Development Environment

  • All the software we use runs on almost any computer
  • Instructions on How To Install Java are available from the HowTo's page
  • Also, there are instructions for installing TextPad
  • If you do not have a computer at home (or work) to use for assignments, then you can use the CTC
  • To use the CTC, you will need a login and password
  • You are automatically assigned a login and password after you enroll in this course

Preparing for Exams

  • Exams provide an opportunity to demonstrate what you have learned
  • If you read the textbook, attend lectures, complete the exercises and assignments, and study for each exam, you should do well
  • You will need to spend time studying for exams
  • I will discuss study techniques before the exams

1.2.3: Summary

  • www.edparrish.com has a link to the course web site and has contact information for the instructor
  • Course information describes the course and it grading policies
  • Home Page lists the online content of the course
  • Schedule lists the planned lesson-topics and assignments
  • Blackboard is where you turn in assignments, take tests and can look at your grades and get feedback on assignments
  • Class Expectations describes what you can expect from me and what I expect from you during the course
  • To succeed in this course, you should attend lectures, ask questions and participate in the discussions
  • Also, you need to complete your assignments and turn them in before the due date
  • To do your homeowk, I suggest that you set up your development environment at home or work

Check Yourself

  1. What is your personal Blackboard login (NOT your password)?
  2. Where is the instructor's contact information located?
  3. What is the closest building next to the instructor's office?
  4. What is the URL where homework assignments are listed?
  5. Where do you turn in homework assignments?
  6. What course should you take before this one if you have not preogrammed before?
  7. How much time should you expect to spend on this course outside of the classroom?
  8. What compiler must you use for this course?
  9. What is scholastic dishonesty?
  10. What is an example of acceptable help and an example of unacceptable help on assignments?
  11. Why is eating and drinking prohibited in the classroom?
  12. What are three things you should do to succeed in this course?

Exercise 1.2: Choices

On a piece of paper and spend one minute writing the following lists:

  1. What are things you choose to do?
  2. What are things you have to do?

1.3: Introduction to Java Programming

Learner Outcomes

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

  • Explain the basic elements of a Java program
  • 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.3.1: Creating a Java Program

  • As with other high-level programming languages, you use an editor to write source code
  • Converting source code into running machine code takes several steps
    1. Writing and saving the source code
    2. Compiling the source code to bytecode
    3. Executing (running) the program

Compiling and Running Java Programs

  1. Writing and saving the source code
    • You can write a program's source code using any text editor
    • You use the text editor to save the source code in a file
    • All source code files must have a .java suffix
  2. Compiling the source code
    • You use a compiler to translate the source code to bytecodes
    • Files with translated bytecodes have .class suffix
  3. Executing (running) the program
    • You use the java program to run the bytecodes
    • When the java program starts, a class loader moves bytecodes from a file into memory
    • Then a bytecode verifier tests the bytecodes to establish safety and security
    • Finally, the Java Virtual Machine (JVM) translates bytecodes into machine language

1.3.2: Example Java Source Code

  • As an example, here is the source code for a simple Java program
1
2
3
4
5
public class HelloWorld {
     public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}
  • The source code for all Java programs have one or more classes
  • You can see that the first line declares a class named HelloWorld
  • public class HelloWorld {
  • The word public means all parts of the application can use this class
  • Notice the curly braces used in the code
  • The left brace { begins the body of a class
  • Likewise, the right brace } ends body of every class
  • Any code between the curly braces is called the class definition
  • The second line of code declares a main method
  • public static void main(String[] args) {
  • Every Java program must have at least one main() method
  • The main() method also has curly braces around the code it executes
  • You need to code your main() methods exactly like this one
  • The third line of code tells the computer to print "Hello, World" to the screen
  • System.out.println("Hello, world!");

1.3.3: Saving Source Code

  • We need to save the example source code in a file
  • You can use almost any text editor to enter and save Java source code
  • We use TextPad in classrooms and labs and you can download it for use at home
  • Let's save the file by:
    1. Start TextPad
    2. Copy the example code from this web page to TextPad
    3. Save the code to the Desktop using the name: HelloWorld.java
  • All file names must be the same as the name of the class
    • With the same capitalization
  • Make sure you save source code using the extension: .java

1.3.4: Compiling and Running Programs Using the Command Line

  • Now that we have saved our file, we need to compile the code and run the program
  • All compilers, including Java, can be run from the command line
  • Sometimes you get into a situation where you need to work with the command line
  • As a programmer, you should always know how to compile and run programs using the command line
  • In fact, any program you submit must run from the command line
  • If you are not familiar with the Windows command line, see: How To Use a Windows Console

Compiling Source Code

  • To compile and run a program from the command line, you first open a console window
    1. From the Start menu, Select Run...
    2. In the text box, type:
      • cmd for Windows NT/2000/XP/Vista
      • command for Windows 95/98/ME
  • Then you change the directory (using cd) to where you saved the source code
  • C:\>cd Desktop
  • Now at the command line type: javac HelloWorld.java
  • If you do not see any errors, the compiler creates a class file
  • You can verify the HelloWorld.class file exists using the dir command
C:\Documents and Settings\EdParris\Desktop>dir HelloWorld.class
 Volume in drive C has no label.
 Volume Serial Number is XXXX-XXXX

 Directory of C:\Documents and Settings\EdParris\Desktop

08/26/2008  05:14 PM               427 HelloWorld.class
               1 File(s)            427 bytes
               0 Dir(s)  68,939,227,136 bytes free

Running a Program

  • At the command line type: java HelloWorld
    • The JVM loads the .class file for HelloWorld
    • Note that you do NOT type the .class suffix
  • The JVM calls the main() method of the class by default

About the Java Compiler and Other Tools

  • Sun Microsystems, Inc. supplies the Java compiler and some other development tools for free
  • Collectively, these tools are known as the Java Development Kit, or JDK
  • For instructions on installing the JDK, see: How To Install Java

1.3.5: Compiling and Running a Program Using TextPad

  • Many text editors have provision for compiling within the editor
  • We use TextPad in our classrooms and labs as a text editor
    • TextPad was kind enough to provide us a free site license
  • Note that you can install TextPad at home
  • To make your in-class exercises easier, we have set up TextPad to compile and run Java programs
  • You can use the HelloWorld program to test compiling and running with TextPad
1
2
3
4
5
public class HelloWorld {
     public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

Compiling with TextPad

  1. Load your source code into the active TextPad window
  2. Select the Tools menu
  3. Select Compile Java
  4. If there are any syntax errors, you will see a page showing them
  5. Otherwise, you will return to your source code page

Running Programs with TextPad

  1. Load your source code into the active TextPad window
  2. Select the Tools menu
  3. Select Run Java Application
  4. Your program will run in a console window

1.3.6: Using IDEs to Develop Programs

  • TextPad and other text editors are fine tools for writing, compiling and running Java programs
  • However, more powerful tools exist for developing Java programs
  • These programs are called Integrated Development Environments (IDEs)
  • IDEs provide many more features than a text editor such as:
    • Visual tools for developing user interfaces
    • Tools for debugging, refactoring, version control, etc
  • Note that I do not recommend using an IDE while learning Java
    • An IDE will generate code for you, which does not help you learn
    • IDEs are complex tools that themselves are difficult to learn
    • Learning the tool can distract you from your goal of learning Java
  • If you know Java well and use it frequently, then learning to use an IDE can improve your productivity
  • If you feel ready to try one, then some of the more popular free Java IDEs are:
  • Does anyone have experience using an IDE?

A Lightweight Development Environment

  • There is an IDE that is simple to learn and useful while learning Java: DrJava
  • In addition to a useable editor, DrJava has a text area for interactively executing Java code
  • Also, it has a visual debugger and is easy to download and runs with installing

1.3.7: Summary

  • Java started development in 1991 and was officially released in 1996
  • The original focus was Applets, but Java is now a widely-used general-purpose programming language
  • Developing Java programs requires three step that you repeat over and over:
    1. Create source code with an editor (or IDE)
    2. Compile to bytecode using a compiler (javac FileName.java)
    3. Execute using a JVM (java FileName)
  • You can use almost any text editor for writing and editing source code
  • Many text editors provide features to make it easier to compile and run programs
  • Once you have mastered the basics of Java, you can use an IDE to improve your productivity
    • While you are learning, though, it is usually better to use a text editor
  • However you decide to develop programs, any program you submit for this course must compile and run from the command line

Check Yourself

  1. What is the purpose of the Java compiler?
  2. What is the file extension of compiled Java files?
  3. What makes Java portable across multiple platforms?
  4. What type of program can you use to write Java source code?
  5. Can you write a Java program without using a class?
  6. What code do you use to print text to the console or command line?
  7. What code do you write for a main() method?
  8. How does a Java program end?
  9. What do you type at the command line to compile the program: Foo.java
  10. What do you type at the command line to run the program named Foo?

Exercise 1.3

Take one minute to prepare an answer the following questions:

Q1: What do you type to compile a program named Foo.java?

Q2: What do you type to run the source code you compile with question 1?

Wrap Up

Due Next:
A1-Information Please! (2/18/09)

Home | Blackboard | Announcements | Schedule | Room Policies | Course Info
Help | FAQ's | HowTo's | Links
Last Updated: March 29 2009 @18:05:07