Question: NOTES FROM CLASS, BELOW IMAGE Objectives To learn how to modularize programs with static methods To understand how control flows between method main and other
NOTES FROM CLASS, BELOW IMAGE

Objectives To learn how to modularize programs with static methods To understand how control flows between method main and other methods To learn how to use input arguments to pass information to methods To learn how to return a value from a method 1
Top-Down Design and Structure Charts top-down design a problem solving method first, break a problem up into its major subproblems solve the subproblems to derive the solution to the original problem structure chart a documentation tool that shows the relationships among the subproblems of a problem 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. 2
House and Stick Figure 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. 3
Structure Chart for Drawing a Stick Figure 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. 4
Method Call Statement (Method Without Arguments) Syntax mName(); Example: draw_circle(); Interpretation the method mName is called after mName has finished execution, the program statement that follows the method call will be executed 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. 5
2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. 6 Main Method for Stick Figure /* * main method is where execution begins */ public static void main(String[] args) { //draw Stick figure with method calls without arguments drawCircle(); drawTriangle(); drawIntersect(); }//end main
Static Method Definitions (Method Without Parameters) Syntax modifier static ftype fname() { local declarations executable statements } 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. 7
Method drawCircle() 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. 8 /* * Method definition for drawCircle * Purpose: Draws a Circle */ public static void drawCircle() { System.out.println(" * "); System.out.println(" * *"); System.out.println(" * * "); }
Method drawTriangle 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. 9 /* * Method definition for drawTriangle * Purpose: Draws a Triangle */ public static void drawTriangle() { drawIntersect(); drawBase(); }
Placement of Methods in a Program Program to Draw a Stick Figure 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. 10
2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. 11 /* * main method is where execution begins */ public static void main(String[] args) { //draw Stick figure with method calls without arguments drawCircle(); drawTriangle(); drawIntersect(); }//end main /* * Method definition for drawCircle * Purpose: Draws a Circle */ public static void drawCircle() { System.out.println(" * "); System.out.println(" * *"); System.out.println(" * * "); }//end drawCircle Drawing Continued
2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. 12 /* * Method definition for drawTriangle * Purpose: Draws a Triangle */ public static void drawTriangle() { drawIntersect(); drawBase(); }//end drawTriangle /* * Method definition for drawIntersect * Purpose: Draws Intersecting Lines */ public static void drawIntersect() { System.out.println(" /\\ "); System.out.println(" / \\ "); System.out.println(" / \\"); }//end drawIntersect Drawing Continued
2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. 13 /* * Method definition for drawBase * Purpose: Draws a Line */ public static void drawBase() { System.out.println(" ------"); }//end drawBase Drawing Continued
Advantages of Using Method Subprograms procedural abstraction a programming technique in which a main method consists of method calls and each method is implemented separately reuse of method subprograms methods can be executed more than once in a program 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. 14
Flow of Control Between the main method and a method Subprogram 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. 15 public static void main(String[] args) { drawCircle(); drawTriangle(); drawIntersect(); } public static void drawCircle() { System.out.println(" * "); System.out.println(" * *"); System.out.println(" * * "); } When drawCircle is called in main, control is handed over to method drawCircle and statements are executed. When the ending brace is reached control is returned to
According to the lecture notes (ONLY slides 1-15) which words must precede a method definition in Java? Select the THREE that apply. \begin{tabular}{l} void \\ \hline double \\ \hline static \\ \hline private \\ \hline public \\ \hline int \\ \hline function \\ \hline char \\ \hline method \end{tabular}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
