Transcript Variables

Basic Programming
INS-basis
GF, PDP and HST
Jens Dalsgaard Nielsen
Jan Dimon Bendtsen
Dept. of Electronic Systems
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
Prerequisites
• Computer savvy (file management, text
editing)
• Problem solving skills
• Time management
• High school math (algebra, trigonometry)
• No prior programming background required
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
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)
Chapter 1
Introduction to Java
Programming
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
Schematic Diagram of a Computer
Figure 5:
Schematic Diagram of a Computer
The Java Programming Language
• Simple
• Safe
• Platform-independent ("write once, run
anywhere")
• Rich library (packages)
• Designed for the internet
An Integrated Development
Environment
Figure 9:
An Integrated Development Environment
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: }
Output
Hello, World!
HelloTester in an IDE
Figure 12:
Running the HelloTester Program in an Integrated Development
Environment
A Simple Program
•
•
•
•
public class ClassName
public static void main(String[] args)
// comment
Method call
Figure 13:
Calling a Method
System Class
System.out Object
println Method
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
Errors
• Syntax errors
System.ouch.print(". . .");
System.out.print("Hello);
• Detected by the compiler
• Logic errors
System.out.print("Hell");
• Detected (hopefully) through testing
The Compilation Process
Figure 14:
From Source Code to Running Program
Chapter 2
Using Objects
Types and Variables
• Every value has a type
• Variable declaration examples:
String greeting = "Hello, World!";
PrintStream printer = System.out;
int luckyNumber = 13;
• Variables
 Store values
 Can be used in place of the objects they store
Types and Variables
• Every value has a type
• Variable declaration examples:
String greeting = "Hello, World!";
PrintStream printer = System.out;
int luckyNumber = 13;
• Variables
 Store values
 Can be used in place of the objects they store
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
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…
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
Uninitialized Variables
• Error:
int luckyNumber;
System.out.println(luckyNumber);
// ERROR - uninitialized variable
Figure 2:
An Uninitialized Object Variable
Syntax 2.2: Assignment
variableName = value;
Example:
luckyNumber = 12;
Purpose:
To assign a new value to a previously defined variable.
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)