comp-202: foundations of programmingcs202/2015-05/web/lectures/lecture1_intro_colors.pdfbest if you...

63
COMP-202: Foundations of Programming Lecture 1: Introduction Sandeep Manjanna, Summer 2015

Upload: others

Post on 31-May-2020

20 views

Category:

Documents


0 download

TRANSCRIPT

COMP-202: Foundations of Programming

Lecture 1: Introduction

Sandeep Manjanna, Summer 2015

Welcome to COMP 202 !!!

• Who am I ?

• A PhD Student in the School of Computer Science

• Where am I from?

• Davangere, Karnataka, India

• What do I do?

• I program ROBOTS !!!

Why are we here?

• This is a required course for my program.

• Computer Science!!! Sounds cool. Should I pursue it?

• Computer programming can come handy.

• I love programming.

• Just giving it a try.

• I don’t know. May be I’ll have an answer at the end of this course.

What you will learn by the end of this course?

• Understand how computers “think”.

• Describe a task such that a computer can understand it by,

• Breaking a complex task into smaller and simpler sub-tasks.

• Converting these sub-tasks into instructions.

• Feeding these instructions into a computer in the form of a Java Program.

• Develop moderately complex programs.

• Understand what happens in a Computer’s brain when you next see a program running on a Computer.

• Reading and understanding the program written by someone else.

“I heard this course is Hard!”

• Programming can be very time consuming and sometimes difficult.• Need to be “perfect” – computers can’t “fill in the

blanks” and understand what makes sense in context

• Luckily, we can continually improve our program until it works.

• Unfortunately, this means we will need to get good at figuring out the error messages that the computer gives us! (Debugging).

• But the good thing is, computer tells you when you make a mistake.

“I heard this course is Hard!”

• The most time consuming part of programming is Debugging : Finding out the errors in the code you just wrote.

Key to in this course?

• Practice!!! Try out all the examples you see in the class and in your reference book.

• Be regular with your assignments.

• Don’t understand a concept? Stop me; ask Questions; and Discuss your doubts.

• Concepts depend on each other. Don’t fall behind.

• Meet the instructor and the TA’s in their office hours. Clarify your doubts as soon as possible.

• Discussion forums on MyCourses are very useful.

Course Content

Roughly speaking,

• Weeks 1 – 3: “Basics” of programming; learning

about the flow of a program

• Weeks 4 – 6: Introduction to Object Oriented

Programming

• Weeks 7 – 8: More details, applications and

examples

Prerequisites

• A CEGEP-level math course or equivalent (e.g., any upper-level math course)

• The ability to think logically and rigorously is more important than calculus, algebra or trigonometry.

• Aimed at students with little or no background in programming• If you’re concerned about your background (too little or

too much), let me know!

Prerequisites

• Important tools for this course,

• Google (Let Google help you find answers for your programming related questions)

• Wikipedia (Useful readings in Wikipedia)

Course Details

• Home page:• http://www.cs.mcgill.ca/~cs202/2015-05/web/index.html

• Lecture notes, course outline, assignment specifications, solutions, schedule for office hours and tutorials.

• myCourses:

• http://www.mcgill.ca/lms

• Assignment submission and return, discussion board, updates for tutorials and TA office hours, grades, announcements

Bulletin Board• Discussion boards are very helpful to discuss the

issues faced during the assignment or the setup.

• General questions about the assignments or course material: Post here for all to see!• OK to post small coding snippets to demonstrate a point

• NOT okay to post your entire assignment

• Also use this forum to share some interesting facts you find out during the course.

• Involved coding questions: Go to office hours.

E-mail

• I will try my best to respond to emails within 24 hours. But, for detailed programming related questions, its best if you come to office hours.

• Please put meaningful subjects (not just “comp 202” or “assignment help”)

• On issues specific to you, make sure to pay attention and stay on top of things!

• This will help assure your email isn't forgotten about. In general, it is your responsibility to make the instructors remember these sorts of issues.

Lectures

• 4.166 hours per week, compulsory

• Suggestions:• Do the readings before class (course outline)

• Ask questions if you don’t understand

• I’ll give mini-exercises to do on paper or on a laptop

• No classes on 26th and 28th of May. Will be conducting an extra class before the midterm exam. I have sent a doodle poll. Please fill it in before the next class (7th of May)

Computers in Class

• “We found that participants who multitasked on a laptop during a lecture scored lower on a test compared to those who did not multitask, and participants who were in direct view of a multitasking peer scored lower on a test compared to those who were not.”

• Faria Sana, Tina Weston, Nicholas J. Cepeda. Laptop multitasking hinders classroom learning for both users and nearby peers in Computers &

Education, Volume 62, March 2013, pp. 24–31

Outside of Class

• Tutorials (optional but very useful)• Six tutorials throughout the term, led by TAs

• Office hours• Details on the course webpage and on the course calendar.

• Schedule changes and updates will be posted on news section in myCourses

Assignments

• Practice concepts learnt in class

• Due every other week

• Very important to do them, as they offer you a chance to get feedback on your progress!

• Feel free to discuss, but complete them individually. Need to internalize the concepts for midterm and final.

• Start them early!

Midterm

• Scheduled for Thursday, June 4th 12:35pm –2:35pm (During class hours)

• Highly recommended that you attend – can only help your final mark

• If not, your midterm grade will be allocated to the final.

Finals

• Will be scheduled and informed on MyCourses before the next class.

Grading Schemes

• Scheme 1:• Assignments 40%, Midterm 20%, Final 40%

• Scheme 2:

• Assignments 40%, Final 60%

• You automatically get the better mark.

Plagiarism

• It is not right, Don’t do it!

• Severe consequences that are not worth the risks.

• It usually takes more effort to conceal the fact that a program was copied than it does to write it yourself.

• We have smart computer ‘programs’ to catch if any.

Recommended Reference

• How to Think Like a Computer Scientist• Allen B. Downey

• Freely available at:

• http://www.greenteapress.com/thinkapjava/

SOCS Computer Labs

• Accounts available to those officially registered

• Computer availability (Trottier 3rd floor):

• In open areas: physically accessible 24 hours a day, 7 days a week

• In side rooms: physically accessible on weekdays 10:00 -20:00, weekends 12:00 - 20:00

• Consultant on duty: weekdays 10:00 - 20:00, weekends 12:00 - 20:00

• Computers run GNU/Linux (Unix-like OS), not MS Windows

• Unix seminars are offered by SOCS Systems staff

• Contact SOCS Help desk: http://socsinfo.cs.mcgill.ca/wiki/Help_Desk

Required Software

• You are encouraged to use your personal computer or laptop to complete course work

• Software used in this course:

• Required: Java Development Kit (JDK)

• Recommended: Dr. Java

• Alternative to Dr. Java: Eclipse

• See course outline for details

This Lecture

• What are algorithms?

• What is programming?

• How does a computer store information?

• Language to speak to a Computer

• Errors and Debugging

• First Java Program

Algorithms

What are Algorithms

• Baking a cake:

• Mix the cake mixture with butter and egg

• Pour it into a baking pan

• Bake the cake in the oven

What went wrong???

• Instructions are very ambiguous.

• We make many assumptions:

• How to crack an egg!

• Open the butter from the wrapper

• Need a bowl to mix it

• Mix everything inside the bowl and not out…

• Computers are dumb as they don’t have brains like we do.

• We need to be very clear and particular when we give instructions to a Computer.

Algorithm

• An algorithm is a step-wise instruction to complete a given task successfully.

• An Example:• Count the number of Apples in the basket (Basket-A).

(Basket-A)

• Step 1:• Take an empty basket (lets call it Basket-B)

• Step 2:• Set a counter to zero (0 because we have zero apples in

Basket-B to start with)• COUNTER = 0

• Step 3:• Take an apple from Basket-A and put it into Basket-B.

• Increase the counter by 1.• COUNTER = COUNTER + 1

• Step 4:• Repeat Step 3 until the Basket-A is empty.

What is Programming?

• Given a task, think of a solution.

• Design an algorithm with precise instructions.

• Translate these instructions in a language that the computer can understand (coding, implementation).

• Check to see if it is correct (testing), fix problems (debugging).

What is a Program?

• Sequence of instructions.

• A program consists of,

• Input

• Output

• Mathematical Operation

• Testing

• Repetition

In our Example,

- Basket with apples

- Number of apples (Counter)

- Counter = Counter + 1

- If the Basket-A is empty

- Repeat until Basket-A is empty

Data Representation in Computers

Information representation in a Computer

• Computer is a computational device. It can understand only numbers.

• All the information stored in a computer is stored as numbers.

• How is this possible!!!!???

Text As Numbers

• Store the numerical representation of each character (e.g., ASCII)

T h i s [space] i s [space]

84, 104, 105, 115, 32, 105, 115, 32

C O M P - 2 0 2

67, 79, 77, 80, 45, 50, 48, 50

Images As Numbers

• Bitmap (.bmp): Store (R,G,B) values of each pixel

• Each pixel in the image has a numerical value between 0 and 255 and is made up of three color channels.

• So for example a pixel could be 37-red, 76-green and 125-blue and it would then look like this -

• If it was 162-red, 27-green and 12-blue, it would look like this -

Music As Numbers

• WAV format (.wav), simplified:• Store the amplitude of the sound wave over time

• The sound wave is the sum of the fundamental frequencies that make up what you hear

Programming Languages

• A way to communicate with the computer.

• Similar to French or English or any other language.

• Set of words with a list of syntax rules.

Programming Language

This is a collection of words , which when

placed together in the correct order , form a

complete sentence .

Programming Language

Syntax Matters!!!

• He made her duck.• He cooked duck for her.

• He cooked the duck that belongs to her.

• He created the duck that belongs to her (e.g., a toy).

• He caused her to lower her body suddenly.

• He magically transformed her into a mass of fowl.

• Her duck is doing something interesting, and he arrived in time to observe.

• Who are “he”, “her”, and “the duck” anyway?

“The Problem of Ambiguity”

Natural vs. Programming Language

Programming Languages

Compilers

• Remember we said computers can only understand numbers!!

• Does that mean we have to write instructions in numbers?

• How will the computer understand the instructions given in programming language?

“Compilers is the Answer”

Compilers

• Compiler is a program that translates the instructions in higher-level programming languages into computer understandable machine-code.

• You have standard compilers to compile higher-level programming languages.

Bytecode

• Machine codes are dependent on the computer architecture• Must recompile to different machine code for each

architecture

• Java aims to be portable, so that you don’t need to do this for each architecture. It does this by introducing an intermediate level called bytecode.

• Thus Java makes the program compatible with cross-architecture usage.

Interpreting Bytecode

• There is yet another program, called the Java Virtual Machine (JVM), which converts bytecode into processor instructions as they are needed.

• Now, only the JVM is dependent on the architecture.

JavaHello.java

BytecodeHello.class

compilerprocessor

instructions

JVM

Debugging

• Bug : The errors in a program.

• Debug : Correcting the errors in a program.

• There are three kinds of errors that a programmer should look out for,

• Syntax Errors

• Run-time Errors

• Logical Errors

Syntax Errors

• Syntax refers to the structure of your program and the rules about that structure.

• An analogy for syntax error in English:

“this sentence has simple syntactical errors”

• Starting the sentence with a lower-case letter.

• No period at the end of the sentence.

• These errors are notified by the compiler. Program compilation fails if you have even a single syntax error.

JavaHello.java

BytecodeHello.class

compilerprocessor

instructions

JVMSyntax Error

Run-time Errors

• This error appears while running the program.

• These errors occur when interpreter is running the bytecode.

• Run-time errors are called Exceptions. We will talk more on exceptions later in the class.

JavaHello.java

BytecodeHello.class

compilerprocessor

instructions

JVMRun-time Exception

Logical Errors

• Errors in the logic of the program.

• An example:

Using ‘<’ instead of ‘>’ by mistake in an expression• This completely reverses what you wanted to do.

• Your program will run without any errors from compiler or the interpreter.

• Thus these errors are tricky to identify and address

Debugging

Good technique : Trace your program on a piece of Paper!!!

First Java Program

Our First Java ProgramHello World!public class HelloWorld

{

public static void main(String[] args)

{

System.out.println("Hello, world!");

}

}

• How to run this? Go to command line:> javac HelloWorld.java

> java HelloWorld

• Your IDE (e.g., DrJava, Eclipse) will make this easier, but do this at least once to understand.

Our First Java Program

public class HelloWorld

{

public static void main(String[] args)

{

System.out.println("Hello, world!");

}

}

Class

Main Method

Statements

Our First Java ProgramGeneral Form

public class ClassName

{

public static void main(String[] args)

{

Statements

}

}

Curly Bracespublic class HelloWorld

{public static void main(String[] args)

{

System.out.println("Hello, world!");

}

}

• Curly braces denote “blocks” of code.

• These blocks help us keep track of what part of code are related.• You can tell which opening { matches which closing }, as

the first { corresponds to the last }

class in Javapublic class HelloWorld

{

public static void main(String[] args)

{

System.out.println("Hello, world!");

}

}

• One of the key units of organization in Java is called a class. Here, we are saying “I want to create a class called HelloWorld, which is:• public (more on this later),

• defined by what is in between the outer { and }

Methods

public 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 defines a method, another key unit of organization in Java.• Don’t worry about public, static, void, String[], args for

now.

The main Methodpublic class HelloWorld

{

public static void main(String[] args)

{

System.out.println("Hello, world!");

}

}

• The main method is a very particular method.

• When you run a Java program, you actually run a Java class.• The execution will start at the beginning of the method called

“main” inside the class that you run

Statementspublic class HelloWorld

{

public static void main(String[] args)

{

System.out.println("Hello, world!");

}

}

• Inside a method, you can put many statements or commands.• All statements in Java end in a semi-colon.

• System.out.println( contents ); prints the contents to screen.

• " " means the stuff inside is a String of text.

Try it out

• Write a Java program that prints the following, in order:

Hello !!!

Today is the 5th of May, 2015

We-we-we so excited

We gonna have COMP-202 today

Summary

Algorithms

Programming

Data in a computer

Programming Language

Compilers and Interpreters

Errors and Debugging

Hello World!! Java Program

Structure of a Java Program