Docstoc

Computer Systems Introduction to Programming Lecturer

Document Sample
Computer Systems Introduction to Programming Lecturer Powered By Docstoc
					     Introduction to Programming

                   Lecturer: Steve Maybank

     Department of Computer Science and Information
                         Systems
               sjmaybank@dcs.bbk.ac.uk
                      Spring 2011


                    Week 5: if statement
11 February 2011        Birkbeck College, U. London   1
                     Overview
      Java Lab 4, Exercises 2 and 3
      Example of a class and an object
      if statement
      See Java for Everyone, Ch. 3




11 February 2011   Birkbeck College, U. London   2
                   JavaLab 4 Exercise 2
    The data is included in the program
       rather than read in from the keyboard.

       int a1=28, b1=418, c1=-89, d1=-3007;

    Find the width in characters of the field
       in which the numbers are to be printed.

11 February 2011       Birkbeck College, U. London   3
                   Exercise 2: first part
      The format specifier is %5d, where 5 is the
       field width and d stands for decimal integer.

      Use four print statements, one for each line
       of the output.

       System.out.printf(“a1:%5d\n”, a1);
       System.out.printf(“b1:%5d\n”, b1); //etc.

11 February 2011        Birkbeck College, U. London   4
             Exercise 2: second part
   double a2 = 28.467, b2 = -1.2;
   double c2 = 0.0145, d2 = 587.2;

    Find the width of the field:

  d 2 :                   5 8 7 .                2 0



11 February 2011   Birkbeck College, U. London         5
                   Format Specifier
    d 2 :                   5 8 7 .                2 0

   Use %9.2f, where f: fixed floating
    point, 9: field width, 2: number of
    places to the right of the decimal point.
   Note the rightmost 0.
   Define a format specifier that places
    587.20 adjacent to the colon.
11 February 2011     Birkbeck College, U. London         6
                   Print Statements

System.out.printf(“a2:%9.2f\n”, a2);
System.out.printf(“b2:%9.2f\n”, b2);
System.out.printf(“c2:%9.2f\n”, c2);
System.out.printf(“d2:%9.2f\n”, d2);



11 February 2011     Birkbeck College, U. London   7
                     JavaLab 4, Exercise 3

String a1=“Tom”, b1=“Jerry”;
/* Print true if a1 precedes b1 in
  lexicographic order, otherwise print false.
  Recall string1.compareTo(string2). This
  method returns an integer, but a boolean
  result is required. */

  11 February 2011        Birkbeck College, U. London   8
                     Exercise 3


System.out.println(a1.compareTo(b1)<=0);
/* And similarly for the other pairs of
  words. */




 11 February 2011   Birkbeck College, U. London   9
                   Example of a Class
   public class BankAccount
   {
     private double balance; // data held in each object
     public BankAccount() // constructor to make objects
     {
            balance = 0;     // each new object has balance=0
     }
     public void payIn(double payment) // pay into the account
     {
            balance = balance+payment;
     }
   }
11 February 2011         JFE Chapter 7.3                         10
                   Creating an Object
   BankAccount ba = new BankAccount();
   /* Create an object ba in the class
     BankAccount. The object ba has its own
     balance which is 0.*/

   double payment = 4.23;
   ba.payIn(payment);
   /* Add £4.23 to the balance in ba. */

11 February 2011      Birkbeck College, U. London   11
                   The if Statement
   int actualFloor;
   if (floor >13)
   {
       actualFloor = floor-1;
   }
   else
   }
       actualFloor = floor;
   }

11 February 2011      Birkbeck College, U. London   12
                   Alternative Code
   int actualFloor = floor;
   if (floor > 13)
   {
      actualFloor = floor-1;
   }



11 February 2011     Birkbeck College, U. London   13
           Flow Chart for if Statement

                   true     floor                   false
                            > 13?



actualFloor =                                           actualFloor=
floor-1                                                 floor




11 February 2011          Birkbeck College, U. London                  14
   Flow Chart for if Statement with no else Branch


                   true     floor                   false
                            > 13?



actualFloor =
floor-1




11 February 2011          Birkbeck College, U. London       15
                       Brackets
      Note the alignments
       {
            …
       }
      Brackets can be omitted for single statements
       (not recommended).
       if (floor > 13)
            actualFloor = floor-1;

11 February 2011    Birkbeck College, U. London   16
             Avoid Code Duplication
   if (floor > 13)
   {
             actualFloor = floor-1;
             System.out.println(“Actual floor: ”+actualFloor);
   }
   else
   {
             actualFloor = floor;
             System.out.println(“Actual floor: ”+actualFloor);
   }



11 February 2011         Birkbeck College, U. London             17
                   Duplication Removed
   if (floor > 13)
   {
             actualFloor = floor-1;
   }
   else
   {
             actualFloor = floor;
   }
   System.out.println(“Actual floor: ”+actualFloor);




11 February 2011         Birkbeck College, U. London   18
                   Multiple Alternatives
             The Richter Scale for Earthquakes

         Value                          Effect
             8      Most structures fall
             7      Many buildings destroyed
             6      Many buildings considerably
                    damaged. Some collapse.
           4.5      Damage to poorly constructed
                    buildings

11 February 2011          Birkbeck College, U. London   19
                      Multiple if Statements
if (richter>=8.0)
    {System.out.println(“Most structures fall”);}
else if (richter >= 7.0)
    {System.out.println(“Many buildings destroyed”);}
else if (richter >= 6.0)
    {System.out.println(“Considerable damage”);}
else if (richter >= 4.5)
    {System.out.println(“Damage to poorly constructed buildings”);}
else
    {System.out.println(“No destruction of buildings”);}

       11 February 2011    Birkbeck College, U. London          20
                          Result
      As soon as one of the tests succeeds
       the message is printed and no further
       tests are made.

      If no test succeeds then the final else
       clause applies.


11 February 2011   Birkbeck College, U. London   21
                   Discussion
      What happens if the order of the tests
       is reversed?

      What happens if the all the “else” words
       (and the final print statement) are
       removed?


11 February 2011   Birkbeck College, U. London   22
             Squares on a Chess Board
8

7
6
5
                                                             char file =`a`;
4                                                            int row = 3;
3
                                                             /* square a3 */
2
1
      a      b         c   d   e      f      g      h
    11 February 2011           Birkbeck College, U. London               23
                   Nested if Statements
  if (file == `a` || file == `c` || file == `e` || file == `g`)
  {
       if (row%2 == 1)
             {colour = “black”;}
       else
             {colour = “white”;}
  }
  else
  {
       if (row%2 == 0)
             {colour = “black”;}
       else
             {colour = “white”;}
  }
11 February 2011           Birkbeck College, U. London            24
               Dangling else Problem
   double shippingCharge = 5.00;
   if (country.equals(“USA”))
         if (state.equals(“HI”))
               shippingCharge = 10.00;
   else
         shippingCharge=20.00;


11 February 2011    Birkbeck College, U. London   25
       Problem Avoided by Using Brackets
double shippingCharge = 5.00;
if (country.equals(“USA”))
{
    if (state.equals(“HI”))
    {
          shippingCharge = 10.00; // Hawaii is more expensive
    }
}
else
{
          shippingCharge = 20.00; // As are shipments outside the USA
}
  11 February 2011       Birkbeck College, U. London               26

				
DOCUMENT INFO