COMPSCI4039: Programming
For MSc IT, MSc SD, MSc ITCS, and others Lecture 1: Hello World
IT Programming – Introduction
Copyright By PowCoder代写 加微信 powcoder
¡ñ We assume that you have never programmed before
¡ñ If you have, you might find some of the initial work
straightforward…
¡ð …but we might do things differently
¡ñ Markers of success:
¡ð Attending the lectures
¡ð Attending the labs and keeping up-to-date with the exercises
¡ð Making use of the lab demonstrators
¡ð Setting and completing your own exercises
¡ñ We will teach you to program in the Java language ¡ð Many other languages are available….
¡ñ As a language for beginners it has strengths:
¡ð Widely used (good for getting a job)
¡ð It is very formal
¡ð Introduces you to the concept of Object Orientation
¡ð It¡¯s free
¡ð It¡¯s portable
¡ñ …and weaknesses:
¡ð It is very formal
¡ð It forces you to get to grips with Object Orientation
¡ð For a few weeks you will be typing commands you might not
fully understand
¡ñ We are trying to teach you programming
¡ð This is a general skill, not tied to a specific language
¡ñ Inevitably, some things we show you will be Java specific ¡ð We will do our best to highlight these
¡ñ We will also have to initially ¡®gloss over¡¯ some concepts that
we will return to later
¡ð This is to ensure you can get programs working as quickly as possible
¡ñ Feel free to play around with other languages (we can help
you in the lab)
¡ð Python is a good choice (and something you¡¯ll use in Semester 2 if you
take ITECH)
What is programming?
¡ñ A program is a sequence of instructions that instruct a computer to perform a specific task
¡ñ Different programs -> different tasks
¡ñ Programs range from very short:
¡ð System.out.println(“Hello World!”); ¡ñ To very complex
¡ð Large programs require huge teams of highly skilled programmers
¡ð Windows 10 includes roughly 50 Million lines of code
Skills linked to programming
¡ñ Testing
¡ð Making sure things work
¡ñ Version control ¡ð Git, svn, …
¡ñ Human computer interaction ¡ð Making things usable
¡ñ The law
¡ð Particularly w.r.t regulations on data protection
Talking with computers
¡ñ Talking with computers (programming) is very different to talking with humans.
¡ñ Computers are incredibly pedantic about grammar.
¡ð You have to ask them to do things in a very precise manner.
¡ñ They are also incredibly obedient.
¡ð They do exactly what you ask, even if it¡¯s nonsense.
Being able to turn a problem described in a human way into a program that a computer can understand is one of the great skills of programming.
Programming languages
¡ñ Inside all computers is one (or more) central processing units (CPUs)
¡ñ These can perform incredibly basic operations
¡ð Even very simple operations require many tedious steps
¡ñ Fortunately, we can work in high-level languages (like Java) and the computer will convert (compile) our programs into the language that the CPU will understand.
Write high- level program
Compile into language that CPU understands
Run program
What we will need to program
A program that allows us to type in our programs and save them (e.g. a text editor)
A compiler for the language we are using
A way of integrating libraries (pre-existing code)
A way of running our compiled program
What we will need to program
– Use your own text editor (e.g. notepad ++, atom, sublime text)
– Compile and run by typing commands into the computer¡¯s command prompt
– Use an Integrated Development Environment (IDE)
– IDEs bring all of these tools together into a single program
The Eclipse IDE
¡ñ You can download and install it for free on your own computer
¡ñ Other IDEs are available (and you might prefer them)
¡ñ You might prefer not to use an IDE at all.
¡ñ Remember: IDEs are tools that help you program. Learning to use Eclipse is not the same as learning to program.
COMPSCI4039: Programming
For MSc IT, MSc SD, MSc ITCS, and others Lecture 1: Hello World
Our first Java program
¡ñ We will now write a program that displays ¡°Hello World¡± on the screen.
¡ñ In Eclipse
a. Create a new project and create a new class (the file with the
program in it)
b. Click the compile & run button
c. Handled by b!
Projects are an Eclipse thing, and not necessarily a Java thing.
Classes are what object oriented programs are built from. In the simplest case, one program = one class. Complex programs = many classes.
The anatomy of our program
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello World!”); }
This line tells the compiler that this class is called HelloWorld. Note that it must be stored in a file called HelloWorld.java.
The anatomy of our program
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello World!”); }
Curly brackets (¡°{¡° and ¡°}¡±) are used to separate blocks of code. Here they show what is inside this class (in this case, what is in our program)
The anatomy of our program
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello World!”); }
This defines a main method. The main method is where the computer starts when it runs our program. We will cover what public, static, void, String, [], args etc mean later. Note the curly brackets showing what is inside the main method.
The anatomy of our program
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello World!”); }
Here we ask Java to print ¡°Hello World!¡± using the method (System.out.println) it provides us.
Things to notice
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello World!”); }
These capital letters are vital. You¡¯ll understand what needs capitals later, but for now, make sure you copy things exactly.
These are optional, but it is standard convention that Java class names start with a capital.
Things to notice
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello World!”); }
Lines with instructions need to end with a semi-colon (¡°;¡±)
Everything inside the class is indented
Everything inside main is indented more
Indenting blocks (things surrounded by brackets) is optional but very strongly recommended.
General structure of a single class Java program
public class MyClass {
public static void main(String[] args) {
¡ñ All programs you write in the first part of this course will look like this.
¡ñ The name MyClass will change.
¡ñ The code inside the main method will
// add your code here
This is a comment and is ignored by the compiler. Good programmers put comments in their code so that other humans can understand how it works.
The single instruction in our program
System.out.println(“Hello World!”);
¡ñ This is where the action was in our program
¡ñ It makes use of a method (System.out.println) provided
by Java to print text to the screen
¡ñ We pass the text we want to display to the method
¡ñ It then does the displaying (we don¡¯t care how)
¡ñ We will:
¡ð Use lots of methods that Java provides in our programs
¡ð Write our own methods to keep our code neat and avoid writing the
same code multiple times
¡ð Detect when methods are called by noticing the round brackets ¡°(¡°
and ¡°)¡±.
We already wrote our own method
public class MyClass {
public static void main(String[] args) {
// add your code here
Here we define a method called main. Any class we want to run must have a method defined this way.
Errors (1)
¡ñ Fact: All programmers make lots of mistakes.
¡ñ Some can be detected by the compiler (and
therefore by an IDE like Eclipse)
¡ñ These are syntax errors e.g:
¡ð missing a semi-colon at the end of a line
¡ð Not having enough curly brackets
¡ð Missing out important capital lettors
¡ð Spelling the name of something wrong
¡ñ The compiler doesn¡¯t know you mean println when you write printline
¡ñ In Eclipse, these errors are highlighted in red and also stop the program compiling.
Errors (2)
¡ñ Others only appear when the program runs
¡ñ These are run-time errors e.g.:
¡ð Writing public static void min(String[] args) instead of
public static void main(String[] args)
¡ñ In this example:
¡ð The compiler is quite happy with you defining a public static
void method called min
¡ð but when the computer tries to run the program it won¡¯t be able to
find the particular format of method it is looking for (a public static void method called main)
¡ñ In Eclipse these errors show up as red output
Errors (3)
¡ñ It¡¯s also possible easy to make programs that work fine, but don¡¯t do what you intended
¡ñ These aren¡¯t detected by compilers, nor do they show up with errors when the program runs
¡ñ To remove these errors requires testing (to be covered later…)
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello Borld!”); }
COMPSCI4039: Programming
For MSc IT, MSc SD, MSc ITCS, and others Lecture 1: Hello World
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com