Class prep · TJ Foundations of Computer Science

Understand the program,then make it work.

An independent beginner course in computer science. Learn to read, run and repair Java programs, then explain their behavior using precise inputs, traces and tests. Every problem has a full worked solution. Built by Neil Kothari, a TJ alumnus.

$395 covers the full school year. The first module is free.

Try one. This is a test-level question from the course.

Problem 1

  • Test level
  • Multiple choice
  • No calculator
  • 4 pts

Allowed inputs are nonempty uppercase words. The required output is the entire input enclosed in square brackets. Which rule satisfies every allowed input?

Choose one answer

original problems, each with a worked answer
624
original problems, each with a worked answer
at test level or beyond
432
at test level or beyond
short lessons, one for every topic
48
short lessons, one for every topic
units, all open now
8
units, all open now

Free lesson

Lesson 1.1: Instructions, Algorithms and Observable Behavior

Learn to state exactly what result a task needs, write steps a machine can follow without guessing, and test whether those steps really work.

Start with a clear specification. Identify the inputs, outputs and allowed actions, then trace a procedure to check whether it meets the goal.

Start with a question

Your little brother is supposed to feed the cat while you are away. You text him: "Feed the cat."

An hour later he texts back: "Done." Did he do it right?

You cannot tell. Which food? How much? Into which bowl? Did he close the bag afterward? "Feed the cat" names a goal, but it does not say what a correct result looks like, and it does not give steps he could follow without guessing.

A computer is a helper that never guesses. It does exactly the steps it is given, in order, using only the actions it knows. So before you write a single line of Java, you need two skills:

  • saying exactly what result counts as correct, and
  • writing steps so precise that nobody, and no machine, has to guess.

By the end of this lesson you will be able to:

  • tell a specification (what result is required) apart from an algorithm (the exact steps),
  • name a task's inputs and outputs and write a specification complete enough to check, including order and spacing,
  • trace the state of a task step by step and check each precondition at the moment an action happens,
  • build procedures from only the operations you are given, such as swapping two items using a spare spot,
  • explain why one passing test proves very little, while one failing allowed case proves an algorithm wrong.

Programs, specifications and algorithms

A program is a set of instructions written in a particular language and run in a particular environment. In this course the language is Java. Before the language matters, though, two ideas matter more.

A specification (or "spec") says what must be true when the task is done, and under what starting conditions. It describes the result, not the steps.

An algorithm is a precise, step-by-step procedure that produces the result. It says how.

SpecificationAlgorithm
Answers the questionWhat counts as a correct result?What exact steps produce it?
Example"The 5 returned books end up on shelf 2, spines facing out, in call-number order.""1. Pick up the leftmost book on the cart. 2. ..."
You check it byLooking at the finished resultFollowing the steps exactly
How many versions?One required resultOften many different algorithms work

Every task has inputs, the things you are given at the start, and outputs, the things the task must produce. An output does not have to be printed text. If the task is "arrange the chairs in a circle," the arranged chairs are the output.

The line between what the task receives and what it must deliver is the task boundary. Anything outside the boundary, such as how the inputs got there or what happens to the output later, is not part of the job.

Inputs cross the task boundary, the algorithm works inside it, and the output crosses back out. The specification describes only what crosses the boundary.

What a complete specification must say

A spec is complete when two careful people checking the same result would always agree on whether it is correct. That usually means the spec names:

  1. The pieces. Which items, words or numbers appear in the result.
  2. The arrangement. Order, spacing, separators, line breaks and orientation (which way up, which way facing).
  3. The closing conditions. What must be true at the end: lid closed, hand empty, spare spot empty again.
  4. What must not change. For example, "no token may be lost or created."

The difference between pieces and arrangement trips up many students. "ant zoo" and "zoo ant" contain the same pieces in a different arrangement. If the spec says "the first word, one space, then the second word," only one of them is correct. The same goes for line order: three lines that contain all the right words in the wrong order fail a spec that names the order.

Worked example 1: turning a vague request into a specification

A teacher asks for a program that will "make a seat tag for a student." Today's student has the last name Okafor and seat number 12.

  1. Find the inputs. There are two: a last name (Okafor) and a seat number (12).
  2. Find the output. One line of text, the tag.
  3. Name the pieces. The tag must contain the name and the number. That alone is not enough, because "12 Okafor," "Okafor12" and "Okafor / 12" all contain both pieces, and they are different outputs.
  4. Name the arrangement. Choose one rule and state it: the name, then one space, then the capital letter S, then the seat number. For these inputs the tag is Okafor S12.
  5. Write the rule for every allowed input, not just this one. For any last name with no spaces and any seat number from 1 through 40, output the name, one space, the capital letter S, then the seat number in digits, and nothing else.

Check: hand the spec to two friends with a new input, say Lin and seat 7. Both should write Lin S7 and nothing else. If either friend has to ask "which comes first?" or "is there a space?", the spec is not finished.

The trap: writing the spec for only the example you were shown, such as "output Okafor S12." That is not a rule. It is wrong for every other student.

Quick check 1. A spec says: "Output the city and the temperature." For the inputs Denver and 18, write two different outputs that would both satisfy this spec. Then add one sentence that makes the spec complete.

Problem 2

  • Core
  • True or false
  • No calculator
  • 2 pts

A packing procedure says, "Take one card from the left tray and put it in the envelope." The left tray is empty. Claim: a successful execution requires an available card, so the stated initial tray does not meet that requirement. Judge the claim and explain the missing precondition.

Is the statement true or false?

Problem 3

  • Core
  • Multiple choice
  • No calculator
  • 4 pts

A machine receives exactly one supplied digit, here 4. Which statement specifies the required observable output rather than an internal implementation step?

Choose one answer

Open the rest of this lesson, free

The full lesson and all 13 of its problems, with every worked answer.

I am aI am a

Your free lesson opens right here, on this device for 30 days. We'll email you occasional TJ class prep updates. Unsubscribe anytime.

How it works

Class prep that works alongside class

A complete beginner course in computer science with no prior programming required. Work alongside your school course or study the material in sequence. Each lesson starts with a concrete example, develops the idea, and shows how to check your reasoning before you try the practice.

  • Original problems in several formats: true or false, multiple choice, multi-part and free response.
  • Each one is marked core, test level or beyond.
  • Trace program state, write small Java programs and use clear tests to explain and repair their behavior.
  • A worked answer for every problem, ending with the mistake that most often costs points.
  • Java values, decisions, loops, classes, arrays, files and supplied graphical interfaces, with original diagrams and worked reasoning.

What is inside

  1. 1Programs, values and the Java workflow6 lessons · 78 problems
  2. 2Decisions and repetition6 lessons · 78 problems
  3. 3Methods and text processing6 lessons · 78 problems
  4. 4Designing classes and collaborating objects6 lessons · 78 problems
  5. 5Arrays and structured collections of values6 lessons · 78 problems
  6. 6Console input, text files and reliable data flow6 lessons · 78 problems
  7. 7Graphics and event-driven programs6 lessons · 78 problems
  8. 8Algorithms, representation and responsible program design6 lessons · 78 problems
The front entrance of Thomas Jefferson High School for Science and Technology
Neil Kothari, co-founder of EduAvenues

Who built it

Neil Kothari

TJHSST alumnus · Co-founder, EduAvenues · 10+ years in TJ preparation

Neil is a TJ alumnus and has spent more than a decade working with TJ students and their families. He built this course so students can connect precise specifications, program state, code and tests. Every problem is written for the course and every answer is checked before it goes in.

Tuition

$395for the full school year

One tuition covers every unit of the course. All 8 units are open now. The Instructions, Algorithms and Observable Behavior lesson stays free for everyone.

  • All 8 units: 48 lessons and 624 original problems
  • A worked answer for every problem
  • Access through the end of the school year
Enroll