software and programming 1

42
SOFTWARE AND PROGRAMMING 1 Lecture: UCL Roberts Building, room 212 6.00-9.00 7 & 14.01.09 Room 135 SCSIS 7.30-9.00 from 21.01.09 Lab: SCSIS 131 6:00-7:30 from 21.01.09 EACH student must have obtained access to Birkbeck computing by 21.01.09 – otherwise no use in the lab Instructor: Prof. Boris Mirkin SCSIS, room 111, tel. 020 7631 6746 E-mail: [email protected]

Upload: pascale-joseph

Post on 01-Jan-2016

19 views

Category:

Documents


5 download

DESCRIPTION

SOFTWARE AND PROGRAMMING 1. Lecture: UCL Roberts Building, room 2126.00-9.00 7 & 14.01.09 Room 135 SCSIS7.30-9.00from 21.01.09 Lab: SCSIS 131 6:00-7:30 from 21.01.09 EACH student must have obtained access to Birkbeck computing by 21.01.09 – otherwise no use in the lab - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: SOFTWARE AND PROGRAMMING 1

SOFTWARE AND PROGRAMMING 1

Lecture: UCL Roberts Building, room 212 6.00-9.00 7 &

14.01.09 Room 135 SCSIS 7.30-9.00 from

21.01.09

Lab: SCSIS 131 6:00-7:30 from

21.01.09

EACH student must have obtained access to Birkbeck computing by 21.01.09 – otherwise no use in the

lab

Instructor: Prof. Boris MirkinSCSIS, room 111, tel. 020 7631 6746E-mail: [email protected]

Course Assistant:

Lab/WebCT/Tests/Assignments: Mr Martin O’SheaE-mail: [email protected]

Page 2: SOFTWARE AND PROGRAMMING 1

2

Webpage

The course web page is currently at my open web-site, with lecture notes and other relevant materials:

http://www.dcs.bbk.ac.uk/~mirkin/sp109

Page 3: SOFTWARE AND PROGRAMMING 1

3

Texts

1. Quentin Charatan & Aaron Kans [CK]JAVA in Two Semesters, 2nd Edition, McGraw-Hill, 2006,

ISBN-10 00771088922. David J. Barnes & Michael Kölling [BK]

Objects First with Java: A Practical Introduction using BlueJ, 2nd edition, Pearson Education, 2005, ISBN 0-13-124933-9The publisher supplies a helpline in installing related software

3. I. Pohl, C. McDowell [PM]Java by dissection, Addison-Wesley, 2000,

ISBN 02017515854. J. Farrell [F]

Java Programming, 2-4 editions, Course Technology, Thompson, 2003-7, ISBN 0-619-21500-3 etc.

5. Free: ON-LINE text by D. Eck (on my web site); other URLs

Page 4: SOFTWARE AND PROGRAMMING 1

4

Teaching Goal: Coding in Real  FRAMEWORK: 1. the interpreter/compiler Java working;2. classes, objects and instances;3. variables and static variables;4. Java data types: primitive and user-

defined;5. arithmetic and Boolean expressions;6. for/while and if…elseif…else structures;7. processing strings;8. elements of input/output in Java;9. methods and constructors;10. arrays and their usage

Page 5: SOFTWARE AND PROGRAMMING 1

5

Teaching Goal PRACTICE:

To have developed skills in practical programming of small but real-world problems e.g. keeping transaction records,

assigning seats to customers, managing a bus schedule, etc.

 

Page 6: SOFTWARE AND PROGRAMMING 1

6

Control• Final exam: 75% of mark• Course-work: 25% of mark

– 2 open-book in-class tests, 4.02 & 4.03,– 2 assignments for home work via WebCT,

4.02-4.03 & 18.02-20.03

Composition of CW 25% mark: T1 5% T2 7% A1 5% A2 8%

Page 7: SOFTWARE AND PROGRAMMING 1

7

Control: Example• Let marks of a student be as follows:

– Exam: 50– T1 12– T2 65– A1 100– A2 0 (hasn’t submitted)

• The total mark will be 48 because it rounds up the result:

50*0.75 + 12*0.05 + 65*0.07 + 100*0.05 + 0*0.08=

=37.5 + 0.6 + 4.55 + 5 + 0 = 47.65

Page 8: SOFTWARE AND PROGRAMMING 1

8

Teaching Philosophy (extracts)

• Huge respect for part-time students• The bulk of students to get trained• No student’s question is stupid• Better to have simple things learnt well

rather than complex things learnt not so well

• Instructor’s small errors - a teaching device for activation of learning process

• Learning a language in a college differs from consuming a lunch in a diner in many ways

• Fast feedback on students’ issues if raised

Page 9: SOFTWARE AND PROGRAMMING 1

9

OOP concepts you already know of

• How a Java program works: interpret/compile

• Variable

• Expression

• Data type

• Loop

• If/elseif/else structure

• Class

• Method

• ParameterThese will be expanded to more realistic environments

Page 10: SOFTWARE AND PROGRAMMING 1

10

Two JAVA environments• Will be using both:

• Java Developer Kit JDK (currently, J2SE) (Conventional)

• Blue J (A public project to make JAVA coding easier)

Page 11: SOFTWARE AND PROGRAMMING 1

11

Conventional JDK: Editing• A source code can be edited in any

text editor: Notepad, emacs, PFE, ...• MS Word caveat: by default, Word

does not save in ASCII text format• Make sure to save the code before

compiling! The file name: the same as that of the class, with extension:

say, class NicTe{…} must be saved as file NicTe.java, case sensitive

Page 12: SOFTWARE AND PROGRAMMING 1

12

Command line invocation

• compilation and execution of Java in JDK are done from a command line

• On Microsoft systems: DOS shell• On Unix: Unix shell• Must make sure that the commands

for compiler and runtime (JVM) are in the command path.

Page 13: SOFTWARE AND PROGRAMMING 1

13

Getting JDK on a system’s path

• Click “Properties” on right-buttoned “My computer”

• Click “Advanced”• Click “Environmental variables”• Enter new path (to the directory

in which javac.exe and java.exe reside)

Page 14: SOFTWARE AND PROGRAMMING 1

14

Compiling with JDK

• Name of the JDK compiler: javac• To invoke:javac <source name>

• compiles <source name> and all classes it depends on into an executable on JVM file <source name>.class

• Example:

javac NicTe.javaproduces file NicTe.class

Page 15: SOFTWARE AND PROGRAMMING 1

15

Execution in JDK

• “java” starts the Java virtual machine:

java NicTe• The named class is loaded and

execution is started.• Other classes are loaded as

needed.• Only possible if class has been

compiled into a file, say, NicTe.class

Page 16: SOFTWARE AND PROGRAMMING 1

16

JDK Problem: Execute what?

How does the system know which of the methods in a class to execute?

Page 17: SOFTWARE AND PROGRAMMING 1

17

main method in JDK

• The JDK java system always executes a method called main, it should have a certain signature:

Signature _______________________

public static void main(String[ ] args){ ...}

• To work with JDK, such a method must be present in your program!

Page 18: SOFTWARE AND PROGRAMMING 1

18

A simple program/* HelloWorld.java Purpose: printing a message to the screen */ class HelloWorld {

// Each program is organised as a class         public static void main(String[] args) {          System.out.println("Hello World!");         } } // end of class HelloWorld /* Always Three Types of Elements ONLY: -comments-class (with modifiers)-methods (with modifiers & parameters/arguments) */

Page 19: SOFTWARE AND PROGRAMMING 1

19

BlueJ coding

• BlueJ programs are organised in the so-called projects

• A BlueJ project is stored in a project-specific directory on disk

• Some files store the source code, some store the compiled code, some store additional BlueJ related information.

Page 20: SOFTWARE AND PROGRAMMING 1

20

The BlueJ directory structure

UserInterface

CalcEngine

Calculator

project: calculator c:\bluej\calculator\bluej.pkg

bluej.pkh

Calculator.java

Calculator.class

Calculator.ctxt

UserInterface.java

UserInterface.class

UserInterface.ctxt

CalcEngine.java

CalcEngine.class

CalcEngine.ctxt

Page 21: SOFTWARE AND PROGRAMMING 1

21

The BlueJ file structure

• bluej.pkg - the package file. Contains information about classes in the package. One per package.

• bluej.pkh - backup of the package file.• *.java - standard Java source file (text).

One per class.• *.class - standard Java code file. One per

class• *.ctxt - BlueJ context file. Contains extra

information for a class. One per class.

Page 22: SOFTWARE AND PROGRAMMING 1

22

• Available on BBK’s network – Java JDK (which allows you to compile and

execute your program)– BlueJ (Preferred editor)

• Installing BlueJ (for home use)– First download the Java JDK from

http://java.sun.com/j2se/1.5.0/download.jsp– Then download BlueJ from http://www.bluej

.org/download/download.html

– Run “bluejsetup-202.exe” and follow the given instructions

Software is free

Page 23: SOFTWARE AND PROGRAMMING 1

23

Objects and classes

• Classes: program templates– represent all objects of a kind

(example: “student”)• Objects == instances

– A template copy to represent a specific element of the class (“an individual student”)

– Instances are created with the so-called constructors, explicitly in JDK or somewhat easier in BlueJ

Page 24: SOFTWARE AND PROGRAMMING 1

24

Variables, methods and parameters

• classes contain data stored in the so-called variables and operations which can be invoked (they are called methods in Java)

• methods may have inputs (they are called parameters in Java) to get additional data needed to have them executed

Page 25: SOFTWARE AND PROGRAMMING 1

25

Remarks• Many instances can be created from a

single class• An object has attributes/variables:

values stored in fields (memory locations).

• The class defines what fields any its object has (a template), but each object may store its own set of values (the state of the object)

• A variable is initialised with assigning it a value, an object – with a constructor

Page 26: SOFTWARE AND PROGRAMMING 1

26

More on Method

Method in Java is a named set of instructions that transforms some input into an output. This is, actually, a machine implementation of the concept of algorithm which itself is a computational analogue to the mathematical concept of function.

Static method: is shared by all instances.

Page 27: SOFTWARE AND PROGRAMMING 1

27

Example of a method (1)Square function y = x2

x               y 1                1 2                4 5               2511            121

The table can be used for invoking a specific value, like, 72 = 49 or 102 = 100.

Page 28: SOFTWARE AND PROGRAMMING 1

28

Example of a method (2) A Java method to calculate the square function: 

public int sq(int x){return xx;} To make it work, variables are needed:

int sevs=sq(7); //puts 49 into sevs

int tens=sq(10);// puts 100 into tens

Page 29: SOFTWARE AND PROGRAMMING 1

29

Structure of a method

modifiers return-type name ( parameter-list ) {

statements;return variable/expression;   //if return type is not void

}Modifiers:

– static -       method/variable that belongs to class as whole and is shared by all instances

– public -    method/variable that is accessible from anywhere

– private -    method/variable that is accessible from only within the class

Output’s type Inputs

Page 30: SOFTWARE AND PROGRAMMING 1

30

HelloWorld with a method// Hello-world program to demonstrate BlueJ

class Hello{ // Method that does the work of printing

public void go() { System.out.println("Hello, world"); } // main method for working outside BlueJ

public static void main(String[] args) { Hello hi = new Hello(); //instance

hi.go(); //method in instance hi

}}

Page 31: SOFTWARE AND PROGRAMMING 1

31

HelloWorld : WHY

Why dots in System.out.println("Hello, world"); hi.go();

?1) To take method from a specific class

instance2) To take a class from a set of classes: Java is

organised as a hierarchically structured set of classes in individual files

Page 32: SOFTWARE AND PROGRAMMING 1

32

Assigning values

• Values are stored into fields (and other variables) via assignment statements:– variable = expression;– price = ticketCost;

• The value on the right is assigned to a variable on the left.

• A variable stores a single value, so any previous value is lost.

Page 33: SOFTWARE AND PROGRAMMING 1

33

Variable• It provides for multiple uses of the

same program• A variable is a name for a location in

memory that can hold data. • Variables are declared and/or

initialised A variable declaration includes the following:– A data type that identifies the type of data that is stored in

the variable

– An identifier that is the variable’s name– An optional assigned initial value

Page 34: SOFTWARE AND PROGRAMMING 1

34

int p;

p

p = 4;

4

In memory:

Page 35: SOFTWARE AND PROGRAMMING 1

35

Page 36: SOFTWARE AND PROGRAMMING 1

36

Scope of a variable:

       The range of statements that can access the variable.

It stretches from the declaration point to the end of the block containing the declaration

Q: WHAT is BLOCK ? (part within curly braces{…} )

Q: WHAT is DECLARATION? (type name ; 3-part command)

Page 37: SOFTWARE AND PROGRAMMING 1

37

HelloWorld N times BlueJpublic class HelloN { int number; \\ variable declared

public void go() { System.out.println("Hello,

world"); } public HelloN(int howmany) {number=howmany; } \\constr to initialise

object

public void prrt() \\printing number times

{ for(int i=1;i<=number;i++) \\loop

go(); System.out.println("ok"); } }

Page 38: SOFTWARE AND PROGRAMMING 1

38

Loop for

for(int var=1;var<=st;var++){do operation depending on var}

• Two types of parentheses: () and {}• The expression in () consists of three

different items: initialising a variable, variable update, and stop-condition

• Given a value of var, {} is executed, after which var is updated, then stop-condition checked and, if yes, {} is executed again; if no, the program proceeds further on

Page 39: SOFTWARE AND PROGRAMMING 1

39

No { } in for-loop in HelloN

Why?Let us add { }: where?

Is there any difference

between before and after “ok”?

Page 40: SOFTWARE AND PROGRAMMING 1

40

Arithmetic Expressions

•  2 * 6 / 4 + 5 – 2 * 3 = 3 + 5 – 6 = 2

• 2 * 6.0 / (4 + 5) – 2 * 3 = 12.0/9 – 6 = – 4.67 (note: reals, not integers)

• 2 * 6 / 4 + (5 – 2) * 3 = 12

There is a natural precedence structure of arithmetic operations. If you are not sure, use parentheses!!!

Page 41: SOFTWARE AND PROGRAMMING 1

41

Basic class structure

public class TicketMachine{ Inner part of the class omitted.}

public class ClassName{ Variables Constructors Methods}

The outer wrapperof TicketMachine

The contents of aclass

Page 42: SOFTWARE AND PROGRAMMING 1

42

Concepts considered• Compiler & interpreter• JDK/JDS and BlueJ environments• Method• Variable; declaration and intialisation• Primitive variable types• Class type• Arithmetic operators and expressions• Class and instance• HelloWorld class in three versions