introduction to programming session 1

32
Summer 2008 Q1 Summer 2008 Q1 Version 1.2 Version 1.2 1 Introduction to Programming Session 1 Point Park University CMPS 322 Summer 2008 Q1

Upload: mei

Post on 14-Jan-2016

43 views

Category:

Documents


0 download

DESCRIPTION

Point Park University CMPS 322 Summer 2008 Q1. Introduction to Programming Session 1. Course Objectives. We will: Examine syntax requirements of programming using Java language. Introduce you to the activity of programming. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to Programming Session 1

Summer 2008 Q1Summer 2008 Q1 Version 1.2Version 1.2 11

Introduction to ProgrammingSession 1

Point Park University

CMPS 322Summer 2008 Q1

Page 2: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 2

Course Objectives

We will:– Examine syntax requirements of programming using Java language.

– Introduce you to the activity of programming.

Each session will extend your software development skills by tackling more complex problems.

Skills you need:– An analytical mind.

– Persistence.

– Willingness to experiment.

Page 3: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 3

Today’s Agenda

Course administrative matters. Introduction to programming. Introduction to Netbeans. First topics of Java language.

– Basic application structure.

– Data types and declaration of variables.

– Arithmetic expressions.

Page 4: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 4

Course Software

Netbeans 6.0– Open source Java development environment from Sun Microsystems.

– Version 6.01 installed in classrooms.

– Version 6.1 is what you should install at home or on your laptop. Link posted on Blackboard. Software is free.

Microsoft Word, Visio, etc.– If supporting documents are needed.

Page 5: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 5

Course Materials

Blackboard (http://bb.ppc.edu).– Please check to see if you are enrolled.

Lecture / discussion / assignments. Please review course overview and policies.

– Please note late policy on assignments.

Please review course schedule.

Page 6: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 6

Blackboard Discussion Forums

Accelerated course schedule mandates equivalent instruction outside of class.

This will be conducted via Blackboard discussions. Topics will be posted, and active, substantive participation will be

expected.– Participation level and quality will contribute to final grade (10% of total

grade).

Page 7: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 7

Assignments

New approach being taken this term with assignments. Three tracks will be available to students.

Track 1: Easier assignments and a final exam.Track 2: More difficult assignments and no final exam.Track 3: One complex assignment and no final exam.

Self-chosen teams of up to two students may work on project.In final class, teams will be asked to display their working programs to the class.

Five assignments spread out over the seven weeks of the course. Assignments will be programming exercises.

Page 8: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 8

Programming

What is a program?– The program is the driving force behind any job that a computer does.

– A program is a sequence (list) of detailed instructions that tells a computer what to do.

The order of the sequence is critical to the successful operation of the program.

– Typically processes input (data) to generate output (information).

What are instructions?– Depends on your point of view.

Page 9: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 9

What Are Instructions?

Instructions describe the problem to be solved in terms of the things the software must do.

Depending on who or what you are in this process, the instructions will vary.

For example, to the Software Sponsor:– The account balance should be debited by the amount of the check, after

determining that the account balance is sufficient to cover the amount of the check.

Page 10: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 10

What Are Instructions?

To the Programmer, instructions are a translation of the Business Sponsor instructions into a set of statements in a programming language.– The Software Sponsor’s Instructions are translated to statements that

describe the specific actions that a program should take.

– These can first be written in a manner that is not specific to any one programming language (e.g., pseudo-code or flowcharts).

– They are then translated into more structured code for a particular language. For example:

int j;j = j + 2;if (j > 120)

int j;j = j + 2;if (j > 120)

Page 11: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 11

What Are Instructions?

To the computer, the structured lines of language specific code are translated into another language that a computer can actually understand.

These instructions are simple binary codes. When combined with binary data, simple tasks can be performed. For example:

– Move a value into a memory location.

– Add two numbers together.

– Jump to a memory location if one value is greater than another.

Page 12: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 12

Back To Programming

The job of the programmer is to translate business requirements into statements that can be processed into instructions that:– Control the operation of a computer.

– Perform the required tasks as the user requires.

– Do so accurately and efficiently.

Our job in this course is to help you be able to do this at an introductory level.

You will write programs. We won’t expect you to become programmers – unless that is your

goal. Being exposed to the process of programming in a real way will help

you understand how computers work and how software is developed.– And thus, becomes an invaluable part of your IT education and

background.

Page 13: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 13

How Do You Program?

Many of you may feel that programming is a daunting task.– At times in this course, it may very well be.

I will try to lay out a process for you to follow that will help you to write programs.

You want to avoid immediately writing code, because this will get you tied up in the details of the language, and usually into trouble.

The process:– Think about the problem.

– Think about it again. Old carpenter’s adage: Measure twice, cut once.

– Scribble out a solution using pseudo code or flow charts.

– Begin to code.

Hopefully, the in-class lab exercises we do will provide you with confidence to proceed.

Page 14: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 14

How Do You Program?

Computer programs are implementations of algorithms. What is an algorithm?

– A step-by-step procedure for solving a problem in a finite period of time.

You must develop an algorithm to solve the problem at hand.– Our problems will be relatively simple compared to those in business

programming.

– Nonetheless, your algorithm must express the problem in a set of procedural steps.

You then translate these steps into the target programming language. You compose the code in the programming tool of choice. You test. And you cycle this process until satisfied that you have met the

requirements of the problem.

Page 15: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 15

How Do You Program?

Example algorithm:– Calculating employee weekly wages.

Look up employee’s pay rate. Determine number of hours worked in week. If hours less than or equal to 40, multiply hours worked by pay rate to yield

regular wages. If hours worked is greater than 40, calculate regular pay as 40 times pay rate;

calculate overtime pay as hours above 40 multiplied by 1.5 times the pay rate; add above two values together to yield total wages.

Page 16: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 16

How Do You Program?

Your job will be to develop algorithms that will solve the problem you have been assigned.– This will be challenging and creative.

This is why I emphasize thinking first. Decide what you want to do before beginning to write code. Remember what we discussed in CMPS 318 regarding problems with

software development.

Page 17: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 17

Why Program in Java?

Java is platform independent.– A Java program can be run without changes on different kinds of

computers.

Java geared to a distributed world.– Java programs can easily be run on computer networks.

Java is a relatively secure language.– Java contains features that protect against viruses and other untrusted

code.

Java fosters robust programming.– Strictly enforced data types.

– Protection against overflows and range errors.

– Variables must be declared and initialized before use.

– Memory management automatic.

Java is object oriented.

Page 18: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 18

What Are Java Programs?

A Java program is made up of class definitions. A class definition contains a header and a body. The body contains methods and properties/attributes. A method is a named section of code that can be called by its name

(i.e., a function). A property is a variable defined within the class. Java programs come in a variety of application formats and contexts:

– Java applications (what we will focus on).

– Java applets (run on web pages).

– Beans (components).

– Enterprise Java (back end server applications).

Page 19: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 19

What Are The States or Stages of Java Programs?

A Java program starts with source code. The Java program is compiled into bytecode. The Java program is executed within the Java runtime environment.

– Bytecode is interpreted by JRE.

– This is different than a C++ EXE.

– Enables the Java platform independence.

Page 20: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 20

Java Applications and Applets

Java Applications– A Stand-alone program.

– Runs independently at command line (under JRE).

– Has a main() method that is starting point of application.

– No HTML file.

Java Applets– Embedded program.

– Runs in a Web browser.

– No main() method.

– Requires an HTML file.

– Run using JDK’s appletviewer or via web browser.

Page 21: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 21

Java Class Libraries

Much of Java’s power comes from its extensive class libraries. We will utilize some of these classes as is appropriate for this course. Java is a combination of the language and the class libraries. To make use of any of the libraries, you must use an import statement.

– The exception here is java.lang, which is automatically included in all programs.

– Difference between these two statements is that first imports all class definitions of java.util, while second imports just JApplet.

import java.util.*;import javax.swing.JApplet;

import java.util.*;import javax.swing.JApplet;

Page 22: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 22

Java Language Fundamentals

Java is case sensitive. Statements normally end with a semi-colon. Statements can be grouped into blocks with braces {}. Statements are free format -- spaces can be freely used and code can

be broken up into multiple lines. Indentation is commonly used to improve code readability.

– There is no requirement or standard for indentation, but there are several common patterns in use.

– My code will follow a fairly common indentation practice.

– Your code will be expected to demonstrate similar practice.

Page 23: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 23

Sample Java Application

The generic Hello World program.

// The HelloWorld application program.

public class HelloWorld

{

/**

* @param args the command line arguments

*/

public static void main(String argv[])

{

/* .Printing Hello World */

;System.out.println(“Hello world!”)

{

{

// The HelloWorld application program.

public class HelloWorld

{

/**

* @param args the command line arguments

*/

public static void main(String argv[])

{

/* Printing Hello World. */

System.out.println(“Hello world!”);

{

{

Page 24: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 24

Sample Java Application

Comments and documentation have several formats.

// The HelloWorld application program.

public class HelloWorld

{

/**

* @param args the command line arguments

*/

public static void main(String argv[])

{

/* .Printing Hello World */

;System.out.println(“Hello world!”)

{

{

// The HelloWorld application program.

public class HelloWorld

{

/**

* @param args the command line arguments

*/

public static void main(String argv[])

{

/* Printing Hello World. */

System.out.println(“Hello world!”);

{

{

Comment.Comment.

Comment.Comment.

JavaDocJavaDoc

Page 25: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 25

Java Data Types

Your programs will need to store and use data. In programming, we must map the data types available in the language

to the problem domain.– With OOP, we can create our own data types that map more directly to the

problem domain.

– However, we must eventually work with the types available in the language.

There are two general categories of data in a computer program:– Numeric.

Whole numbers (integers). Decimal numbers (floating point).

– Non-numeric. Strings of characters. Single characters. Boolean (true/false).

– These are the intrinsic data types of the language.

Your programs will also eventually use classes.

Page 26: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 26

Java Data Types

Text reference begins at Page 34. Four integer types available:

– byte

– short

– int

– long

See Table 2.1 for value ranges for these types and for the memory requirements.

Page 27: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 27

Declaring Variables

Your programs will need to be able to store data in them for computation and processing.

This will necessitate that you declare variables. Variables are storage locations in memory that you can refer to by a

name that has meaning to you for the context of the application. Variables are declared by selecting data type and variable name:

int myintvalue, j, k3;double salary = 10000.0;

int myintvalue, j, k3;double salary = 10000.0;

Note initialization syntax.

Note initialization syntax.

Page 28: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 28

Declaring Variables

Variables are not initialized automatically. Use of a variable before initialization is illegal in Java. May be initialized at time of declaration, or via assignment statement.

double salary = 10000.0;

salary = 22000.0;

double salary = 10000.0;

salary = 22000.0;

Page 29: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 29

Arithmetic Expressions

Most programs will need to support numeric calculations. This is accomplished via statements that combine variables with the

arithmetic operators.– +, -, *, /, %.

These operators are the binary operators.– They require left and right operands.

There are also a number of unary operators, notably:– ++, --, +=, -=, *=, /=.

The complete set of Java operators, and their precedence, is shown on Page 80.

Let’s whiteboard some sample expressions. And then work on a little Java program with math.

Page 30: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 30

Type Casting

Our example demonstrated that sometimes Java allows mixed arithmetic expressions, sometimes it doesn’t.

When it doesn’t, we can usually convince it to do so by type casting. Type casting involves stating what your target data type is. Syntax is to place target data type within parentheses, in front of data

to be converted.

int j;j = (int) 3.24;

int j;j = (int) 3.24;

Page 31: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 31

In Class Labs

Get practice with Netbeans. Demonstration of Programming Exercise 2.1. Practice time with Programming Exercise 2.2.

– Pair programming anyone?

Page 32: Introduction to Programming Session 1

Summer 2008 Q1 Version 1.2 32

Assignments

Get Netbeans installed. Reading: Chapters 1 and 2 from text. Assignment 1.

– Due next week.

Participate in discussion forums.