CPSC 124 Introduction to Programming Spring 2024

Lab 7
Subroutines and Functions

Due: Tue 3/26 at the start of lab


Introduction

Subroutines are powerful organizational tools. Part of their power comes from reuse — you can write a subroutine once, and then use it many times (in the same program or in new programs). Even more of their power comes from being able to use subroutines like building blocks to progressively create more complex things. Functions allow the subroutines to compute a value that is then used by the rest of the program, increasing the utility of subroutines even more.

Collaboration

Labs and projects are for practice and learning. While it can be very productive to work on problems with your peers, it is also easy to underestimate how much you yourself understand and can do in such situations — so often something looks easy when someone else does it! With this in mind, you should always make the first attempt on a problem or programming task yourself using only the resources provided for the course (textbook, slides and examples posted on the schedule page, other resources linked on the course webpages). After that point, you are encouraged to come to office hours and/or go to Teaching Fellows. You may not collaboratively write solutions or code, and you may not copy solutions or code written by others, even if you contributed ideas.

You can discuss specific exercises with other students in general terms — such as how you might get started on that problem, or how or when to use various programming constructs, or strategies for debugging — but how to use a particular programming construct to solve a specific problem or debugging a particular program should only be discussed in office hours or with the Teaching Fellows.


Setup


Exercises

For all exercises:

Utilize incremental development — implement small pieces (such as a subroutine or function) one-by-one, testing after each. One strategy is to start with the subroutines/functions, writing code in main to test each, before then writing the actual main program.


Exercise #1

We have now seen enough to understand what the following means:

  public static void main ( String[] args ) {
  }

This is the declaration for a subroutine called main which takes an array of Strings and a parameter and does not return anything.

Then:


Exercise #2

Your program must contain the following elements:

Make use of the various subroutines and functions where you can — printHistogram should use printHashes to print the hashes for each slot of the array and the main program should use generateStats and printHistogram.

Hint: The task performed by generateStats is very similar to the task performed by the DiceCounter program from class, except that generateStats should be able to work with dice with any number of sides (not just 6) and should return the counts array instead of printing the frequencies.


Exercise #3

As a kid, you might have used Pig Latin to be silly or to carry on secret conversations. In Pig Latin, each word is replaced by a transformed word intended to be nonsensical to those who don't know Pig Latin. There are some variations of Pig Latin, but here's the version we will use:

Your program must contain the following elements:

Call the various subroutines and functions as appropriate: findFirstVowel should use isVowel to determine if a particular character is a vowel or not, pigifyWord should use findFirstVowel to find the position of the first vowel in the word so you can determine how to pigify it, and main should use pigifyWord to convert each word to Pig Latin.

Hints:


Handin and Grading

Don't forget to hand in your work when you are done!

As in lab 2 (and all labs), grading will be based on both functionality and style. Style means following the good programming style conventions outlined in lab 2, as well as producing reasonably compact and elegant solutions. "Reasonably compact and elegant" means that your program is not significantly longer or more complex than it needs to be. In particular, you must use loops when loops are appropriate — achieving repetition by copying-and-pasting a bunch of code will not earn credit.