Download else

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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 Ng
SCSIS, room NG26, tel. 020 7631 6725
E-mail: [email protected]
Lab BBK536 : students whose family names (surnames) begin M-Y
Instructor:
Prof. Boris Mirkin
SCSIS, room 111, tel. 020 7631 6746
E-mail: [email protected]
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 website, functions with relevant
materials too:
www.dcs.bbk.ac.uk/~mirkin/sp105
2
Formerly Recommended Texts
1. David J. Barnes & Michael Kölling
Objects First with Java: A Practical
Introduction
using BlueJ, Second edition,
Pearson Education,
2005, ISBN 0-13-124933-9
The publisher supplies a helpline (team’s telephone
included) in installing the related software
2. J. Farrell
Java 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
3
Currently Recommended Text
Q. Charatan, A. Kans
Java 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 the
material: an excellent text for slow learners.
4
Software is free
• 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
5
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
6
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.java
produces file NicTe.class
7
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
8
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)
9
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)
10
Concepts to be learnt
•
•
•
•
•
•
Arithmetic expression and precedence
Casting
Boolean expression
Statement
Loops for, while
Choice structure if/elseif/else
11
Five arithmetic operations in Java
• * multiplication 53=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)
12
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
13
Unifying Type
Unifying Type: The type of result when calculations use
different types
Order for Implicitly Establishing Unifying Type:
•
double
•
float
•
long
•
int
•
short
•
byte
14
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;
15
Boolean expressions: true or false
• |, ||
or
• &, &&
and
• !
not
• ==
equal to
• <
less than
• >
greater than
• <=
< or ==
• >=
> or ==
Always after arithmetic; If not sure, use
parentheses (…): first performed inside
16
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?
17
Precedence table
18
Loop for
for(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 { }
19
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
20
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?
21
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
22
Example: answer 2
int K = 10;
while(K >1) {
if (K< 7)
break;
else
System.out.print(“ ” + K);
K--;
}
23
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) */
24
BlueJ HelloWorld N times
public 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");
}
25
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.
26
Three branching structures
(1) Do under a condition; otherwise do
nothing [if… structure]
if(BooleanExpr) Statement or
if(BooleanExpr) {Statements}
(2) Do under a condition; otherwise do
differently [if…else… structure]
if(BooleanExpr)
{Statements1}
else
{Statements2}
27
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
28
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;
29
Statements
•
•
•
•
•
Assignment (followed by ;)
Method call (followed by ;)
if/ifelse/else (block, no ;)
for/while loop (block, no ;)
break (followed by ;)
30
This is what was covered tonight
•
•
•
•
•
•
•
Arithmetic expression and precedence
Casting
Boolean expression
Statement
Loop for
Loop while
Choice structure if/elseif/else
31