Ch03-1 - Building Java Programs Chapter 3 Introduction to...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects Based on slides by (C) Marty Stepp and Stuart Reges, 2007
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Parameters Section 3.1
Background image of page 2
Reminder: global constants global constants to fix "magic number" redundancy problems: public static final int FIGURE_WIDTH = 5; public static void drawFigure1() { drawPlusLine(); drawBarLine(); drawPlusLine(); } public static void drawPlusLine() { System.out.print("+"); for (int i = 1; i <= FIGURE_WIDTH ; i++) { System.out.print("/\\"); } System.out.println("+"); } public static void drawBarLine() { System.out.print("|"); for (int i = 1; i <= 2 * FIGURE_WIDTH ; i++) { System.out.print(" "); } System.out.println("|"); }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Another repetitive figure Task – draw these figures: ************* ******* *********************************** ********** * * ********** ***** * * * * ***** The lines and figures are similar, but not exactly the same.
Background image of page 4
A redundant solution public class Stars1 { public static void main(String[] args) { drawLineOf13Stars(); drawLineOf7Stars(); drawLineOf35Stars(); draw10x3Box(); draw5x4Box(); } public static void drawLineOf13Stars() { for (int i = 1; i <= 13 ; i++) { System.out.print("*"); } System.out.println(); } public static void drawLineOf7Stars() { for (int i = 1; i <= 7 ; i++) { System.out.print("*"); } System.out.println(); } public static void drawLineOf35Stars() { for (int i = 1; i <= 35 ; i++) { System.out.print("*"); } System.out.println(); } ... redundant ! Would constants help? What would be a better solution? drawLine - method to draw a line of any number of stars. drawBox - method to draw a box of any size.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Parameterization parameterized method : Method that is given extra information (such as number of stars to draw) when it is called. parameter : Value passed to method by the caller. Writing parameterized methods requires 2 steps: write the method to accept the parameter call the method and pass the parameter value (s) desired main drawLine ******* 7 drawLine ************* 13
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/15/2010 for the course COMPSCI Comp 142 taught by Professor Topham during the Spring '06 term at Shoreline.

Page1 / 23

Ch03-1 - Building Java Programs Chapter 3 Introduction to...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online