simplifying the penicillin code exercise
play

Simplifying the Penicillin Code Exercise private Window window; } - PowerPoint PPT Presentation

Great Days in History: 28 September 1928 } 92 years ago? } Sir Alexander Fleming finds mould growing in his lab, leading to? } the discovery of penicillin } In honor of the occasion, the penicillium mold is rendered by computer: Class


  1. Great Days in History: 28 September 1928 } 92 years ago…? } Sir Alexander Fleming finds mould growing in his lab, leading to…? } …the discovery of penicillin } In honor of the occasion, the penicillium mold is rendered by computer: Class #27: More about Writing Methods Software Design I (CS 120): D. Mathias 2 Software Design I (CS 120) 1 2 Simplifying the Penicillin Code Exercise private Window window; } To do: Take the full code } Pause the video so you can think about how you would public Penicillin() create a drawBlob() method. to draw the dish full of { // Code to make window (omitted here) penicillium spores… } If you want to be adventurous (of course you do), edit // declare the Oval variables to draw the image Oval dish1, dish2; Oval blob1, blob2, blob3, blob4, blob5, blob6; } …and replace the code the code example so that it includes a drawBlob() // draw the dish // (… code omitted here …) for the first “blob” with a method. // create one penicillium blob private method that does } download the code examples blob1 = new Oval( 140, 120, 120, 120 ); blob1.setBackground( Color. lightGray ); blob2 = new Oval( 150, 130, 100, 100 ); the same thing } create a project in Eclipse blob2.setBackground( Color. blue ); window.add( blob1 ); window.add( blob2 ); } import the files into the project // create second penicillium blob blob3 = new Oval( 140, 280, 120, 120 ); } edit PenicillinWrong.java blob3.setBackground( Color. lightGray ); blob4 = new Oval( 150, 290, 100, 100 ); blob4.setBackground( Color. blue ); window.add( blob3 ); window.add( blob4 ); // create third penicillium blob // (… code omitted here … ) } 3 4 Software Design I (CS 120) Software Design I (CS 120) 3 4 1

  2. Simplifying the Penicillin Code Exercise } To do: Let’s do the same } Now that you’ve seen how to add drawBlob() , add private Window window; thing for the dish drawDish() public Penicillin() { // Code to make window (omitted here) // declare the Oval variables to draw the image Oval dish1, dish2; Oval blob1, blob2, blob3, blob4, blob5, blob6; // draw the dish dish1 = new Oval( 50, 50, 400, 400 ); dish1.setBackground( Color. green ); dish2 = new Oval( 60, 60, 380, 380 ); dish1.setBackground( Color. yellow ); window.add( dish1 ); window.add( dish2 ); // create second penicillium blob blob3 = new Oval( 140, 280, 120, 120 ); blob3.setBackground( Color. lightGray ); blob4 = new Oval( 150, 290, 100, 100 ); blob4.setBackground( Color. blue ); window.add( blob3 ); window.add( blob4 ); // create third penicillium blob // (… code omitted here … ) } 5 6 Software Design I (CS 120) Software Design I (CS 120) 5 6 Control Flow with Methods Method Variables Now, when main() runs: public static void main( String[] args ) } private void drawBlob() { { Creates new Penicillin() Penicillin p = new Penicillin(); 1. Oval blob1, blob2; p.drawDish(); object by calling constructor blob1 = new Oval( 140, 120, 120, 120 ); p.drawBlob(); blob1.setBackground( Color. lightGray ); blob2 = new Oval( 150, 130, 100, 100 ); Control passes to constructor, 2. } blob2.setBackground( Color. blue ); which makes window elements and window.add( blob1 ); basic dish image public Penicillin() window.add( blob2 ); } { Control returns to main() // code to draw window and dish 3. // (code omitted here) } We place local variables (only used by this method) inside method } drawDish() method is called 4. } Not visible to or usable by any other part of the class // draw Petri dish Control passes to that method, 5. } Problem : this drawBlob() method is too specific! private void drawDish() which also executes and then { returns again to main() } T o draw the other blobs in their different locations, we would need two // (… code omitted here … ) } more private methods, one for each blob drawBlob() method is called 6. // draw one penicillium blob } T o do: Fix this problem. (That is, add input parameters to the Process repeats one last time, and private void drawBlob() 7. { then we come back to main() for method, so that you only need one method to draw all the blobs, // (… code omitted here … ) the last time, and program is done with each at a different locations) } 7 8 Software Design I (CS 120) Software Design I (CS 120) 7 8 2

  3. Exercise Adding Parameters to a Method } T o add input parameters: } Pause the video and attempt to add parameters to the private void drawBlob() { Figure out what you want 1. Oval blob1, blob2; drawBlob method so that with a single method for to make different each blob1 = new Oval( 140, 120, 120, 120 ); blob1.setBackground( Color. lightGray ); drawing blobs we can put blobs anywhere we want. time the method runs blob2 = new Oval( 150, 130, 100, 100 ); blob2.setBackground( Color. blue ); (here, we want to vary the window.add( blob1 ); ( x, y ) location) window.add( blob2 ); } Add a parameter for each 2. value you want to differ Find every occurrence of 3. private void drawBlob( int x, int y ) the things to change, and { Oval blob1, blob2; replace each fixed value blob1 = new Oval( x, y, 120, 120 ); with the corresponding blob1.setBackground( Color. lightGray ); blob2 = new Oval( x + 10, y + 10, 100, 100 ); parameter instead blob2.setBackground( Color. blue ); window.add( blob1 ); window.add( blob2 ); Now you have a much } } more powerful method! 9 10 Software Design I (CS 120) Software Design I (CS 120) 9 10 Method Parameters Parameters as Local Variables Parameter identifier Parameter type private void drawBlob( int x, int y ) { Oval blob1, blob2; blob1 = new Oval( x, y, 120, 120 ); blob1.setBackground( Color. lightGray ); blob2 = new Oval( x + 10, y + 10, 100, 100 ); blob2.setBackground( Color. blue ); private void drawBlob( int x, int y ) window.add( blob1 ); window.add( blob2 ); { } // Code goes here… } } When we add parameters to a method, they also act like local variables during code execution } Method parameters are variables like any other } x and y are only visible/usable inside this method } Each must be properly declared when first introduced } They take different values, depending upon what is passed to } This requires that each one be given a type and an identifier , them as input following all the normal rules for each } Each parameter in the list is separated by commas 11 12 Software Design I (CS 120) Software Design I (CS 120) 11 12 3

  4. Local Values of Input Parameters The return Statement } Now, each time the } We can mark the end of a method using simple code: public static void main( String[] args ) method is called, the { return; // run constructor to set up window values of x and y change . Penicillin pen = new Penicillin(); } This will return control to the exact location in the code // Draw the dish. 1 st call sets our variables: 1. pen.drawBlob(); where the method was originally called x == 140, y == 120 // create the penicillium blobs pen.drawBlob( 140, 120 ); Runs, after which point 2. pen.drawBlob( 140, 280 ); pen.drawBlob( 280, 200 ); x , y are forgotten (and } For simple void methods, we often do not bother } they no longer exist!) // draw one penicillium blob } The method automatically returns control when it reaches the private void drawBlob( int x, int y ) { 2 nd call runs with new 3. end of its code-block Oval blob1, blob2; parameter values: blob1 = new Oval( x, y, 120, 120 ); blob1.setBackground( Color. lightGray ); } Sometimes, however, we will want to explicitly return x == 140, y == 280 blob2 = new Oval( x + 10, y + 10, 100, 100 ); blob2.setBackground( Color. blue ); control , particularly when we want to end the method window.add( blob1 ); Runs, then forgets again 4. window.add( blob2 ); early in some situations } etc… 5. 13 14 Software Design I (CS 120) Software Design I (CS 120) 13 14 public vs. private Methods Review: public vs. private Method Access } Suppose we made the blob-drawing method public in Penicillin } If we make a method in a class C private , it can only } Then, if we give someone the Penicillin class code to use (like we be used by object instances of the class C itself have been using pre-written code for Window , Oval , etc.), they can write more code, like the following: } If we make a method in C public , it can be used by: public class Main { Any object instance from the class C itself 1. public static void main( String[] args ) { Any other class that runs the C() class constructor and 2. Penicillin pen = new Penicillin(); pen.drawBlob( 0, 0 ); creates an instance of an object of type C to use pen.drawBlob( 0, 400 ); pen.drawBlob( 400, 0 ); } When we write methods, we should decide if we want } } other coders to be able to use those methods or not Do you want this to be possible? Note : if we don ’ t want them to even be YES : then make it public } You decide! able to instantiate a Penicillin If you don’t : make method private , in NO : then make it private } object at all, then we could also make which case the compiler will not allow it. the constructor private . 15 16 Software Design I (CS 120) Software Design I (CS 120) 15 16 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend