COMP 1001 : Introduction to Programming

Download Report

Transcript COMP 1001 : Introduction to Programming

Welcome to Hdip 001
Introduction to Programming
Lecturer: Fintan Costello
Where you are
Hdip 001 Programming lectures:




Tues 11-12 B1.08
Wed 2-3 B1.09
Thurs 11-12 B1.09
Hdip 001 programming practicals:
 Thursday 2-4 in the computer lab (block C in
science building)
 Practicals will start next week (Thursday 22nd)
Resources for the programming course
Textbook: Introduction to JAVA Programming,
Y. Daniel Liang. Prentice Hall, 4th ed.
Campus bookstore.
We’ll be using this textbook a lot: you should get it.
Make sure you get the 4th edition!
Web site: http://inismor.ucd.ie/~fintanc/dip001
Look here to find these slides, course rules and
instructions, exam info, practicals, and so on.
What will you learn on this course?
How to make a computer do what you want:
•Write programs to quickly solve complex problems
•Write programs to process information in various ways
•Write programs to respond to different situations.
You will also develop a useful attitude towards computers
•Not to be afraid when things go wrong, but to debug
•To try things on your computer and learn from your mistakes
•To solve problems in a clear, unambiguous and precise way.
Hdip D001 programming: what you do
In this course you will be learning how to
program in a computer language called Java.
You must
Attend programming lectures three times weekly
Attend practicals once weekly (1 * 2 hours)
Textbook is required reading
Marks are based on practical work (30%), plus
an exam at the end of term (70%)
Programs and algorithms: some terms
An algorithm is a precise description of how
to solve a problem
A program is a series of instructions or
commands to a computer
A well written program implements an
algorithm which solves a problem
Running a program == carrying out the
instructions
Algorithm for making tea
boil water;
put teabag into cup;
pour water into cup until full;
wait 3 minutes;
remove teabag;
if (want milk){
put milk into cup; }
if (want sugar){
put sugar into cup; }
stir;
Algorithms
An ALGORITHM is:
1. Unambiguous
2. Executable
3. Terminating
Problem: open a door
Algorithm:
read sign on door;
if (sign says "pull")
{
pull the door open;
} else {
push the door open;
}
Will this work for all doors?
Problem: Say hello to someone
Algorithm:
Ask the person’s name;
Remember their name:
NAME  their name;
Say(“Hello “ + NAME);
Programming languages
Natural languages: French, English, Swahili…
Natural languages are forgiving: if you have
wrong the grammar, pronounciation, or speling,
people can still understanding you be.
Programming languages: C, perl, Java...
Computers are not as smart as people: if you
don’t have your program’s grammar and spelling
exactly right, the computer will not understand
and will give an error
Your first java program
Welcome.java (L, p. 15)
// Welcome.java: this application prints
// a welcome message
public class Welcome
{
public static void main(String[] args)
{
System.out.println("Welcome to Java");
}
}
Reseved words
// Welcome.java: this program prints
// a welcome message
public class Welcome
{
public static void main(String[] args)
{
System.out.println("Welcome to Java");
}
}
The words in bold are reserved words: they mean something
special in the java programming language.
Reserved words are case-sensitive: Public is wrong.
What do these words mean?
// Welcome.java: this program prints
// a welcome message
public class Welcome
{
public static void main(String[] args)
{
System.out.println("Welcome to Java");
}
}
class is a word that identifies something as a program in java
public is a modifier which identifies something as publicly
accessible (other programs can “call” it, which means run it)
main identifies the main part of a program (the bit that does
most of the work). We’ll explain static and void later.
Blocks {......}
A block is a sequence of code between matching curly brackets
// Welcome.java: this program prints
// a welcome message
public class Welcome
{
public static void main(String[] args)
{
System.out.println("Welcome to Java");
}
}
This block identifies everything that is in the public
class Welcome
This block identifies everything that is in the main part
of the class.
Statements
A statement is an action.
A statement ends with a semi-colon
;
System.out.println("Welcome...");
println means ‘print this on a line of its own’ (you
can also use print, which doesn’t start a new line).
System.out means that println is an action carried
out by the output part of a set of Java programs which
you can use to interact with your computer’s System.
We’ll learn more about this later when we talk about methods
Comments in Java
Comments are simply notes for the programmer to remind
themseleves and other programmers what a program is
meant to do. Comments can contain any text you like
// this is a one line comment
// and here is another
/* this comment can
extend over several lines
.......
*/
Comments are not java commands. Always comment
your programs! It helps you remember what’s going on.