© peter andreae what is programming about comp 102 #2 2012 t1 peter andreae computer science...

20
© Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

Upload: elfreda-powers

Post on 20-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: © Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae

What is Programming About

COMP 102 #2

2012 T1

Peter AndreaeComputer Science

Victoria University of Wellington

Page 2: © Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae

COMP 102 2:2Menu

• More course details• Strategies for learning in COMP102.• Programs and programming languages• Object Oriented Programming

Reading: • Text Book Chapter 1

Announcements:• Sign up for a lab session! (Labs start Today)• Voting for a Class Rep

• Put a message about yourself on the forum if you want to be class representative; the class will vote on Monday.

• Trouble with passwords? Go to school office: CO 358

Page 3: © Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae

Withdrawal dates

• Early withdrawal with refund: by Fri 16 March• no consequences to early withdrawal

• Standard withdrawal without refund: 16 March – 18 May• Withdrawal recorded• No grade on transcript• Withdrawal counts as a fail for determining "Satisfactory

Academic Progress"

• Late withdrawal with Dean's permission: after 18 May• Requires permission of Associate Dean• Normally given only when special circumstances arise after

18 May.

COMP 102 1:3

Page 4: © Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae

COMP 102 1:4Plagiarism (Cheating)

• You must not present anybody else’s work as if it were your own work: • Basic principle of academic honesty.• applies to work by other students, friends, relatives, the web,

books…• If you received substantial help, then you must state who

helped and how much.• If you declare any work from someone else, then it isn’t

plagiarism!!!

• In COMP102: • You can work in pairs on the core & completion parts of

assignments BUT• You must put a comment at the top of your code saying that

you worked with ….• If you use code from the assigned text book, or

from the lectures, then you do not need to declare it;If you use any other code that wasn’t yours, then declare it!

Page 5: © Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae

COMP 102 1:5Cheating in the assignments.

Assignments are primarily for learning, not assessing

Cheating in the assignments is not worth it!

• You won't learn, so you will probably fail.

• If caught, you'll lose marks --- or worse.

• Assignments have a small contribution to your grade.

Page 6: © Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae

COMP 102 2:6Lab Facilities

• All scheduled labs are in CO 242 and 243(or VS2.26 at A&D)

• Can use any of CO 237, 238, 242, and 243 when not booked for a course.

• Can also use home computers.

• The number of workstations is limited⇒ need to be considerate

⇒ cannot guarantee workstations available at just the time you want.

Page 7: © Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae

COMP 102 2:7Lectures vs Textbook

Lectures • Interactive • Multiple media • Real time

Good for • Overview • Motivation • Problem solving methods• Understanding• Illustration

Textbook • One way • Visual only • Static • Re-readable • Carefully checked and edited

Good for• Detailed explanations• Lists of facts and rules• Careful definitions• Large examples

Page 8: © Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae

COMP 102 2:8

A program is a specification for the behaviour of a computer:

• What the computer should do when: • the program is started• the user types something• the user clicks with the mouse• a message arrives over the network• some input from a camera/switch/sensor arrives.• ……

• Responses may be simple or very complex.

• A program consists of • descriptions of responses to events/requests • written as instructions • in a language the computer can understand:

• Low level, High level, Specialised

What is a Program

Page 9: © Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae

COMP 102 2:9Machine & Assembly

Language• What the computer can understand

• Different for each computer

• Very detailed, low-level control of the computer

• Horrible to read

::

00011001 01001111 01101001 00111010 00101001 10110101

::

copy the contents of memory location 143 into register 1.

add the contents of memory location 116 to the contents of register 1.

copy the contents of register 1 to memory location 181.

::

LD d1 143AD d1 116ST d1 181

::

Page 10: © Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae

COMP 102 2:10High Level Programming

Languages• designed for people to use• designed to be translated into

machine language• compiled (translated all at once), or• interpreted (translated one step at a time), or• compiled to an intermediate language, then

interpreted(examples at: http://www.99-bottles-of-beer.net/ )

Must be • Precise: no ambiguity about what to do• Expressive: must be able to specify whatever you want

done.• Readable: People must be able to read the

instructions.• Translatable: able to be translated into machine

language• Concise: not “long-winded” or redundant

SmalltalkML

AdaC++

EiffelProlog

HaskellMiranda

JavaC#

PythonScratch

GameMakerAlice

FORTRANLISPAlgol

COBOLBasic

CPascalSimulaModula

PHPJavascript

Page 11: © Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae

COMP 102 2:11Specialised language:

MazeMouse• Writing a program to control a Mouse in a Maze

• The mouse should get out of the maze• No matter what shape the maze is!!

• The program must cope with “the general case”!

• Very Simple Language:• Sequence of Forward, Left, and/or Right

eg: FLFR

• What should the mouse do when • there’s a space ahead

• there’s a space only to the left

• there’s a space only to the right

• there’s space only to the sides

• it’s in a dead-end

??

Page 12: © Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae

COMP 102 2:12Programming Languages

• Different languages support different paradigms: • imperative, • object-oriented, • functional, • logic programming, ...

Object Oriented programming languages:• Organise program around Classes (types) of objects

• Each class of objects can perform a particular set of actions

• Most instructions consist of asking an object to performone of its actions

Page 13: © Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae

COMP 102 2:13Java

• A high-level Object-Oriented programming language

• Designed by Sun Microsystems, early-mid 1990's.

• Widely used in teaching and industry.

• Related to C++, but simpler. Similar to C#.

• Good for interactive applications.

• Extensive libraries of predefined classesto support, UIs, graphics, databases, web applications, ...

• Very portable between kinds of computers.

Page 14: © Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae

COMP 102 2:14Constructing Programs

Design

Edit

Test

• The Design—Edit—Test cycle:

Given a task:

Page 15: © Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae

COMP 102 2:15Building programs

• Specification:• Work out what you want the program to accomplish

• Design• Work out what the computer must do to accomplish the task

• Edit• Express the design in a programming language

• instructions for individual steps, • structure of the program

• Test• Run the program and see whether it works as intended

• may need to try it out on lots of different cases.

Page 16: © Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae

COMP 102 2:16A program for the Maze

MouseSpecification

• Program to get the mouse out of any maze with a reachable exit(Mouse always starts in the top left corner facing right)

Design • "make the mouse move into an empty space,

When there is a choice, go forward if possible, otherwise to the left"

Edit• space ahead

• space only to the left

• space only to the right

• space only to the sides

• dead-end

??F

LF

RF

LF

LLF

Program

Page 17: © Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae

Testing the program

• (Try it out with the MazeMouse demo)

COMP 102 2:17

Page 18: © Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae

A different task:

Specification:• Find the average of a sequence of numbers from the user

• Design:

• Initialise a count and a running total to 0• Ask the user to enter the numbers• Repeat until there are no more numbers:

• read the next number• add it to the total• increase the count by 1

• If there was at least one number• Print out the total / count

• Otherwise, print a message.

COMP 102 2:18

Page 19: © Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae

COMP 102 2:19A Java Program

import comp102.*;/** Program to compute the average of a sequence of numbers

*/public class MeanFinder {

public void findMean () {double total= 0;int count =0;UI.print( "Enter numbers (followed by 'done'): " );while ( UI.hasNextDouble( ) ) {

total = total + UI.nextDouble( );count = count + 1;

}if (count > 0) {

UI.printf( "Mean = %5.2f \n", (total/count) ); }else {

UI.println( "You entered no numbers"); }

}}

Page 20: © Peter Andreae What is Programming About COMP 102 #2 2012 T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae

COMP 102 2:20Learning to Program in Java

What’s involved?

• Understand what the computer can do and what the language can specify

• Problem solving:• program design,• data structuring,

• Programming language (Java):• syntax and semantics• style and common patterns• libraries of code written by other people

• Testing and Debugging (fixing).

• Common patterns in program design.• Important data structures and algorithms.