jens dalsgaard nielsen jan dimon bendtsen dept. of electronic systems basic programming ins-basis...

25
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

Upload: jonas-harrison

Post on 04-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

Jens Dalsgaard Nielsen

Jan Dimon BendtsenDept. of Electronic Systems

Basic Programming

INS-basis GF, PDP and HST

Page 2: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

Course Goals

• To understand the activity of programming

• To become familiar with computing environments, compilers etc.

• To be able to program, compile and run Java programs

• To be able to recognize (and fix) syntax and logic errors

• To be able to use computer programs as an engineering tool

Page 3: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

Prerequisites

• Computer savvy (file management, text editing)

• Problem solving skills

• Time management

• High school math (algebra, trigonometry)

• No prior programming background required

Page 4: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

Course Organization• Introduction to the course, getting started

• Tools, getting operational

• Classes and objects

• Program control

• Basic GUI programming

• Data structures

• File I/O

• Slightly less basic GUI programming

1)+10) Mini-project

Page 5: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

Examination

• “Mini-project” - at the end of the course, you write a program that: Satisfies a “requirement specification” posed by

the lecturers Demonstrates that you know how to program in

Java Demonstrates that you are able to use an

Integrated Development Environment (Eclipse)

• Individual examination (but it is OK to do the actual work in small groups)

Page 6: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

Chapter 1

Introduction to Java Programming

Page 7: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

What Is Programming?

• Computers are programmed to perform tasks

• Different tasks = different programs

• Program Sequence of basic operations executed in

succession Contains instruction sequences for all tasks it

can execute

• Sophisticated programs require teams of highly skilled programmers and other professionals

Page 8: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

Schematic Diagram of a Computer

Figure 5: Schematic Diagram of a Computer

Page 9: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

The Java Programming Language• Simple

• Safe

• Platform-independent ("write once, run anywhere")

• Rich library (packages)

• Designed for the internet

Page 10: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

An Integrated Development Environment

Figure 9:An Integrated Development Environment

Page 11: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

File HelloTester.java

1: public class HelloTester 2: { 3: public static void main(String[] args) 4: { 5: // Display a greeting in the console window 6: 7: System.out.println("Hello, World!"); 8: } 9: }

OutputHello, World!

Page 12: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

HelloTester in an IDE

Figure 12:Running the HelloTester Program in an Integrated Development Environment

Page 13: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

A Simple Program

Figure 13:Calling a Method

System ClassSystem.out Objectprintln Method

• public class ClassName • public static void main(String[] args) • // comment• Method call

Page 14: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

Syntax 1.1: Method Call

 object.methodName(parameters)

Example: System.out.println("Hello, Dave!");

Purpose:To invoke a method of an object and supply any additional parameters

Page 15: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

Errors

• Syntax errors

• Detected by the compiler

• Logic errors

• Detected (hopefully) through testing

System.ouch.print(". . .");System.out.print("Hello);

System.out.print("Hell");

Page 16: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

The Compilation Process

Figure 14:From Source Code to Running Program

Page 17: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

Chapter 2

Using Objects

Page 18: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

Types and Variables

• Every value has a type

• Variable declaration examples:

• Variables Store values

Can be used in place of the objects they store

String greeting = "Hello, World!";PrintStream printer = System.out;int luckyNumber = 13;

Page 19: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

Types and Variables

• Every value has a type

• Variable declaration examples:

• Variables Store values

Can be used in place of the objects they store

String greeting = "Hello, World!";PrintStream printer = System.out;int luckyNumber = 13;

Page 20: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

Syntax 2.1: Variable Definition

 typeName variableName = value; or typeName variableName;

Example:  String greeting = "Hello, Dave!";

Purpose:To define a new variable of a particular type and optionally supply an initial value

Page 21: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

Identifiers

• Identifier: name of a variable, method, or class

• Rules for identifiers in Java: Can be made up of letters, digits, and the underscore

(_) character Cannot start with a digit Cannot use other symbols such as ? or % Spaces are not permitted inside identifiers You cannot use reserved words They are case sensitive

Continued…

Page 22: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

The Assignment Operator

• Assignment operator: =

• Not used as a statement about equality

• Used to change the value of a variable

int luckyNumber = 13; luckyNumber = 12;

Figure 1:Assigning a New Value to a Variable

Page 23: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

Uninitialized Variables

• Error:

Figure 2:An Uninitialized Object Variable

int luckyNumber;System.out.println(luckyNumber);   // ERROR - uninitialized variable

Page 24: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

Syntax 2.2: Assignment

 variableName = value;

Example: luckyNumber = 12;

Purpose:To assign a new value to a previously defined variable.

Page 25: Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

Summary Main goal: Programming as an Engineering

tool Examination: Mini-project Organization: Short(?) lectures and plenty of

programming at the PC Programming exercises are carried out in

Eclipse “Hello World” Variables are used for storing values, via

assignment; they are referred to using identifiers (names)