1 / 22

SFTWARE AND PROGRAMMING 1

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

demont
Download Presentation

SFTWARE AND PROGRAMMING 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  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 (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

  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 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"); } }

  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 whereas 2 * 6 / 4 + 5 – 2 * 3 = 2

  6. 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

  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 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

  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 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

  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 whileformat?

  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): • 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;

  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; else Statement3; • 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;

More Related