+ All Categories
Home > Documents > Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202...

Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202...

Date post: 28-May-2020
Category:
Upload: others
View: 37 times
Download: 0 times
Share this document with a friend
49
COMP-202 Unit 1: Introduction CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics
Transcript
Page 1: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202Unit 1: Introduction

CONTENTS:What Is Programming?How a Computer WorksProgramming LanguagesJava Basics

Page 2: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

Last Class

• Solving many little problems to avoid thinking about too much at once!

• A tiny bit about java

Page 3: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

Today

• What is programming?•

• How computers think•

• A tiny bit more about java: The structure of a program

Page 4: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

Part 1: What Is Programming?

Page 5: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 5

Programming and Computers•In order to understand what programming is, we need to know what a computer is

Page 6: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 6

Programming and Computers•In order to understand what programming is, we need to know what a computer is•A computer is a machine that executes lists of instructions–We feed a list of instructions to the computer and the computer executes them–The computer may apply the instructions on additional information fed to the computer (the input)–The computer may produce information as a result of executing this list of instructions (the output)

Page 7: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 7

Programming and Computers Programming a computer involves two things:

1) Designing lists of instructions that will make the computer solve specific problems (a.k.a. writing your program)

2) Having the computer execute the instructions (a.k.a. running your program)

Page 8: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 8

Why do we want to program?Before we go any further, it's important to answer the

question:

“What's the benefit of programming?”

Page 9: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 9

Why do we want to program?

Page 10: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 10

Benefits of programming• Programming computers allows us to sleep much more

easily knowing that nothing ever could go wrong.• Our programs will always work perfectly and will never

crash or make any sort of mistakes.

Page 11: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 11

Actually....maybe not

Anyone used Windows Vista?

Page 12: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 12

Benefits of programmingAll jokes aside, generally we program computers because

we either can't or don't want to do the task ourselves.

Some reasons:

-very repetitive task-arithmetic of large numbers-searching through millions of documents

-having someone “help” us without having to bother a real human

-playing a game against a computer-researching information

Page 13: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 13

Giving a computer instructionsA very good way to think about giving a computer

instructions is the following:

1)First figure out what is given in the problem and where it is you are trying to go.

2)Figure out a broad picture of how you want to get from your start to the goal. When you do this, you should figure out, at each step, what you are given, and what you want to produce. Now you have several, smaller problems.

You can solve the smaller problems the same way.

Page 14: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 14

Example: Planning Dinner

Start: Goal:

We need to construct a path from the start to the goal

Page 15: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 15

Example: Planning Dinner

Start: Goal:

Page 16: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 16

Giving a computer instructionsAt each partial step, we need to make sure we have very

precisely determined what we have at the moment and what we need to produce in that step.

For example:Step 1)Shopping:

We have : a recipea burning desire to eat foodmoney

We want to produce:raw ingredients

Page 17: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 17

Giving a computer instructions

Here we can also notice that one of the things we have is not really needed to solve this partial problem.

For example:Step 1)Shopping:

We have : a recipea burning desire to eat foodmoney

We want to produce:raw ingredients

Page 18: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 18

Giving a computer instructionsAt each partial step, we need to make sure we have very

precisely determined what we have at the moment and what we need to produce in that step.

Step 2)Chopping:

We have : raw ingredients

We want to produce:chopped ingredients

What is very important about this?

Page 19: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 19

Giving a computer instructionsStep 2)Chopping:

We have : raw ingredients

We want to produce:chopped ingredients

The input of step 2 is part of the output of step 1. This means that we can use it.

If step 2 required something else (for example, if we also needed to buy knives), then this wouldn't be a complete solution.

Page 20: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 20

Giving a computer instructionsAt each partial step, we need to make sure we have very

precisely determined what we have at the moment and what we need to produce in that step.

Step 3)Cooking

We have : chopped ingredients

We want to produce:cooked food

And we've found a path from one to the other.

Page 21: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 21

Giving a computer instructionsWhen you are giving instructions to a computer, most of

the time, you will repeat this divide and conquer process.

For example:Shopping could be split into:1)Pick up onions2)Pick up bread3)Pick up meat4)Wait in line at the cash register5)Pay the cashieretc.

Page 22: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

Giving the instructions to a computer

Once you have mapped out a solution to a problem in small enough instructions, you can program a computer to execute the instructions

Page 23: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

Two things to remember about computers

1)They require very specific instructions:

-You can't tell a computer to cook dinner. It has to be much more detailed!

Page 24: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

Two things to remember about computers

2)They “interpret” instructions very literally.

-No sense of idioms as we do in human languages.

Page 25: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 25

Amelia Bedilia

Page 26: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 26

Amelia Bedilia

“Draw the drapes”

“Make a sponge cake”

“Pitch the tent”

Page 27: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 27

Human and Computer Languages

Consider the following English sentence:"The lady hit the man with a baby"

Does this mean 1)A lady hit a man who had a baby? (Dude, what a jerk!)2)A lady used a baby to hit a man? (Good lord!)3)A lady and a baby ganged up on a man and hit him. (Kids today!)

Computer statements, on the other hand, always have only one possible interpretation—although sometimes no one knows how a computer will interpret something!

Page 28: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 28

Human and Computer Languages

One of the challenges is to learn the different interpretations the computer will give to commands.

The computer will not normally tell you how it is interpreting things. It is up to you to figure it out, both by looking at your code and observing the output.

Page 29: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 29

Feedback from the computer•

If you omit the proper instructions or include the wrong instructions, generally 4 things can happen:

1)You get incredibly lucky and on that particular input it works anyway

2)The program gives the incorrect output3)The program crashes4)The program goes on forever and ever•

Page 30: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 30

Java!Now, let's go over our very first program!

Page 31: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 31

Java Program: Hello Worldpublic class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); }}

Page 32: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 32

Java Program: Hello Worldpublic class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); }}

One thing to notice is all these { and }

These are used to denote “blocks” of code.

The purpose of a block is mainly to help the programmer keep track of what parts of code are related.

Page 33: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 33

Java Program: Hello Worldpublic class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); }}

{'s always mark the beginning of a “block” (or segment) of code.

Each { always has a corresponding } to mark the end.You can tell which corresponds to which because the first

{ opened, will correspond with the last }

Page 34: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 34

Java Program: Hello Worldpublic class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); }}

Try at home: What happens if you remove the final }, so that there are not matching braces? What happens if you add an extra } at the end so that there are 3 }s?

Page 35: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 35

Java Program: Hello Worldpublic class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); }}

Most (but not all) of the time, right before a { will be some code specifying what kind of block of code something is.

For the first {, the code is “public class HelloWorld”

Page 36: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 36

class in Javapublic class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); }}

One of the key units of organizing things in Java is called a class. A class can have many different things in it which we'll see throughout the term.

Page 37: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 37

class in Javapublic class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); }}

Here we are saying: “I want to create a class called HelloWorld and I want it to be public”

The first { signifies the start of the definition of the classThe final } signifies the end of the definition of the class

Page 38: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 38

Java Program: Hello Worldpublic class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); }}

On the second line we have another {

In this case, the code before is defining a method

Page 39: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 39

methods in Javapublic class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); }}

A method is also a key unit of organization in Java.

Page 40: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 40

methods in Javapublic class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); }}

Here we are saying: “I want to create a method called main

There are several other words (public, static, void, String[], args) that we'll talk about later.

Page 41: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 41

methods in Javapublic class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); }}

The second { signifies the start of the method called main.

The first } signifies the end of the definition of the method called main.

Page 42: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 42

methods in Javapublic class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); }}

Since the definition of the method called main is inside the definition of the class called HelloWorld, it means that the method main is a part of the class HelloWorld.

Page 43: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 43

Methods and classesAlmost every line of code you write in Java will be inside

a class. This helps keep things nicely organized.

Almost every line of code you write in Java will also be inside of a method. This also helps keep things nicely organized.

Every method you ever write, will be part of a class.

Page 44: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 44

main methodThe main method is a very particular method.

When you run a Java program, you actually run a Java class

When you do this, the execution of your program will always start at the beginning of the method called main inside whatever class you run.

Page 45: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 45

Java Program: Hello Worldpublic class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); }}

Inside of a method, you can put many statements or commands.

The highlighted line is an example of a statement.

All statements in Java end in a semi-colon.

Page 46: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 46

Java Program: Hello Worldpublic class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); }}

The command System.out.println is a way to print something to the screen.

It will print everything between (“ and “) as long as it's on 1 line.

Page 47: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 47

Summary:-Important to break problems down into many pieces-Classes and methods are just 2 of the ways that Java

helps us to organize things. These are important because they'll help make sure we don't need to think about too much at once.

Page 48: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 48

Homework:-Read the lecture titled unit1 on the course

webpage.

-The material is mostly not directly examinable, but it's good to keep in mind while coding.

Page 49: Unit 1: Introduction - cs.mcgill.cacs202/2012-01/web/lectures/xue/unit1/unit1short.pdf · COMP-202 - Introduction 19 Giving a computer instructions Step 2)Chopping: We have : raw

COMP-202 - Introduction 49

Next Class •Printing more complex statements•Getting input from the user to make our programs more useful!


Recommended