1. Course Information

What We Will Cover


Exercise 1.1

Instructions

Use the next 10 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 can 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?

Note: You do not have to turn in the answers to this exercise. However, you are expected to submit answers to subsequent exercises.

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

  • Programming requires you to know many facts and procedures
  • Also, you must learn how to solve problems
  • To help you master the materials, I provide you with exercises and assignments in addition to lectures
  • To succeed in this course, you need to make use of this material

Attend Class

  • You need to attend class and pay attention
  • When asked questions, you need to respond
  • When you cannot attend class for some reason, you need to catch up before the next class meeting

Complete In-Class Exercises During Class

  • In-class exercises are problems you are given class-time to complete
  • You should complete these problems during the allotted time in class
    • However, you have about one-week to complete them after class
    • See WebCT for the exact date and time
  • You can discuss them with partners or the instructor
    • Much easier to solve problems in the classroom than at home
  • You should submit the exercises to WebCT before you leave the classroom
    • Ensures that something is turned in on time
    • You can always unsubmit them if you want to make changes during the week
  • Graded on a best-effort basis
    • You get full credit for just attempting to complete the exercise

Complete On-line Exercises On-time

  • Online exercises (CodeLab) are due about a week after we discuss the topics
  • CodeLab gives you a series of graduated exercises with "instant" feedback
  • I suggest that you complete the on-line exercises before the homework assignment

Complete Homework Assignments On-time

  • Homework assignments have you apply and explore lecture concepts
  • Homework assignments can include reading, quizzes and writing code
  • If you get confused about or stuck on an assignment:
  • 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 -- late homework is not accepted
  • Instead, most homework has extra credit possibilities

Study 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 hour reading the lecture notes and textbook before class
    • 4 hours in the classroom
    • 8 hours for on-line exercises and homework assignments (excluding extra credit)
  • I collect time estimates from students for homework 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. Attend class and actively participate
    2. Submit the in-class exercises before you leave
    3. Complete on-line exercises on-time
    4. Do your homework assignments and turn them in on time
    5. 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

With no more than a single partner, take 5 minutes to complete the following.

  1. Start a text file named exercise1.txt
    You will be adding to this file during the lesson, so save it often.
  2. Prepare the exercise header as described in the HowTo on submitting exercises
  3. Label this exercise: Exercise 1.2
  4. Submit all exercises for this lesson in one file unless instructed otherwise
  5. Complete the following and record the answers to any questions in exercise1.txt.

Specifications

  1. List the elements of success for this course
  2. What is the average time you can expect to spend outside the classroom on this course?
  3. Where do you plan to work on your programming assignments?

1.3: Programming and Problem Solving

Objectives

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

  • Describe the programming process
  • Solve simple computational problems

1.3.1: What Programming is About

  • Programming is about solving problems using a computer program
  • A few terms and definitions you need to learn:
  • Algorithm: A sequence of precise instructions which leads to a solution

    Computer programming: An algorithm expressed in a language the computer can understand

  • Programming is a creative process
  • No complete set of rules for creating a program
  • However, programs are best developed in two phases

Program Development Phases

  • Programs are developed in two general phases:
    1. Problem-solving phase
      • Result is an algorithm that solves the problem
    2. Implementation phase
      • Result is the algorithm translated into a programming language
  • Each of these phases has several general steps to them
  1. Problem definition
    • First step is to analyze and understand the problem
    • We will look at this step in more detail in the next section
  2. Algorithm design
    • Next step is to define an algorithm
    • Set of steps that a computer can use to perform a task
    • We will look at this step in more detail as well
  3. Algorithm (desktop) testing
    • After developing an algorithm, you need to verify it is correct
    • Develop a sample problem
    • Walk through each step mentally or manually
    • If there is an error, change the algorithm
  4. Translate to Java
    • After you are sure the algorithm works, then write the program code
    • Code is written using a programming language
    • The language we use for this course is called Java
    • As you write the program code, you translate human-readable source code into a language understood by the computer
    • You check your translation using a program called a compiler
    • Discuss compiling later today
  5. Computer testing
    • After code compiles, you test it to make sure it:
      1. Does what it is supposed to do
      2. Does not do what it is not supposed to do
      3. Does what it used to do
  6. Working program
    • When it works well, you document and deliver the program

1.3.2: Analyzing Problems

  • The first step in solving a problem is to understand it
  • You need to examine the data and conditions of the problem
  • The topics in this section suggest some ways to help you analyze the problem and gain an understanding

Paraphrase the Problem

  • A good way to start analyzing a problem is to write it down in your own words
  • Often times writing down the problem will lead to other ways of analyzing it

Ask Questions

  • If you are given a problem, you naturally ask questions like: What? When? Why? Where? How?
  • Here are some questions that are often useful to ask when faced with a programming problem:
    1. What output is expected?
    2. What format is required for the output?
    3. What calculation must be performed?
    4. What data is needed for the calculations?
    5. What data do I have to work with?
    6. Where will I get the data I do not have?
    7. What do the data items look like?
    8. How will I know when I have processed all the data?
    9. How many times is the process going to repeat?
    10. What special error conditions might happen?
  • Try reviewing the list and answering the questions that seem pertinent

Draw a Diagram

  • You have heard the old saying, "a picture is worth a thousand words"
  • Sometimes a visualization of the problem can help you understand it
  • For example, if the problem is about a rectangle then you draw a rectangle

Look for Similarities

  • Often a problem reminds you of other problems you have seen before
  • Recognizing similarities can give you insight into understanding the current problem
  • For example, if you have seen a problem for a list of numbers then you may see similarities when given a problem dealing with a list of names

Separate the Parts

  • If you have a large or complex problem, then you can often break it into smaller pieces that are easier to understand
  • For example, if you have to travel from one location to another, you can plan the journey in steps

More Information

1.3.3: Developing Algorithms

  • A computer algorithm is a set of steps that a computer can use to perform a task
  • Coming up with a step-by-step method for solving a problem is not always easy
  • It is often a trial-and-error process requiring many attempts and refinements
  • This is made more complicated because the computer can only do certain things
  • Our algorithm must only contain steps that a computer can perform

Operations a Computer Can Perform

  • Get data from a keyboard or other input device
  • Display data on a screen or other output device
  • Move data from one location to another
  • Store data into and retrieve data from memory
  • Perform arithmetic, such as addition and subtraction, very quickly
  • Compare two data values for equality or inequality
  • Process instructions in a sequence (in order)
  • Process different instructions depending on conditions that are met
  • Repeat a set of instructions while certain conditions are met

Devising a Plan

  • There are several approaches to solving problems algorithmically
  • We will look at several during this course
  • One approach you may find useful is: Using a Formula

1.3.4: Using a Formula

  • Formulas are a powerful tool we can borrow from mathematics
  • Oftentimes, you can easily solve a computing problem using a formula
  • For an example, we will use the problem:
  • Find out how big around a rectangle is.

Starting the Analysis

  • We first start by rephrasing the problem in our own words
  • We state our problem as precisely as we can:
  • Calculate the perimeter of a rectangle.

Understanding the Problem

  • We continue our analysis by trying to understand the problem in more depth
  • We select some of the questions that seem pertinent to the problem
    1. What output is expected?
    2. What calculation must be performed?
    3. What data is needed for the calculations?
    4. Where will I get the data I do not have?
  • What are the answers to these questions for our example problem?
  • Thinking about these questions improve our understanding of the problem
  • We can see that we can solve the problem by using a formula

Defining the Algorithm

  • Once we understand our problem sufficiently, we develop an algorithm
  • For our example problem, we might define our algorithm as:
    1. Get the length of the rectangle
    2. Get the width of the rectangle
    3. Calculate the perimeter using the equation:
      perimeter = 2 * (length + width)
    4. Display the result of the calculation
  • We must test the algorithm to be certain it is correct

1.3.5: Processing a List

  • Let us look at another problem-solving example
  • One problem that computers are good for solving is to process a list of data
  • For an example, we will use the problem:
  • Count the number of times that a name occurs on a list.

Analyzing the Problem

  • What is a good problem statement?
  • Next, we ask ourselves some questions to improve our understanding
    1. What output is expected
    2. What data do I have to work with?
    3. What do the data items look like?
    4. How will I know when I have processed all the data?
    5. How many times is the process going to repeat?
  • Ask we ask these questions, more questions occur to us like:
    1. How many names are on the list?
    2. What name are we looking for?
  • After answering these, we should be ready to develop a solution

Defining the Algorithm

  • One possible algorithm is:
    1. Get the list of names
    2. Get the name to check
    3. Set a counter to zero
    4. For each name on the list
      1. Compare the name being checked to the name on the list
      2. If the names are the same then add one to the counter
    5. When done, display the value of the counter
  • Once we have an algorithm, we need to test it for correctness

1.3.6: Summary

  • Programming is about solving problems using a computer program
  • Programs are best developed in two phases
    1. Problem-solving phase
    2. Implementation phase
  • In this section we looked at the first phase: problem solving
    • We will look at the second phase in the next section
  • First step to take when solving a problem is to analyze the problem
  • Second step is to develop an algorithm that solves the problem
  • Only after verifying that the algorithm works do you translate it to Java

Check Yourself

  1. What is the first step to take when creating a program?
  2. What are the two main phases of the program design process?
  3. What is the importance of the problem-solving phase?
  4. What is an algorithm?

Exercise 1.3

  1. Prepare the exercise header as described in the HowTo on submitting exercises
  2. Label this exercise: Exercise 1.3
  3. Submit all exercises for today's lesson in one file unless instructed otherwise
  4. Complete the following and record the answers to any questions in exercise1.txt.

Specifications

  1. Analyze and define an algorithm for the following problem:
  2. What is the paycheck amount (before taxes) for an employee who is paid hourly? You may assume that the employee does not get paid extra for overtime.

Check your answer

1.4: Introduction to Java

Objectives

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

  • Describe how source code becomes a running program
  • Start a command-line session
  • Navigate directories from the command line
  • Compile and run programs when given the source code
  • Display program output to the command line

1.4.1: About Java

  • Java is a programming language
  • A programming language allows a programmer to describe a computation
  • Java is a high-level language
  • High-level programming languages are easier to use than low-level languages

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 SDK 1.5
  • Today a general-purpose programming language used extensively

More information

1.4.2: Creating a Java Program

  • Converting source code into a program takes several steps
    1. Writing and saving the source code
    2. Compiling the source code to bytecode
    3. Executing (running) the program
  • This process is repeated many times during development

Let's discuss each step in more detail:

  1. Writing and saving the source code
    • Write a program's source code using any text editor
    • 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

  • Let us go through these steps with an example program.

1.4.3: Example Java Source Code

  • As an example, here is the source code for a simple Java program
  • What do you notice about this code?
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 program 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.4.4: Saving Source Code

  • We need to save the example source code in a file
  • You can use almost any text editor to save Java source code
    • Windows: jEdit, Notepad, WordPad, TextPad, ...
    • Linux/UNIX: jEdit, VIM, gedit, pico, ...
    • Mac: jEdit, SimpleCap, ...
  • We use TextPad in the classroom and you can download it for use at home
  • Let's save the file by:
    1. Start TextPad
    2. Start > CIS and CS Classes > TextPad

    3. Copy the example code from this web page to TextPad
    4. 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 the file using the four-letter extension: java

About Using IDEs

  • There are 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
    • 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
  • I do recommend using IDEs once you have a good understanding of Java
    • Easier to develop user interfaces
    • Improves programmer productivity

1.4.5: Using the Windows 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
  • How many people know how to use the command line on Windows?

Opening a Console

  • Windows provides a command-line interface using a console window
  • You can open a console by following these steps:
  1. From the Start menu, Select Run...
  2. In the text box, type:
    • cmd for Windows NT/2000/XP
    • command for Windows 95/98/ME

Using the Command Line

  • To use the command line, you type commands at a command prompt
  • C:\>dir
    • C:\> is the command prompt
    • dir is the command
  • After typing a command, you press the Enter key
  • For example, you can list the files in the current directory using dir
  • C:\>dir
  • When a console session starts, you may not be in the directory you want
  • You can see the current directory location by looking at the command prompt
  • C:\Documents and Settings\edparris>
  • To change to a lower subdirectory level type: cd directoryName
  • C:\>cd Desktop
  • To change up one directory level type: cd ..
  • C:\>cd ..
  • To change to the root of the file system: cd \
  • C:\>cd \
  • To change to any location in the file system: cd path
  • C:\>cd \Documents and Settings\YourLogin\Desktop

Ending a Console Session

  • At the command line, type exit and press the Enter key
  • C:\>exit

More Information

1.4.6: Compiling and Running Programs

  • Let us use a Windows command line to compile and run a program
  • First, open a console window
  • Then change the directory (cd) to where you saved the source code
  • C:\>cd Desktop

Compiling Source Code

  • 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 0CFF-CD25

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

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

Running a Program

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

Other Ways to Compile and Run

  • There are other ways to compile and run programs
    • I will show you an easier way next time
  • However, you must know how to compile source code and run a program from the command line
  • Any program you submit must run from the command line

1.4.7: Summary

  • Java programs require three steps:
    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 most 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 should use an IDE
  • While you are learning, it is better to use a text editor

Check Yourself

  1. What is the advantage of a high-level language?
  2. How do you print text in Java?
  3. How does a program end?
  4. How does Java translate source code into machine code?
  5. What makes Java portable across multiple platforms?
  6. What do you type at the command line to compile the program: Foo.java
  7. What do you type at the command line to run the program named Foo?

Exercise 1.4

  1. Prepare the exercise header as described in the HowTo on submitting exercises
  2. Label this exercise: Exercise 1.4
  3. Complete the following steps and answer the questions.
  4. Note: If you run out of time, you can complete this exercise in the CTC or at home.

Specifications

  1. Start TextPad (or any other editor) and enter this code:
  2. public class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Hello, world!");
        }
    }
    
  3. Save this code to a convenient directory location like the Desktop
  4. Compile the source code using the command line.
  5. Run the program using the command line.
  6. Record your answers to the following question in your exercise1.txt file:
  7. 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?

Note: do not forget to turn in your exercises following the instructions for How To Submit In-Class Exercises.

Wrap Up

Due Next:
A1: Information Please! (9/7/05)
Exercise 1 and CodeLab 1 (9/7/05)

Home | WebCT | Announcements | Schedule | Expectations | Course info
Help | FAQ's | HowTo's | Links

Last Updated: October 09 2005 @20:58:25