Skip to main content
← Advanced CS Study Guide

Code Scholars Tutoring · Advanced CS

0.1 Design Before Implementation

How it works

Deciding responsibilities and interfaces before writing code is what makes a program changeable later. Design is a separate activity from coding, not a preamble to it.

Sketch the classes, their responsibilities, and how they interact before opening an editor.

Common error

Starting to type immediately, which produces code that works and cannot be modified.

Worked example

Example

Input: nonempty list of scores
Output: largest score
Rule: do not modify the input
Cases: one item, duplicates, negative values

A contract states the accepted inputs, returned result, and permitted side effects before code is selected. A maximum should begin with an actual element, not zero, because every score may be negative.

Exercises

0.1.1 · Explain

A maximum finder starts best at 0. Give a failing input and explain a correct initialization.

Show solution

For [-8, -3], it incorrectly returns 0. For a nonempty input, initialize best to the first element, then compare the remaining values.

0.1.2 · Write code

Implement and demonstrate a maximum scan for the nonempty array {-8, -3, -10} without changing it.

Show solution

Initialize from the input and maintain the largest value seen.

One valid solution

int[] scores = {-8, -3, -10};
int best = scores[0];
for (int value : scores) if (value > best) best = value;
System.out.println(best);

Expected output

-3
Open Java compiler

Need help with this topic?

Work through questions with a tutor and get help with the steps you find difficult.

Get help with this topic