SFTWARE AND PROGRAMMING 1
Lecture: MB33 7:30-9:00 (except 11&18.01.06)Lab: B43, MB321, MB536 6:00-7:30 (from
25.01.05)[each student must have obtained access to Birkbeck
computing]
Lab MB536: students whose family names fall in A-FInstructor:Mr Zheng ZhuLKL, tel. 020 7763 2115 E-mail: [email protected]
Lab G03 Clore Centre: students whose family names fall in G-Ka
Instructor: Mrs Jenny HuSCSIS, room NG26, tel. 020 7631 6726E-mail: [email protected]
Lab 12 Gordon Sq. 43: students whose family names fall in Ke -Y
Instructor: Prof. Boris MirkinSCSIS, room 111, tel. 020 7631 6746E-mail: [email protected]
O
2
Webpages
The course web page
webct.bbk.ac.uk
is used for announcements and assignments. To have access, ALL must submit their login to Marie-Helen ([email protected])An open-to-all web-site with lecture notes, schedule and files:www.dcs.bbk.ac.uk/~mirkin/sp105
3
Test1 8/2/6 awareness
Test1 subjects:• Variable: type, declaration,
initialisation• Expression• Loop for• Loop while• if( )… else if( ) ... else• Simple class structure• Simple method
4
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 public void prrt() \\printing number times { for(int i=1;i<=number;i++) \\loop go(); System.out.println("ok"); }
}
5
Precedence in arithmetic expressions
• /, %, and * first, then + and –:2 * 6 / 4 + 5 – 2 * 3 = 3 + 5 – 6 = 2
(here are integers only)
• To change this or if not sure, use ( ): 2 * 6.0 / (4 + 5) – 2 * 3 = 12.0/9 – 6 = –
4.67 (reals are here because of 6.0)2 * 6 / 4 + (5 – 2) * 3 = 12 whereas2 * 6 / 4 + 5 – 2 * 3 = 2
6
Unifying TypeUnifying Type: The type of result when calculations
use different types
Order for Implicitly Establishing Unifying Type:• double• float• long• int• short• byte
7
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:
• float weeklybudget = (float) bankbalance /4;
8
Boolean expressions
9
Precedence table
10
Ticket Machine (1)/* * TicketMachine models a ticket machine that issues * flat-fare tickets. */public class TicketMachine{private int price;private int balance;private int total;
public TicketMachine(int ticketCost) //constructor { price = ticketCost; balance = 0; total = 0; } public int getPrice() { return price; } public int getBalance() { return balance; }
// see next page for continuation
11
Ticket Machine (2)// TicketMachine’s continuationpublic void insertMoney(int amount) { if(amount > 0) balance = balance + amount; else { System.out.println("Use a positive amount: " + amount); } } public int refundBalance() { int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund; }// continued on the next page
12
Ticket Machine (3)// TicketMachine’s end public void printTicket() { if(balance >= price) { // Simulate the printing of a ticket. System.out.println("##################"); System.out.println("# The BlueJ Line"); System.out.println("# Ticket"); System.out.println("# " + price + " pence."); System.out.println("##################"); System.out.println();
total = total + price; // Update the total balance = balance - price; // Update the balance } else { System.out.println("You must insert at least: " + (price - balance) + " more pence."); } } }//end of class
13
A comment
I consider printTicket()method as somewhat inconsistent:
printing (an accessing activity) is mixed up with
changing the balance and total (mutating activities)
Any suggestions?
14
Questions
• How many methods are in TicketMachine?
• If there is any syntactic difference between a method and constructor (in BlueJ)?
• Which of the methods are accessors and which are mutators?
15
Loop forfor(int var=1;var<=st;var++){do operation
depending on var}
• var++ is var=var+1;• 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
16
Loop while for(init; test; update){ statements }All 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; while(test){ statements;
update } Similar elements: ( ), { }, initialisation, test
test condition (not necessarily involving the counter!), and update – in a different structure
17
Example:
for (int K = 10; K > 1 ; K--) {
//k -- is k=k-1;
if (K < 7)
{ break; } //what is “break”?
else
System.out.print(“ ” + K);
}
1. What this loop does?
2. Can it be rewritten in the while format?
18
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
19
Example: answer 2 int K = 10;
while(K >1) {
if (K< 7)
break;
else
System.out.print(“ ” + K);
K--;
}
20
Java branching structure (1,2):
(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) Statement1;
elseStatement2;
21
Java branching structure (3):
(3) Several conditions to do differently [if…else if… … else if… else structure]
if(BooleanExpr1) Statement1;
else if(BooleanExpr2)Statement2;
elseStatement3;
• Note NO Bool. Exp at else
22
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;