Computer Science 202
Introduction to Programming
Fall 2012, The College of Saint Rose
BaaBaa2Methods BlueJ Project
Click here to download a BlueJ project for BaaBaa2Methods.
BaaBaa2Methods Source Code
The Java source code for BaaBaa2Methods is below. Click on a file name to download it.
/* * Example BaaBaa2Methods: move all of the work into 2 methods, * including that which is only done once. * * Jim Teresco, The College of Saint Rose, CSC 202, Fall 2012 * * $Id: BaaBaa2Methods.java 1975 2012-11-07 18:41:56Z terescoj $ */ public class BaaBaa2Methods { // a method to perform the 4 println statements that // were being repeated in the BaaBaaBad version public static void refrain() { System.out.println("Baa, baa, black sheep,"); System.out.println("Have you any wool?"); System.out.println("Yes sir, yes sir,"); System.out.println("Three bags full."); } // this method is just for readability -- we call it // just once, but it simplifies our main method public static void verse() { System.out.println("One for the master,"); System.out.println("One for the dame,"); System.out.println("And one for the little boy"); System.out.println("Who lives down the lane."); } // the main method is still where our program will begin // executing, even though there is now an additional // method defined within our class public static void main(String[] args) { // we replace the println statements here // calls that each print a refrain or the verse refrain(); verse(); refrain(); } }