cosc111:$$ computer$programming$i - university of british columbia · 2014-08-10 · loops •...

45
COSC 111: Computer Programming I Dr. Bowen Hui University of Bri>sh Columbia Okanagan 1

Upload: others

Post on 10-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

COSC  111:    Computer  Programming  I  

Dr.  Bowen  Hui  University  of  Bri>sh  Columbia  

Okanagan  

1  

Page 2: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Model  Repe>>ons  

•  Some>mes,  we  want  to  express:  “do  X  ten  >mes”  “do  X  un>l  A  is  false”  “as  long  as  A  is  true,  do  X”  

•  Examples:  – Draw  5  lines  –  Tally  up  all  the  numbers  un>l  everyone’s  been  counted  (e.g.,  sum)  

– While  there’s  s>ll  >me  leT  in  the  game,  count  down  the  >me  and  let  user  keep  playing  

2  

Page 3: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Loops  

•  In  Java,  there  are  3  kinds  of  statements  that  let  you  model  repe>>on  – while  loop  –  for  loop  – do-­‐while  loop  (not  required  for  this  course)  

3  

Page 4: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

While  loop  •  Template:  while(  condi&on  )      statement  where  a  statement  can  be  replaced  by  a  statement  block  

•  Visualiza>on:    

•  ATer  statement  is  executed,    go  back  to  check    the  condi>on  

4  

Page 5: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Example  •  Recall  template:  while(  condi&on  )      statement  

•  Example:  Random  gen  =  new  Random();  boolean  isEven  =  checkEvenOdd(  gen.nextInt()  );  //  this  method  has  to  be  defined  somewhere  else    while(  !isEven  )      isEven  =  checkEvenOdd(  gen.nextInt()  );    //  what  is  the  value  of  isEven  at  the  end  here?  

5  

Page 6: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Comparing  Strings  

6  

•  To  compare  if  two  numbers  are  equal,  use  ==  –  E.g.:  int  x,  y;  …  if(  x  ==  y  )  …  

•  To  compare  if  two  strings  are  equal,  use  the  equals()  method  –  E.g.:  String  a,  b;  …  if(  a.equals(  b  )  )  …  

Page 7: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

User  Input  example  

7  

private  void  prMenu()  {  

 System.out.println(  “Select  one  of  these:”  );    System.out.println(  “1.  Coffee”  );    System.out.println(  “2.  Tea”  );    Scanner  sysin  =  new  Scanner(  System.in  );    int  userInput  =  sysin.nextInt();    while(  userInput  <  1  &&  userInput  >  2  )    {      System.out.println(  “Pick  1  or  2”  );      userInput  =  sysin.nextInt();    }    System.out.print(  “You  selected:  ”  );    if(  userInput  ==  1  )      System.out.println(  “coffee”  );    else      System.out.println(  “tea”  );  

}  

Page 8: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

User  Input  example  

8  

private  void  prMenu()  {  

 System.out.println(  “Select  one  of  these:”  );    System.out.println(  “1.  Coffee”  );    System.out.println(  “2.  Tea”  );    Scanner  sysin  =  new  Scanner(  System.in  );    String  userInput  =  sysin.nextInt();    while(  userInput  <  1  &&  userInput  >  2  )    {      System.out.println(  “Pick  1  or  2”  );      userInput  =  sysin.nextInt();    }    System.out.print(  “You  selected:  ”  );    if(  userInput  ==  1  )      System.out.println(  “coffee”  );    else      System.out.println(  “tea”  );  

}  

Page 9: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

User  Input  example  

9  

private  void  prMenu()  {  

 System.out.println(  “Select  one  of  these:”  );    System.out.println(  “1.  Coffee”  );    System.out.println(  “2.  Tea”  );    Scanner  sysin  =  new  Scanner(  System.in  );    String  userInput  =  sysin.nextLine();    while(  !userInput.equals(  “coffee”  )  &&  !userInput.equals(  “tea”  )  )    {      System.out.println(  “Type  coffee  or  tea”  );      userInput  =  sysin.nextLine();    }    System.out.print(  “You  selected:  ”  +  userInput  );  

}  

Page 10: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

User  Input  example  v2  

10  

private  void  prMenu()  {  

 System.out.println(  “Select  one  of  these:”  );    System.out.println(  “1.  Coffee”  );    System.out.println(  “2.  Tea”  );    Scanner  sysin  =  new  Scanner(  System.in  );    int  userInput  =  sysin.nextInt();    while(  userInput  <  1  ||  userInput  >  2  )    {      System.out.println(  “Pick  1  or  2”  );      userInput  =  sysin.nextInt();    }    System.out.print(  “You  selected:  ”  );    if(  userInput  ==  1  )      System.out.println(  “coffee”  );    else      System.out.println(  “tea”  );  

}  

take  integer  input    integer  comparisons          print  selec>on  depending  on  integer  stored  

Page 11: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

For  loop  •  Template:  

for(  ini&aliza&on;  condi&on;  increment  )      statement  where    –  a  statement  can  be  replaced  by    a  statement  block  

–  an  ini>aliza>on  gives  a  star>ng  value  to  a  variable  

–  condi>on  checks  the  value  of  that  variable  

–  increment  updates  that  variable    

•  Visualiza>on:  

11  

Page 12: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Example  

•  Recall  template:  for(  ini&aliza&on;  condi&on;  increment  )      statement    

•  Example:  int  counter;  for(  counter=1;  counter<=5;  counter++  )      System.out.println(  counter  );    //  what  output  will  this  display?  

 12  

keep  track  of    the  values  in  counter  

Page 13: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Small  varia>ons  

13  

•  Recall  template:  for(  ini&aliza&on;  condi&on;  increment  )      statement  

•  ini&aliza&on  can  include  a  declara>on  –  int  counter;  for(  counter=1;  …  )  …  

–  for(  int  counter=1;  …  )  …  –  same  func>onality,  but  counter  has  different  scope  

•  increment  can  be  any  assignment  –  can  be  decrement  instead  –  can  be:  counter  =  2    //  not  increment  or  decrement  x  =  y        //  completely  irrelevant  

Page 14: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Countdown  example  public  class  NYCountdown  {  

 private  int  secToGo;    public  NYCountdown(  int  secLeT  )    {      secToGo  =  secLeT;      startCoun>ng();    }    private  void  startCoun>ng()    {      for(  int  i=secToGo;  i>0;  i-­‐-­‐  )        System.out.println(  i  );      System.out.println(  “Happy  New  Year!”  );    }  

}  

14  

Page 15: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Countdown  example  public  class  NYCountdown  {  

 private  int  secToGo;    public  NYCountdown(  int  secLeT  )    {      secToGo  =  secLeT;      startCoun>ng();    }    private  void  startCoun>ng()    {      for(  int  i=secToGo;  i>0;  i-­‐-­‐  )        System.out.println(  i  );      System.out.println(  “Happy  New  Year!”  );    }  

}  

15  

Page 16: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Ac>vity:  print  N  of  *  

16  

•  Write  using  for  loop  •  Recall  template:  for(  ini&aliza&on;  condi&on;  increment  )      statement  

•  Write  using  while  loop  •  Recall  template:  while(  condi>on  )      statement  

Page 17: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Solu>ons  –  for  loop  int  N  =  10;  for(  int  i=1;  i<=N;  i++  )  

 System.out.println(  “*”  );        int  N  =  10;  int  i  =  0;  while(  i  <  N  )  {  

 System.out.println(  “*”  );    i++;  

}  

17  

int  N  =  10;  for(  int  i=0;  i<N;  i++  )  

 System.out.println(  “*”  );      int  N  =  10;  while(  N  >  0  )  {  

 System.out.println(  “*”  );    N-­‐-­‐;  

}  

Page 18: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Solu>ons  –  for  loop  int  N  =  10;  for(  int  i=1;  i<=N;  i++  )  

 System.out.println(  “*”  );        int  N  =  10;  int  i  =  0;  while(  i  <  N  )  {  

 System.out.println(  “*”  );    i++;  

}  

18  

int  N  =  10;  for(  int  i=0;  i<N;  i++  )  

 System.out.println(  “*”  );      int  N  =  10;  while(  N  >  0  )  {  

 System.out.println(  “*”  );    N-­‐-­‐;  

}  

keep  track  of  i:  i=1      *  i=2  …    *…  i=10      *  i=11          false!  

keep  track  of  i:  i=0      *  i=1  …    *…  i=9      *  i=10          false!  

Page 19: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Solu>ons  int  N  =  10;  int  i  =  0;  while(  i  <  N  )  {  

 System.out.println(  “*”  );    i++;  

}  

19  

int  N  =  10;  while(  N  >  0  )  {  

 System.out.println(  “*”  );    N-­‐-­‐;  

}  

keep  track  of  i:  i=0      *  i=1  …    *…  i=9      *  i=10          false!  

looks  like  the  long  hand  for  a  for  loop!  

Page 20: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Solu>ons  int  N  =  10;  int  i  =  0;  while(  i  <  N  )  {  

 System.out.println(  “*”  );    i++;  

}  

20  

int  N  =  10;  while(  N  >  0  )  {  

 System.out.println(  “*”  );    N-­‐-­‐;  

}  

keep  track  of  i:  i=0      *  i=1  …    *…  i=9      *  i=10          false!  

keep  track  of  N:  N=10    *  N=9  …    *…  N=1      *  N=0          false!  

Page 21: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Infinite  Loops  •  A  loop  that  repeats  forever  is  an  infinite  loop  •  Occurs  when  condi>on  is  never  false  •  What  will  happen?    

–  Program  “hangs”  –  Not  expec>ng  user  input  

•  How  to  stop  it?  –  In  command  prompt:  type  Ctrl-­‐C  –  In  Eclipse:  click  on  red  “stop”  buxon    

•  Why  does  it  happen?  –  logic  error  or  typo  –  carefully  trace  through  variables  in  the  condi>on  make  sure  those  variables  are  incremented/decremented  accordingly  

21  

Page 22: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Examples  with  Infinite  loops  int  count  =  1;  while(  count  <=  25  )  

 count  =  count  –  1;      for(  count  =  10;  count  >  0;  count++  )  

 System.out.println(  count  );      for(  count  =  2;  count  >  0;  count  =  2  )  

 System.out.println(  count  );  

22  

How  to  fix  these?    Generally,  you  want  the  increment  to  make  your  variable  get  closer  and  closer  to  viola>ng  the  condi>on  

Page 23: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Nested  Loops  int  count=1;  while(  count  <=  10  )  {  

 int  count2  =  1;    while(  count2  <=  20  )    {      System.out.print(  “here\t”  );      count2++;    }    System.out.println(  “\n”  );    count++;  

}  //  how  many  “here”  gets  printed?  

23  

Page 24: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Nested  Loops  int  count=1;  while(  count  <=  10  )  {  

 int  count2  =  1;    while(  count2  <=  20  )    {      System.out.print(  “here\t”  );      count2++;    }    System.out.println(  “\n”  );    count++;  

}  //  how  many  “here”  gets  printed?  

24  

10  rows  of  20  “here”s  

Page 25: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Ac>vity:  drawing  *  paxerns    

•  Add  a  method  called  drawTriangle()  that  draws  a  triangle  paxern  such  as:  *  **  ***  ****    

25  

public  class  Stars  {  

 private  int  N;    public  Stars(  int  num  )    {      N  =  num;    }  

}  

public  class  DrawStars  {  

 public  sta>c  void  main(  String[]  args  )    {      Stars  drawing  =  new  Stars(  4  );              drawing.drawTriangle();        drawing.drawArrowHead();    }  

}  

the  last  row  has  N  stars  

Page 26: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Solu>on  

26  

public  void  drawTriangle()  {    for(  int  i=0;  i<N;  i++  )    {      for(  int  j=0;  j<=i;  j++  )        System.out.print(  "*"  );      System.out.println();    }  

}  

Page 27: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Ac>vity:  drawing  *  paxerns    

•  Add  a  method  called  drawArrowHead()  that  draws  an  arrow  head  paxern  such  as:  *  **  ***  **  *     27  

public  class  Stars  {  

 private  int  N;    public  Stars(  int  num  )    {      N  =  num;    }  

}  

public  class  DrawStars  {  

 public  sta>c  void  main(  String[]  args  )    {      Stars  drawing  =  new  Stars(  6  );              drawing.drawTriangle();        drawing.drawArrowHead();    }  

}  

the  middle  row  has  N/2  stars  

Page 28: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Solu>on  public  void  drawArrowHead()  {  

 for(  int  i=0;  i<N/2;  i++  )    {      for(  int  j=0;  j<=i;  j++  )        System.out.print(  "*"  );      System.out.println();    }    for(  int  i=(N/2)-­‐1;  i>0;  i-­‐-­‐  )    {      for(  int  j=0;  j<i;  j++  )        System.out.print(  "*"  );      System.out.println();    }  

}  

28  

Page 29: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Loops  and  GUIs  

•  Graphical  user  interface  (GUI)  lets  us  visualize  our  programs  

•  General  ideas  of  a  class  and  test  class  are  the  same,  but  the  skeleton  used  are  slightly  different  

•  GUI  not  tested,  but:  – Need  to  know  how  to  use  them  – Need  to  know  how  to  add  axributes  and  methods  or  other  object  variables  

– Need  to  know  how  to  add  statements  inside  the  paint()  method  

29  

Page 30: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Example  

30  

Page 31: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

31  

Page 32: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

                 .  .  .  

32  

Page 33: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Steps  for  drawing  random  rectangles  

33  

Page 34: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

GUI  methods  •  setColor()  

–  Default  to  black  –  Available  colours:  red,  yellow,  blue,  orange,  pink,  cyan,  magenta,  

black,  white,  gray,  lightGray,  darkGray  –  Can  make  your  own  colours:  see  hxp://mathbits.com/MathBits/Java/

Graphics/Color.htm  •  drawRect()  

–  Pass  in:  x-­‐coordinate,  y-­‐coordinate,  width  of  rectangle,  height  of  rectangle  

Other  methods  to  try:  •  drawString()        

–  Pass  in:  text,  x-­‐coordinate,  y-­‐coordinate  •  drawImage()  

–  Pass  in:  Image  object,  x-­‐coordinate,  y-­‐coordinate,  null  –  e.g.:  

Image  pix  =  new  ImageIcon(  “lion.png”  ).getImage();  g2d.drawImage(  pix,  0,  0,  null  );   34  

Page 35: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Example  

35  

Page 36: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

36  

Page 37: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

               .  .  .  

37  

Page 38: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Steps  for  drawing  random  dots  

38  

Page 39: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

GUI  methods  •  BasicStroke  –  Class  for  defining  the  axributes  of  the  stroke  in  the  drawing  (e.g.,  width,  line  joins,  end  caps,  etc.)  

–  Example  shows  an  object  with  a  thicker  pen  width  •  setStroke()  –  Sets  the  stroke  to  the  BasicStroke  object  

•  drawLine()  –  Pass  in:  star>ng  x-­‐coordinate,  star>ng  y-­‐coordinate,  ending  x-­‐coordinate,  ending  y-­‐coordinate  

•  draw()  –  Pass  in:  a  shape  object  (see  next  example)  

39  

Page 40: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

How  to  change  color  of  dots  each  >me?  

•  Inside  the  loop:  – Generate  a  random  number  – Based  on  the  value  of  that  number,  set  the  colour  differently  

40  

Page 41: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

41  

Page 42: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

42  

Page 43: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

             .  .  .  

43  

Page 44: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

Steps  to  draw  ellipses  repeatedly  

44  

Page 45: COSC111:$$ Computer$Programming$I - University of British Columbia · 2014-08-10 · Loops • In$Java,$there$are$3$kinds$of$statements$that letyou$model$repe>>on$ – whileloop$

GUI  classes  

•  Dimension  – Class  with  width  and  height  axributes  

•  Ellipse2D  – Ellipse2D.Double  specifies  double  precision  – hxp://docs.oracle.com/javase/tutorial/2d/geometry/primi>ves.html  

•  AffineTransform  – Class  with  affine  transforma>on  methods  that  can  be  applied    

45