methods with several loops. - cs.albany.edusdc/csi201/spr13/lect/c10/c10.pdfthis is really really...

40
Methods with several loops.

Upload: others

Post on 26-Sep-2019

1 views

Category:

Documents


0 download

TRANSCRIPT

Methods with several loops.

Prerequisite Topics● Sequential computing steps (straight line prog.)● Java programming: Syntax, operations,

bookClass use.● VARIABLES:

– declare: make a named variable),

– assign: copy or recopy values into a variable

– use: read and copy or calculate with the variable's value

● METHODS: (1) making them (2) calling them● Lecture09 and Lab05: Singly nested loops:

setup, test, repeat block

Anything-With-Anything Principle:Java

(like other modern programming languages)

permits and encouragesyou

to combineANYTHING

withANYTHING: different or same

Java syntax has context-free rules.

● This is really really easy!!● Modern programming languages (+ XML) are

–extremely flexible with what you can combine with what.

– extremely rigid about nesting parentheses {, (, [

(or indentation in Python and some others)

● Take your iClicker out.● DO NOT TURN IT ON, until further notice!!

● ALL Choices (A), (B), (C), (D), (E) in the next 3 questions are FALSE, WRONG choices!!

What can't you program in a main

method?

public class anyApp{ public static void main(String[]a) {

 }}

What CAN'T you put in here?(A) Declarations, like int cDown;(B) Assignments, like  cDown = ... ;(C) Calls to Java methods, like System.out.println(“hi”);(D) Calls to methods you've written,  <ref>.<methName>( ... ); (E) Loops.

What can't you program in a method you have added to a

class like ArtisticTurtle?

public class ArtisticTurtle extends     Turtle{ public void brush (int len) {

 }}

What CAN'T you put in here?(A) Declarations, like int cDown;(B) Assignments, like  cDown = ... ;(C) Calls to Java methods, like System.out.println(“hi”);(D) Calls to methods you've written,  <ref>.<methName>( ... ); (E) Loops.

What can't you program inside the block of

statements that a loop makes the computer

repeat?

  int nStarsToPrint = 15;  while( nStarsToPrint > 0)  {

    nStarsToPrint = nStarsToPrint­1;  }

What CAN'T you put in here?(A) Declarations, like int cDown;(B) Assignments, like  cDown = ... ;(C) Calls to Java methods, like System.out.println(“hi”);(D) Calls to methods you've written,  <ref>.<methName>( ... ); (E) Loops.

public class PaintablePicture     extends Picture{ public void mM (int x, int y) {     while(        )   {        }

 }}

Can you put one loop inside a method body?(A) YES, of course. (B) No.

SETUP

TEST

INSTRUCTIONS TO REPEAT

this is a real iClicker question

public class PaintablePicture     extends Picture{ public void mM (int x, int y) {     while(        )   {        }

   while(        )   {        }  } }

Can you put two loops inside a method body?(A) YES, of course. (B) No.

SETUP

TEST

INSTRUCTIONS TO REPEAT

SETUP

INSTRUCTIONS TO REPEAT

TEST

evaluate a TEST to decide

whether togo around or to

get off.

evaluate a (different) TEST to decide whether to

go around or to get off.

The effect of that program is like one roller coaster after another

public class PaintablePicture     extends Picture{ public void mM (int x, int y) {     while(        )   {           while(         )      {

      }

   } }}

Can you put another loop inside the body of instructions to repeat belonging to a loop?(A) YES, of course. (B) No.

SETUP

TEST

MORE INSTRUCTIONS

SETUP

INSTRUCTIONS TO REPEAT

TEST

public class PaintablePicture     extends Picture{ public void mM (int x, int y) {     while(        )   {           while(         )      {

      }

   } }}

Can you put another loop inside the body of instructions to repeat belonging to a loop?(A) YES, of course. (B) No.

SETUP

TEST

MORE INSTRUCTIONS

SETUP

INSTRUCTIONS TO REPEAT

TEST

SETUPTEST

SETUP

TEST

INSTRUCTIONS TO REPEAT

MORE INSTRUCTIONS

What a for loop meansin brief.

for loops LOOK EASIERbut are really much more tricky than

while loops.They can mess up your programs if

you don't understand them well enough!

Unfortunately, text authors teach them too early!

REPEAT A

for(        ;     ;       ){    

}

SETUP TEST REPEAT B

The usual way: Thefor (    ;     ;    ) part is written on one line.

SETUP TEST PEAT B

  

while(     ){   

  }

SETUP;

TEST

REPEAT A

REPEAT B;

for(         ;             ;                   )

{    

}

SETUP

TEST

REPEAT BREPEAT A

(One statement)

Forgetaboutforloops

* WHAT, ME WORRY *

The MAD programming language ca. 1960 read about it on Wikipedia and its references.

ASCIIArtASCII=American Standard Code for Information Interchange

public class AsciiArtApp{  public static void main(String[] a)  {    AsciiArtist refAA;    refAA = new AsciiArtist( );    System.out.println("Let's draw 15 stars.");    refAA.nStars( 15 );    System.out.println("Let's draw 7 stars.");    refAA.nStars( 7 );    System.out.println("Let's draw a 15x4 box.");    refAA.nmStarBox( 15, 4 );    System.out.println("Let's draw a 4x12 box.");    refAA.nmStarBox(  4, 12);    System.out.println("Let's draw Alfred E. Newman.");    refAA.alfred( );    System.out.println("Let's draw a 15x4 box.");    refAA.nmStarBoxV2( 15, 4 );    System.out.println("Let's draw a 4x12 box.");    refAA.nmStarBoxV2(  4, 12);  }}

> java AsciiArtAppLet's draw 15 stars.***************Let's draw 7 stars.*******Let's draw a 15x4 box.************************************************************Let's draw a 4x12 box.

printed by DrJava after I clicked Run

printed by main

printed by nStars(15);

printed by main

printed by nStars(7);

printed by main

printed by nmStarBox(15, 4);

public class AsciiArtApp{  public static void main(String[] a)  {    AsciiArtist refAA;    refAA = new AsciiArtist( );    System.out.println("Let's draw 15 stars.");    refAA.nStars( 15 );    System.out.println("Let's draw 7 stars.");    refAA.nStars( 7 );    System.out.println("Let's draw a 15x4 box.");    refAA.nmStarBox( 15, 4 );    System.out.println("Let's draw a 4x12 box.");    refAA.nmStarBox(  4, 12);    System.out.println("Let's draw Alfred E. Newman.");    refAA.alfred( );    System.out.println("Let's draw a 15x4 box.");    refAA.nmStarBoxV2( 15, 4 );    System.out.println("Let's draw a 4x12 box.");    refAA.nmStarBoxV2(  4, 12);  }}

a CALL to method nStars

parameter value 15

another CALL to method nStars

parameter value 7

parameter value 15

public class AsciiArtist{ public void nStars(int nToPrintMORE) {    while( nToPrintMORE > 0 )    {      System.out.print("*");      nToPrintMORE = nToPrintMORE ­ 1;    }    System.out.println( "" ); } //More stuff to show you soon.} 

declaration of parameter variable

How the definition of the method named nStars is added to the AsciiArtist class.

public class AsciiArtApp{  public static void main(String[] a)  {    AsciiArtist refAA;    refAA = new AsciiArtist( );    System.out.println("Let's draw 15 stars.");    refAA.nStars( 15 );    System.out.println("Let's draw 7 stars.");    refAA.nStars( 7 );    System.out.println("Let's draw a 15x4 box.");    refAA.nmStarBox( 15, 4 );    System.out.println("Let's draw a 4x12 box.");    refAA.nmStarBox(  4, 12);    System.out.println("Let's draw Alfred E. Newman.");    refAA.alfred( );    System.out.println("Let's draw a 15x4 box.");    refAA.nmStarBoxV2( 15, 4 );    System.out.println("Let's draw a 4x12 box.");    refAA.nmStarBoxV2(  4, 12);  }}

parameter value 15

a CALL to method nmStarBox

parameter value 4

> java AsciiArtAppLet's draw 15 stars.***************Let's draw 7 stars.*******Let's draw a 15x4 box.************************************************************Let's draw a 4x12 box.

printed by main

printed by nmStarBox(15, 4);

Thinking about rectangles: The number of rows *********** (here 4) is the number of stars in each column!

public void nmStarBox( int nInRow,                     int mInColMORE {  while( mInColMORE > 0 )  {    

    mInColMORE = mInColMORE ­ 1;  }}

code that prints one row*******************

public void nmStarBox( int nInRow,                     int mInColMORE {  while( mInColMORE > 0 )  {    int nInRowMORE;    nInRowMORE = nInRow;    while( nInRowMORE > 0 )    {      System.out.print("*");      nInRowMORE = nInRowMORE ­ 1;    }    System.out.println( "" );    mInColMORE = mInColMORE ­ 1;  }}

code that prints one row*******************

public void nmStarBox( int nInRow,                     int mInColMORE {  while( mInColMORE > 0 )  {    int nInRowMORE;    nInRowMORE = nInRow;    while( nInRowMORE > 0 )    {      System.out.print("*");      nInRowMORE = nInRowMORE ­ 1;    }    System.out.println( "" );    mInColMORE = mInColMORE ­ 1;  }}

code that prints one row*******************

public void nmStarBox( int nInRow,                        int mInColMORE ){  while( mInColMORE > 0 )  {    int nInRowMORE;    nInRowMORE = nInRow;    //Purpose: Re­copy the count of what    //we want from the param. variable    //to the loop control variable.    while( nInRowMORE > 0 )    {      System.out.print("*");      nInRowMORE = nInRowMORE ­ 1;    }    System.out.println( "" );    mInColMORE = mInColMORE ­ 1;  }}

Destroys the count of what we want. So we re-copy it.

public void nmStarBoxV2(int nInRow,                     int mInColMORE)

{  while( mInColMORE > 0 )  {    this.nStars( nInRow );    mInColMORE = mInColMORE ­ 1;  }}

INSTEAD OF explicitly coding a loop inside the body of a loop, we can CALL the already coded nStars method to print one row *************.

code that prints one row ************

public class AsciiArtist{  public void nStars( int nToPrintMORE )  {    while( nToPrintMORE > 0 )    {      System.out.print("*");      nToPrintMORE = nToPrintMORE ­ 1;    }    System.out.println( "" );  }    public void nmStarBox( int nInRow,                            int mInColMORE )  {    while( mInColMORE > 0 )    {      int nInRowMORE;      nInRowMORE = nInRow;      //Purpose: Re­copy the       //count of we want       //from the parameter variable      //to the loop control variable.      while( nInRowMORE > 0 )      {        System.out.print("*");        nInRowMORE = nInRowMORE ­ 1;      }      System.out.println( "" );      mInColMORE = mInColMORE ­ 1;    }  }  //Continued on the next slide.

public void nmStarBoxV2(int nInRow,                           int mInColMORE)  {    while( mInColMORE > 0 )    {      this.nStars( nInRow );      mInColMORE = mInColMORE ­ 1;    }  }      public void alfred( )  {    System.out.println("Here is a stub printing:");    System.out.println("Sorry, the behavior to draw Alfred E. Newman is not implemented yet."                      );  }} //End of the definition of class AsciiArtist and the AsciiArtist.java file. 

public void nmStarBoxV2(   int nInRow, int mInColMORE){ while( mInColMORE > 0 ) {  this.nStars( nInRow );  mInColMORE = mInColMORE­1; }}public void nStars(           int nToPrintMORE ){ while( nToPrintMORE > 0 ) {  System.out.print("*");  nToPrintMORE=nToPrintMORE­1; } System.out.println( "" );}

public void nmStarBox(    int nInRow, int mInColMORE){ while( mInColMORE > 0 ) {  int nInRowMORE;  nInRowMORE = nInRow;  while( nInRowMORE > 0 )  {   System.out.print("*");   nInRowMORE = nInRowMORE­1;  }  System.out.println( "" );  mInColMORE = mInColMORE­1; }}

Which do you prefer?

public class AsciiArtApp{  public static void main(String[] a)  {    AsciiArtist refAA;    refAA = new AsciiArtist( );    System.out.println("Let's draw 15 stars.");    refAA.nStars( 15 );    System.out.println("Let's draw 7 stars.");    refAA.nStars( 7 );    System.out.println("Let's draw a 15x4 box.");    refAA.nmStarBox( 15, 4 );    System.out.println("Let's draw a 4x12 box.");    refAA.nmStarBox(  4, 12);    System.out.println("Let's draw Alfred E. Newman.");    refAA.alfred( );    System.out.println("Let's draw a 15x4 box.");    refAA.nmStarBoxV2( 15, 4 );    System.out.println("Let's draw a 4x12 box.");    refAA.nmStarBoxV2(  4, 12);  }}

Completing Project02● Make PaintablePicture extends Picture to just have the definitions of the two methods for Step 8 (purpleSplotch and SizeableSplotch) (those names are not critical for this project...SPECIFIED NAMES WILL BE critical in Project03) DON'T put a main in!!

● Adapt the principles and examples of loops and nested loops to generate MANY x and y values to locate Pixels that your code will get at with Pixel p = this.getPixel(x,y); and recolor with p.setColor( ... );