220 likes | 284 Views
Dive into software basics in this lecture discussing variable types, expressions, loops, classes, and methods. Learn about arithmetic expressions, type casting, and boolean logic. Instructor-led with hands-on lab sessions.
E N D
O 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-F Instructor: Mr Zheng Zhu LKL, tel. 020 7763 2115 E-mail: zheng@dcs.bbk.ac.uk Lab G03 Clore Centre: students whose family names fall in G-Ka Instructor: Mrs Jenny Hu SCSIS, room NG26, tel. 020 7631 6726 E-mail: jennychhu@yahoo.com Lab 12 Gordon Sq. 43: students whose family names fall in Ke -Y Instructor: Prof.Boris Mirkin SCSIS, room 111, tel. 020 7631 6746 E-mail: mirkin@dcs.bbk.ac.uk
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 (marie-helene@dcs.bbk.ac.uk) An open-to-all web-site with lecture notes, schedule and files: www.dcs.bbk.ac.uk/~mirkin/sp105
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
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 public void prrt() \\printing number times { for(int i=1;i<=number;i++) \\loop go(); System.out.println("ok"); } }
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 whereas 2 * 6 / 4 + 5 – 2 * 3 = 2
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
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;
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
Ticket Machine (2) // TicketMachine’s continuation public 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
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
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?
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?
Loop for for(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 varis updated, then stop-condition checked and, if yes, {} is executed again; if no, the program proceeds further on
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
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 whileformat?
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
Example: answer 2 int K = 10; while(K >1) { if (K< 7) break; else System.out.print(“ ” + K); K--; }
Java branching structure (1,2): • 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; else Statement2;
Java branching structure (3): (3) Several conditions to do differently [if…else if… … else if… else structure] if(BooleanExpr1) Statement1; else if(BooleanExpr2) Statement2; else Statement3; • Note NO Bool. Exp at else
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;