Computer Science 268: Introduction to Computer Programming (Java)

Study Guide

Unit 4

Read Chapter 4 of the textbook: Programming in the Large I: Subroutines

This chapter introduces the concept of breaking up a program into manageable pieces to improve code-writing efficiency and enhance program design. The chapter focuses on what the textbook calls subroutines, which is a common term in many programming languages. However, in object-oriented programming languages such as Java, the correct term for these collections of code is method. Consider this as you read the following sections.

Section 4.1 Black Boxes

This section discusses subroutines (more correctly, methods) in the context of viewing them as “black boxes” that perform actions based on inputs. In this view, the mechanism or inner workings of the method is not as important as the function it provides.

This section provides background to subsequent sections. Read and review it carefully.

Section 4.2 Static Subroutines and Static Variables

This section discusses the role of the subroutine (correct term is method) in Java, as well as the static subroutine and static variables, which are consequences of the way Java has been designed. The section also discusses how to call a subroutine (method) and how subroutines (methods) can be used in programs. Finally, member variables are introduced and discussed.

Read and review this section carefully.

Section 4.3 Parameters

This section discusses how to send information into a method using parameters. The concept of overloading as it pertains to subroutines (methods) is covered, followed by an example. Parameters using arrays and command-line arguments are discussed next. The section covers throwing exceptions in subroutines (methods) and ends with a discussion of global and local variables.

Read and review this section carefully.

Section 4.4 Return Values

This section covers the retrieval of output from a method using the return statement. Some examples are explored and, finally, the 3N+1 problem is revisited.

Read and review this section carefully.

Section 4.5 Lambda Expressions

This section is an introduction to lambda functions. These are fairly new to Java, introduced in Java 8. Lambda functions offer some very nice programming features, but keep in mind that this topic is more suited to advanced programming courses. Lambda functions are beyond the scope of this course.

Section 4.6 APIs, Packages, Modules, and Javadoc

This section covers some important topics in the course. First, the notion of an Application Programming Interface (API) is discussed as a type of toolbox that you can use in your programs. From there, the Java standard packages are introduced and discussed, followed by an explanation of how to use these standard packages in your own programs. The section ends with a discussion of the Java code documentation package, Javadoc, and closes by mentioning static import.

Read and review sections 4.6.1 through 4.6.5 carefully. Section 4.6.6 is beyond the scope of this course.

Section 4.7 More on Program Design

This section covers the important topic of program design, and includes a discussion of pre- and postconditions with respect to subroutine (method) design. The next two sections discuss an example of graphical user interface (GUI), resulting in a final program that is beyond the scope of this course.

Read and review section 4.7.1 carefully. Sections 4.7.2 and 4.7.3 are beyond the scope of this course.

Section 4.8 The Truth About Declarations

This section covers some essential information regarding declaration and initialization of variables in Java programs. Also covered are named constants and a discussion of a variable’s scope—that is, where a variable can be seen and used in a program.

Read and review this section carefully.

Programming Exercises

It is a good practice to attempt the programming exercises in the textbook, to put the topics you have learned into concrete Java code. This, in turn, allows you to reflect on the material you have read. Skip any exercises relating to topics in Chapter 4 that are not covered in the course.

Resist the temptation to download the programming exercises from the textbook and simply run them. The greatest benefit in learning to program in a new language occurs when you type all the example code yourself.

Typing the code, even for simple examples, engages your brain in the process of writing code, compiling code, running code, and diagnosing and resolving problems that may arise. It is the latter skill—problem solving—that can become the most powerful tool in learning to program.

Quiz

It is a good practice to review the chapter quizzes in the textbook, as the questions allow you to reflect on the material you have read.