SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30...
-
Upload
angelina-stewart -
Category
Documents
-
view
213 -
download
0
Transcript of SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30...
![Page 1: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/1.jpg)
SOFTWARE AND PROGRAMMING 1
Lecture: Gor B4 7:40-9:00 (from 24.01.07)Lab: SH131, BBK536 6:00-7:30 (from 24.01.07)[each student must have obtained access to Birkbeck
computing]
Lab SH131: students whose family names (surnames) begin A-L
Instructor:Ms Marie-Helene NgSCSIS, room NG26, tel. 020 7631 6725 E-mail: [email protected]
Lab BBK536 : students whose family names (surnames) begin M-Y
Instructor: Prof. Boris MirkinSCSIS, room 111, tel. 020 7631 6746E-mail: [email protected]
![Page 2: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/2.jpg)
2
Webpages
Course web page at webCT: http://vle.bbk.ac.uk
Please check it regularly. It will be used for announcements and assignments.Another page, at an open-to-all web-site, functions with relevant materials too:www.dcs.bbk.ac.uk/~mirkin/sp105
![Page 3: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/3.jpg)
3
Formerly Recommended Texts
1. David J. Barnes & Michael KöllingObjects First with Java: A Practical
Introduction using BlueJ, Second edition, Pearson Education, 2005, ISBN 0-13-124933-9The publisher supplies a helpline (team’s telephone included) in installing the related software
2. J. FarrellJava Programming, Second edition, Course Technology, Thompson, 2003, ISBN 0-619-21500-3
3. I. Pohl, C. McDowell Java by dissection, Addison-Wesley, 2000, ISBN 0201751585
4. Free: ON-LINE text by D. Eck (on my web site) and other useful URLs
![Page 4: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/4.jpg)
4
Currently Recommended Text
Q. Charatan, A. KansJava in Two Semesters, Second edition, The
McGrow-Hill Education, 2006, ISBN 978-0-07-710889-2
In fact, either will do.One more:Edward Currie (2006), Fundamentals of Programming Using
Java, Thomson Learning, ISBN-10: 1-84480-451-8.
This book contains well explained examples but covers not all thematerial: an excellent text for slow learners.
![Page 5: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/5.jpg)
5
• 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– Download BlueJ from http://www.bluej
.org/download/download.html
– Run “bluejsetup-202.exe” and follow the given instructions
Software is free
![Page 6: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/6.jpg)
6
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 7: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/7.jpg)
7
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 8: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/8.jpg)
8
Execution
• “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 9: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/9.jpg)
9
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 10: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/10.jpg)
10
Concepts from lecture 1• Compiler (javac.exe) and
Interpreter (java.exe), should be on system’s path
• JDK and BlueJ for running Java• Class (template) and Object (its
instantiation); every Java program must be a class
• Variable and its type; primitive types• Method (input-output operation)
and its Parameters (inputs - with their types at method’s declaration)
![Page 11: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/11.jpg)
11
Concepts to be learnt
• Arithmetic expression and precedence
• Casting• Boolean expression• Statement• Loops for, while• Choice structure if/elseif/else
![Page 12: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/12.jpg)
12
Five arithmetic operations in Java
• * multiplication 53=15
• / division 36/9=4, 39/9=4, 39/50=0 (integers)
36.0/9=4.0, 39.0/9=4.3333333, 39.0/50=0.78 (reals)
• % remainder 36%9=0, 39%9=3, 39%50=39
• + summation 5 + 3 = 8
• - subtraction 5 – 3 = 2
• Other operators such as Abs or exp or log are in class Math of Java (to be explained later)
![Page 13: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/13.jpg)
13
Arithmetic expressions• Precedence: */% first, then +-
If not sure, use (…): first performed inside
• 2 * 6 / 4 + 5 – 2 * 3 = 3 + 5 – 6 = 2 (integers)
• 2 * 6.0 / (4 + 5) – 2 * 3 = 12.0/9 – 6 = – 4.67 (reals are here)
• 2 * 6 / 4 + (5 – 2) * 3 = 12
![Page 14: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/14.jpg)
14
Unifying TypeUnifying Type: The type of result when calculations
use different types
Order for Implicitly Establishing Unifying Type:• double• float• long• int• short• byte
![Page 15: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/15.jpg)
15
Type casting
The unifying type can be overridden by explicitly stating a type cast:
• Place the desired type result in parentheses followed by the variable or constant to be cast
• (int) 6.0+7=13
• Example:
• int bankbalance=931786;
float weeklybudget = (float) bankbalance /4;
![Page 16: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/16.jpg)
16
Boolean expressions: true or false• |, || or• &, && and• ! not• == equal to• < less than• > greater than• <= < or ==• >= > or ==Always after arithmetic; If not sure,
useparentheses (…): first performed
inside
![Page 17: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/17.jpg)
17
Condition
int x=2;x+4*2>5 true
at what x is this false?
int x=3;int y=1;x-4 == 1.5*(y+2)
can this be true?
![Page 18: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/18.jpg)
18
Precedence table
![Page 19: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/19.jpg)
19
Loop forfor(int var=1;var<=st;var++) % no ‘;’ here!!!{do operation depending on var}var++ is var=var+1 , not var=var+2;
• Two types of parentheses: (loop specified) and {body to do}• The expression in () consists of three items in this order:
– initialising the counting variable once, – variable update, and – stop-condition
• First, – var is intialised, – stop-condition is tested;
• if true, block {} is executed,• if no, the program proceeds further on, after the block { }
– control returns to ( ) • After control returns to ( ),
– var is updated; – stop-condition is checked;
• if true, block {} is executed, then control returns to ( ), • if no, the program proceeds further on, after the block { }
![Page 20: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/20.jpg)
20
Loop while: less rigid for(init; test; update){ statements }
All three in the parentheses refer to a counter that is initialised, updated and tested over reaching the pre-specified threshold
Structure of while loop, less rigid – init, test and update are not necessarily based on counter:
init; while(test){ statements;
update } Similar elements: ( ), { }, initialisation, test
condition (not necessarily involving the counter!), and update
![Page 21: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/21.jpg)
21
Example:
for (int K = 10; K > 1 ; K--) {
//k-- is k=k-1;
if (K < 7) { break; } // Stops execution of the loop
else
System.out.print(“ ” + K);
}
1. What this loop does?
2. Can it be rewritten in the while format?
![Page 22: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/22.jpg)
22
Example: answer 1 for (int K = 10; K > 1 ; K--) {
if (K < 7)
{ break; }
else
{ System.out.print(“ ” + K);}
}
What this loop does?
Prints 10 9 8 7
![Page 23: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/23.jpg)
23
Example: answer 2 int K = 10;
while(K >1) {
if (K< 7)
break;
else
System.out.print(“ ” + K);
K--;
}
![Page 24: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/24.jpg)
24
Simplest program/* HelloWorld.java Purpose: printing a message to the screen */ class HW {
// Each program is organised as a class public static void main(String[] args)
{ System.out.println("Hello, World!"); } } // end of class HW /* Always Three Types of Elements ONLY: • comments• class (with modifiers)• methods (with modifiers and parameters) */
![Page 25: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/25.jpg)
25
BlueJ HelloWorld N timespublic class HelloN {
int number; \\ variable declared
public void go() { System.out.println("Hello,
world"); } public HelloN(int howmany) {number=howmany; } \\constr-r to initialise
an object
public void prrt() \\printing number times
{ for(int i=1;i<=number;i++) \\loop
go(); System.out.println("ok"); }
}
![Page 26: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/26.jpg)
26
No { } in for-loop in HelloN
Why? A: Because loop covers one next statement y default
Let us add { }: where? Is there any difference
between before and after “ok”?A: Yes, there is. If after, HW and ok alternate in
the out-print.
![Page 27: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/27.jpg)
27
Three branching structures
(1) Do under a condition; otherwise do nothing [if… structure]
if(BooleanExpr) Statement orif(BooleanExpr) {Statements}
(2) Do under a condition; otherwise do differently [if…else… structure]
if(BooleanExpr)
{Statements1}
else
{Statements2}
![Page 28: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/28.jpg)
28
Java branching structure (3):
(3) Several conditions to do differently [if…else if… … else if… else structure]
if(BoolExpr1) Statement1;
else if(BoolExpr2)\\and not BoolExpr1
Statement2;else \\ (not BoolExpr1) and (not BoolExpr2)
Statement3;• Note NO Bool. Exp at else
![Page 29: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/29.jpg)
29
If/else example• Ticket’s price is £5, 60+ concession £3, children
12 or less go for free• Need a variable for the age, say YourAge, and
the price, say Price;• The fragment can be as:
if (YourAge<=12) Price=0;
else if (YourAge<=60) Price=5;
else //note NO CONDITION here
Price=3;
![Page 30: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/30.jpg)
30
Statements
• Assignment (followed by ;)• Method call (followed by ;)• if/ifelse/else (block, no ;)• for/while loop (block, no ;)• break (followed by ;)
![Page 31: SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from 24.01.07) Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [each student must have obtained access.](https://reader036.fdocuments.us/reader036/viewer/2022081603/56649e915503460f94b96591/html5/thumbnails/31.jpg)
31
This is what was covered tonight
• Arithmetic expression and precedence
• Casting• Boolean expression• Statement• Loop for• Loop while• Choice structure if/elseif/else