Topic 3 Clicker 1 static Methods and What is the name of the method that is Structured Programming called when a Java program starts? "The cleaner and nicer the program, A. main the faster it's going to run. B. static And if it doesn't, it'll be easy C. void to make it fast." D. println -Joshua Bloch E. class Based on slides by Marty Stepp and Stuart Reges from http://www.buildingjavaprograms.com/ 2 Comments Using comments comment : A note written in source code by the Where to place comments: programmer to describe or clarify the code. at the top of each file (a "comment header") Comments are not executed when your program runs. at the start of every method (seen later) Syntax: to explain complex pieces of code // comment text, on one line or, /* comment text; may span multiple lines */ Comments are useful for: Examples: // This is a one-line comment. Understanding larger, more complex programs. /* This is a very long Multiple programmers working together, who multi-line comment. */ must understand each other's code. 3 4
Comments example Program Hygiene - a.ka. Style /* Suzy Student, CS 101, Fall 2019 Provide a structure to the program This program prints lyrics about ... something. */ Eliminate redundant code public class BaWitDaBa { Use spaces judiciously and consistently public static void main(String[] args) { // first verse Indent properly System.out.println("Bawitdaba"); System.out.println("da bang a dang diggy diggy"); Follow the naming conventions System.out.println(); // second verse Use comments to describe code behavior System.out.println("diggy said the boogy"); System.out.println("said up jump the boogy"); Follow a brace style } } Good software follows a style guide See links on assignment page 5 6 Google C++ Style Guide Google C++ Style Guide http://code.google.com/p/google-styleguide/ 7 8
Algorithms Why Worry About Program Hygiene ? algorithm : A list of steps for solving a problem. Computer Scientists and Software developers spend as Example algorithm: "Bake sugar cookies" much time maintaining code as they do creating new code Mix the dry ingredients. Cream the butter and sugar. Beat in the eggs. Stir in the dry ingredients. You should spend time on thinking and coding. Set the oven temperature. You should NOT be wasting time looking for that Set the timer. missing closing brace. Place the cookies into the oven. Allow the cookies to bake. "Code is read more often than it is written." Mix ingredients for frosting. - Guido Van Rossum (Creator of the Python Language) ... 10 Problems with algorithms Structured algorithms lack of structure : Many small steps; tough to structured algorithm : remember. Split solution into coherent tasks. 1 Make the cookie batter. redundancy : Consider making a double Mix the dry ingredients. batch... Cream the butter and sugar. Beat in the eggs. Mix the dry ingredients. Stir in the dry ingredients. Cream the butter and sugar. Beat in the eggs. 2 Bake the cookies. Stir in the dry ingredients. Set the oven temperature. Set the oven temperature. Set the timer. Set the timer. Place the first batch of cookies into the oven. Place the cookies into the oven. Allow the cookies to bake. Allow the cookies to bake. Set the oven temperature. Set the timer. 3 Add frosting and sprinkles. Place the second batch of cookies into the oven. Mix the ingredients for the frosting. Allow the cookies to bake. Spread frosting and sprinkles onto the cookies. 11 12 Mix ingredients for frosting. ... ...
Removing redundancy A program with redundancy A well-structured algorithm can describe // This program displays a delicious recipe for baking cookies. public class BakeCookies { repeated tasks with less redundancy. public static void main(String[] args) { 1 Make the cookie batter. System.out.println("Mix the dry ingredients."); System.out.println("Cream the butter and sugar."); Mix the dry ingredients. System.out.println("Beat in the eggs."); ... System.out.println("Stir in the dry ingredients."); System.out.println("Set the oven temperature."); System.out.println("Set the timer."); 2a Bake the cookies (first batch). System.out.println("Place a batch of cookies into the oven."); Set the oven temperature. System.out.println("Allow the cookies to bake."); System.out.println("Set the oven temperature."); Set the timer. System.out.println("Set the timer."); ... System.out.println("Place a batch of cookies into the oven."); System.out.println("Allow the cookies to bake."); 2b Bake the cookies (second batch). System.out.println("Mix ingredients for frosting."); System.out.println("Spread frosting and sprinkles."); 3 Decorate the cookies. } } ... 13 14 Building complex systems is hard Static methods Some of the most complex systems are static method : A named group of statements. software systems denotes the structure of a program class eliminates redundancy by code reuse method A statement statement procedural decomposition : statement dividing a problem into methods method B statement a way to manage complexity statement method C Writing a static method is like statement adding a new command to Java. statement statement 15 16 http://spectrum.ieee.org/computing/software/why-software-fails
Design of an algorithm Using static methods 1. Design the algorithm. // This program displays a delicious recipe for baking cookies. public class BakeCookies2 { Look at the structure, and which commands are repeated. public static void main(String[] args) { // Step 1: Make the cake batter. Decide what are the important overall tasks. System.out.println("Mix the dry ingredients."); System.out.println("Cream the butter and sugar."); Good programmers do this BEFORE writing any code System.out.println("Beat in the eggs."); System.out.println("Stir in the dry ingredients."); // Step 2a: Bake cookies (first batch). System.out.println("Set the oven temperature."); 2. Declare (write down) the methods. System.out.println("Set the timer."); System.out.println("Place a batch of cookies into the oven."); Arrange statements into groups and give each group a name. System.out.println("Allow the cookies to bake."); // Step 2b: Bake cookies (second batch). System.out.println("Set the oven temperature."); System.out.println("Set the timer."); 3. Call (run) the methods. System.out.println("Place a batch of cookies into the oven."); System.out.println("Allow the cookies to bake."); The program's main method executes the other methods to // Step 3: Decorate the cookies. System.out.println("Mix ingredients for frosting."); perform the overall task. System.out.println("Spread frosting and sprinkles."); } 17 18 } Declaring a method Calling a method Executes the method's code Gives your method a name so it can be executed Syntax: <name> (); Syntax: You can call the same method many times if you like. public static void <name> () { <statement> ; <statement> ; Example: ... <statement> ; printWarning(); } Output: Example: This product causes cancer public static void printWarning() { in lab rats and humans. System.out.println("This product causes cancer"); System.out.println("in lab rats and humans."); 19 20 }
Recommend
More recommend