CodeYard: Learning to Program in Java
Sort by: Curriculum unit & section · Just problems, by category
Show: Everything · Lessons · Worksheets · Problems
- Introduction to Algorithms 1.1 Worksheet 🌶️ An algorithm is an ordered series of steps to solve a problem. Let's say that you are …
- Fix the missing semicolon Java Basics 1.1 🌶️ The program below is supposed to print "Hello, World!" but it has a bug! Try running it e…
- Build the line: declare and initialize temperature 1.1 Worksheet 🌶️ Click the tokens below, in order, to build a line that declares an integer variable named…
- Fix the wrong data type Java Basics 1.2 🌶️ The program below is supposed to track whether it's raining and print that value. It does…
- Declare a student record Java Basics 1.2 🌶️ A student is taking 6 courses, has a GPA of 3.5, is currently enrolled, and is named Mari…
- Fahrenheit to Celsius Java Basics 1.2 🌶️ The program below is supposed to convert a Fahrenheit temperature to Celsius using the fo…
- Print a phrase with quote marks Java Basics 1.3 🌶️ Write code in the box so that the program prints the exact phrase: She said "Hello!" to m…
- Print two lines with one statement Java Basics 1.3 🌶️ Write code in the box that prints these two lines, using a single System.out.println stat…
- Rectangle perimeter and area Java Basics 1.3 🌶️ The program below gives you a rectangle's width and length, and its last two lines (locke…
- Seconds to minutes and seconds Java Basics 1.3 🌶️🌶️ The variable totalSeconds holds a length of time in seconds. The three locked lines at th…
- Split a two-digit number Java Basics 1.3 🌶️ The variable number holds a two-digit whole number. The two locked lines at the bottom pr…
- Line trace: swapping two values with temp 1.3 Worksheet 🌶️ Below is a method that swaps two values using a temp variable. Trace a, b, and temp immed…
- Swap two values Java Basics 1.4 🌶️🌶️ The program below gives first and second their starting values, and its last two lines (l…
- Update an account balance Java Basics 1.4 🌶️ The variable balance holds the amount in an account, deposit is a deposit being made, and…
- Rotate three values Java Basics 1.4 🌶️🌶️🌶️ The int variables first, second and third have starting values, and the three locked line…
- Expression and Assignment Tracing 1.4 Worksheet 🌶️🌶️ Practice for evaluating expressions and tracing what each assignment statement actually d…
- Whole dollars Java Basics 1.5 🌶️ The program below is supposed to figure out how many whole dollars are in a price, then p…
- Round to the nearest whole number Java Basics 1.5 🌶️🌶️ The variable temperature holds a positive temperature with a decimal part, like 71.6. The…
- Round a negative number Java Basics 1.5 🌶️🌶️ change holds a negative decimal number, like -3.6. The locked line at the bottom prints a…
- printAverage Java Basics 1.5 🌶️ The method below is supposed to calculate the average number of points scored per game an…
- Average of four test scores Java Basics 1.5 🌶️🌶️ test1, test2, test3 and test4 are four whole-number test scores. The locked line at the b…
- Cash drawer Java Basics 1.5 🌶️🌶️🌶️ A cash drawer has to make change. `cents` holds an amount of money in cents (41 means 41 …
- Score updates Java Basics 1.6 🌶️ score holds a player's starting points, and the locked line at the bottom prints the fina…
- Point2D: Quick Reference 1.7 Lesson 🌶️ Some problems ask your method to accept a `Point2D` parameter, or an `ArrayList<Point2D>`…
- Student: Quick Reference 1.7 Lesson 🌶️ Some problems ask your method to accept a `Student` parameter, or an `ArrayList<Student>`…
- Method Signatures and Calling Methods 1.9 Worksheet 🌶️🌶️ A few key ideas this worksheet checks: - Procedural abstraction: you can use a method …
- Predict the Output: Calling Methods 1.9 Worksheet 🌶️🌶️ Before you run any of this code, work it out by hand. Every question here is about what a…
- Method Vocabulary and Contracts 1.9 Worksheet 🌶️ A library, a package, and an API documentation page are three different things — and a Ja…
- announcePurchase Java Basics 1.9 🌶️ Write a void method named announcePurchase that takes two parameters: a String named item…
- printTotalCost Java Basics 1.9 🌶️ Write a void method named printTotalCost that takes three parameters: a String named item…
- sumTwo Java Basics 1.9 🌶️ Write a method that takes two int parameters, a and b, and returns their sum.
- printTemperatureReport Java Basics 1.9 🌶️ Write a void method named printTemperatureReport that takes one int parameter, celsius. U…
- weightedSum Java Basics 1.9 🌶️ Write a method that takes two int parameters, a and b, and returns a plus five times b. (…
- averageThreeInts Java Basics 1.9 🌶️🌶️ Write a method that takes three int parameters, a, b, and c, and returns their average as…
- roundPositive Java Basics 1.9 🌶️🌶️ Write a method that takes one positive double parameter, value, and returns it rounded to…
- totalWithTip Java Basics 1.9 🌶️🌶️ Write a method that takes two int parameters, billCents (a bill amount, in cents) and tip…
- Scoreboard.reportBonus Java Basics 1.9 🌶️ Below is a partially written Scoreboard class. It already has a method, bonusFor, that ad…
- WeatherDesk.describeTemperature Java Basics 1.9 🌶️🌶️ Below is a partially written WeatherDesk class. It already has two overloaded versions of…
- BonusDesk.comboBonusReport Java Basics 1.9 🌶️🌶️🌶️ Below is a partially written BonusDesk class. It already has two methods: addAmount(n, am…
- Method Calls in Expressions 1.9 Worksheet 🌶️🌶️ A method call inside a larger expression runs first, and its return value takes its place…
- Unit 1 Review: Tough Expressions 1.9 Worksheet 🌶️🌶️ The hardest kind of Unit 1 multiple-choice question: an expression that mixes operators, …
- The Math Class: random() 1.11 Worksheet 🌶️🌶️ `Math.random()` returns a double value that is always greater than or equal to 0.0, and a…
- circleArea Java Basics 1.11 🌶️ Write a method that takes one double parameter, radius, and returns the area of a circle …
- hypotenuseLength Java Basics 1.11 🌶️ Write a method that takes the lengths of the two legs of a right triangle, legA and legB,…
- absoluteDifference Java Basics 1.11 🌶️ Write a method that takes two double parameters, a and b, and returns the absolute differ…
- largerRoot Java Basics 1.11 🌶️🌶️ Write a method that takes the three coefficients of a quadratic equation, a, b, and c (re…
- compoundInterest Java Basics 1.11 🌶️🌶️ Write a method that takes a double parameter principal (a starting amount of money), a do…
- distanceFromOrigin Geometry & Shapes 1.11 🌶️ Write a method that takes one Point2D parameter, p, and returns its distance from the ori…
- Fix cubeVolume Java Basics 1.11 🌶️ The method below is supposed to return the volume of a cube with the side length `side`. …
- Unit 1 Review: Methods, Objects, and null 1.14 Worksheet 🌶️🌶️ A review for the end of Unit 1: how a method call is chosen and what it returns, how `==`…
- String Objects, Concatenation, and Indexes 1.15 Worksheet 🌶️ A **String** in Java is not a primitive type like `int` or `double`, it is an **object**,…
- tripleString Strings (no loops) 1.15 🌶️ Write a method that takes one String parameter, s, and returns a new String containing s …
- substring: Bounds and Overloads 1.15 Worksheet 🌶️🌶️ `substring` is the method you will reach for constantly, and it is also the easiest one t…
- initials Strings (no loops) 1.15 🌶️ Write a method that takes two String parameters, first and last (a person's first and las…
- lastLetterTwice Strings (no loops) 1.15 🌶️ Write a method that takes one String parameter, s, and returns a String containing just t…
- middleCharacter Strings (no loops) 1.15 🌶️ Write a method that takes one String parameter, s, and returns the single character in th…
- removeCharAt Strings (no loops) 1.15 🌶️🌶️ Write a method that takes a String parameter s and an int parameter index, and returns a …
- Find the Index, Then Cut 1.15 Worksheet 🌶️🌶️ Nearly every String task on the AP exam has the same shape: **find** where something is, …
- firstWord Strings (no loops) 1.15 🌶️ Write a method that takes one String parameter, s, and returns everything in s up to (but…
- textAfter Strings (no loops) 1.15 🌶️🌶️ Write a method that takes two String parameters, text and target, and returns everything …
- insideParentheses Strings (no loops) 1.15 🌶️🌶️ Write a method that takes one String parameter, text, and returns the part of text betwee…
- swapEnds Strings (no loops) 1.15 🌶️🌶️ Write a method that takes one String parameter, s, and returns a new String with the firs…
- withNeighbors Strings (no loops) 1.15 🌶️🌶️ Write a method that takes two String parameters, text and target. Find where target first…
- removeFirst Strings (no loops) 1.15 🌶️🌶️ Write a method that takes two String parameters, text and target, and returns text with t…
- replaceOnce Strings (no loops) 1.15 🌶️🌶️ Write a method that takes three String parameters, text, target and replacement, and retu…
- insertAfter Strings (no loops) 1.15 🌶️🌶️ Write a method that takes three String parameters, text, target and addition, and returns…
- lastNameFirst Strings (no loops) 1.15 🌶️🌶️ Write a method that takes one String parameter, fullName, made of a first name, exactly o…
- secondOccurrenceIndex Strings (no loops) 1.15 🌶️🌶️🌶️ Write a method that takes two String parameters, text and target, and returns the index i…
- renameExtension Strings (no loops) 1.15 🌶️🌶️ Write a method that takes two String parameters: filename, a file name with exactly one d…
- reformatDate Strings (no loops) 1.15 🌶️🌶️🌶️ A date is written as month/day/year, with slashes and no leading zeros, like "3/14/2024" …
- Comparing Strings: compareTo 1.15 Worksheet 🌶️🌶️ `equals` answers a yes-or-no question: do these two Strings hold the same characters? `co…
- comesBefore Strings (no loops) 1.15 🌶️ Write a method that takes two String parameters, a and b, and returns true if a comes bef…
- maskAccountNumber Strings (no loops) 1.15 🌶️🌶️🌶️ Extra Write a method that takes one String parameter, number, and returns it with every charact…
- extractDomain Strings (no loops) 1.15 🌶️🌶️🌶️ Extra Write a method that takes one String parameter, url, and returns just its domain. A url a…
- Unit 1 Review: What Do These String Calls Return? 1.15 Worksheet 🌶️🌶️ A review for the end of Unit 1: the String methods that the quiz and the test keep asking…
- Fix trimEnds Strings (no loops) 1.15 🌶️🌶️ The method below is supposed to return `s` without its first and last character, so `"(he…
- Fix host Strings (no loops) 1.15 🌶️ The method below is supposed to return the part of an email address AFTER the `@`, so `"a…
- Fix isAdmin Strings (no loops) 1.15 🌶️🌶️ The method below is supposed to return true if `user` is exactly `"root"` and false other…
- Fix comesFirst Strings (no loops) 1.15 🌶️🌶️ The method below is supposed to return true if the String `a` comes before the String `b`…
- isVowel Strings (no loops) 1.15 🌶️ Write a method `isVowel` that returns `true` if the one-letter lowercase String `letter` …
- Exam Free Response 1 Practice: RosterTools.initials Objects & Classes 1.17 🌶️🌶️ This is practice in the shape of AP Exam Free Response Question 1 (Methods and Control St…
- Find the Line: Errors in Unit 1 Code 1.17 Worksheet 🌶️🌶️ Reading an error is a skill of its own. Each program below has exactly one thing wrong wi…
- Exam Free Response 1 Practice: RosterTools.formatRosterLine Objects & Classes 1.17 🌶️🌶️🌶️ Continuing the RosterTools class from the previous problem. You may assume initials works…
- largerAbsoluteValue Conditionals 2.2 🌶️ Write a method that takes two int parameters, a and b, and returns true if a's absolute v…
- canAfford Conditionals 2.2 🌶️ Write a method that takes two double parameters, price and budget, and returns true if th…
- containsText Strings (no loops) 2.2 🌶️ Write a method that takes two String parameters, text and target, and returns true if tar…
- isAdult Conditionals 2.2 🌶️ The method below is supposed to return true if age is 18 or older, and false otherwise; i…
- Nested if Statements and Multiway Selection 2.4 Worksheet 🌶️🌶️ An if statement can contain another if statement inside its own body — this is called nes…
- letterGrade Conditionals 2.4 🌶️🌶️ Write a method that takes one int parameter, score, and returns a letter grade as a Strin…
- Boolean Operators: Quick Reference 2.5 Worksheet 🌶️ This page reviews the three boolean operators you'll use constantly in conditionals, then…
- Truth Tables and Venn Diagrams 2.5 Worksheet 🌶️ Two ways to see exactly when a compound boolean expression is true: a **truth table** lis…
- Compound Boolean Expressions: Precedence and Short-Circuit Evaluation 2.5 Worksheet 🌶️🌶️ Quick recap from the boolean-operators reference: `!` (not), `&&` (and), and `||` (or) co…
- isInRange Conditionals 2.5 🌶️ Write a method that takes three int parameters, value, min, and max, and returns true if …
- isLeapYear Conditionals 2.5 🌶️🌶️ Write a method that takes one int parameter, year, and returns true if it is a leap year,…
- isValidTriangle Conditionals 2.5 🌶️🌶️ Write a method that takes three int parameters, sideA, sideB, and sideC, representing thr…
- beginsWith Strings (no loops) 2.5 🌶️🌶️ Write a method that takes two String parameters, text and prefix, and returns true if tex…
- Fix isTeen Conditionals 2.5 🌶️ The method below is supposed to return true if `age` is from 13 to 19 inclusive, and fals…
- Exam Free Response 1 Practice: GeoTools.quadrantLabel Objects & Classes 2.5 🌶️🌶️ This is practice in the shape of AP Exam Free Response Question 1 (Methods and Control St…
- Exam Free Response 1 Practice: GeoTools.describeRoute Objects & Classes 2.5 🌶️🌶️ Continuing the GeoTools class from the previous problem. You may assume quadrantLabel wor…
- classifyPoint Objects & Classes 2.5 🌶️🌶️ Write a method that takes a Point2D parameter, p, and an int parameter, scale, and return…
- standing Objects & Classes 2.5 🌶️🌶️ Write a method that takes a Student parameter, s, and an int parameter, minAttendance, an…
- Fix fizzBuzzWord (Error 1) Conditionals 2.5 🌶️🌶️ The method below is supposed to return the FizzBuzz word for the positive int `n`: `"Fizz…
- Equivalent Boolean Expressions 2.6 Worksheet 🌶️ Two boolean expressions are **equivalent** when they produce the exact same result for ev…
- Fix lengthOrZero Objects & Classes 2.6 🌶️ The method below is supposed to return the length of the String `s`, or 0 if `s` is `null…
- Unit 2 Review: Conditions and Branches 2.6 Worksheet 🌶️🌶️ A review for the end of Unit 2: boolean operators and their precedence, short-circuit eva…
- sameOrBothMissing Objects & Classes 2.6 🌶️🌶️ Write a method `sameOrBothMissing` that takes two `Money` objects, **either of which may …
- DNA: Bases and Codons Lab 2.6 🌶️🌶️ Write fourteen small methods in **Bases.java** that treat DNA the way a cell does: one **…
- Chess: Steps and Rules Lab 2.6 🌶️🌶️ Write six methods in **ChessRules.java** that decide whether a chess move is legal **on a…
- weeksToPayOff One Loop 2.7 🌶️ You owe `balance` dollars on a loan and pay `weekly` dollars each week (a positive whole …
- nextBusMinute One Loop 2.7 🌶️ A bus leaves the stop at minute `every`, then at `2 * every`, `3 * every`, and so on. You…
- daysToFillJar One Loop 2.7 🌶️🌶️ You are filling a jar with coins to reach a `goal`. On day 1 you add 1 coin, on day 2 you…
- Fix sumOddsUpTo One Loop 2.7 🌶️ The method below is supposed to return the sum of the odd numbers from 1 up to and includ…
- nextDeparture Objects & Classes 2.7 🌶️🌶️ Buses leave a stop at the `first` time and then every `every` minutes (1 or more). You ge…
- pennyChallengeDays Objects & Classes 2.7 🌶️🌶️🌶️ In a savings challenge you put `start` into a jar on day 1. After that, each day you put …
- booksToOverhang Objects & Classes 2.7 🌶️🌶️🌶️ A stack of identical books sits on the edge of a table. In the best way to stack them, th…
- caesarShiftString One Loop 2.7 🌶️🌶️🌶️ Extra Write a method that takes one String parameter, s, and one int parameter, shift, and retu…
- printSquares One Loop 2.8 Given n, print the square of each integer from 1 to n, one per line, using System.out.pri…
- Trace: palindrome check indices 2.8 Worksheet 🌶️🌶️ Below is the loop from a palindrome checker for the String s = "level" (length 5). Trace …
- repeatText One Loop 2.8 🌶️ Your group chat needs a laugh. Write a method `repeatText` that takes a String `s` and an…
- launchCountdown One Loop 2.8 🌶️ A rocket launch counts down out loud. Write a method `launchCountdown` that takes an int …
- readathonPages One Loop 2.8 🌶️ In a read-a-thon you read as many pages as the day number: 1 page on day 1, 2 pages on da…
- sumOfEvens One Loop 2.8 🌶️ Write a method `sumOfEvens` that takes an int `n` and returns the sum of the even numbers…
- Fix addUpTo One Loop 2.8 🌶️ The method below is supposed to return the sum of the ints from 1 up to and including `n`…
- factorial One Loop 2.8 🌶️ Write a method `factorial` that takes an int `n` (0 to 12) and returns `n!`: the product …
- countMultiplesInRange One Loop 2.8 🌶️🌶️ Paydays come every `d` days, on day numbers that are multiples of `d` (including day 0). …
- printTimesTable One Loop 2.8 🌶️ Write a method `printTimesTable` that takes an int `n` and prints the first ten lines of …
- isPrime One Loop 2.9 🌶️🌶️ Write a method that takes one int parameter, n, and returns true if it's a prime number, …
- countDigits One Loop 2.9 🌶️ Write a method that takes one int parameter, n, and returns how many digits it has. You c…
- sumDigits One Loop 2.9 🌶️🌶️ Given a non-negative int n, return the sum of its digits. sumDigits(126) returns 9 (1+2+6…
- isArmstrongNumber One Loop 2.9 🌶️🌶️🌶️ An Armstrong number is one where, if you raise each of its digits to the power of the TOT…
- isBalancedParentheses One Loop 2.9 🌶️🌶️ Write a method that takes one String parameter, s, made up only of the characters '(' and…
- pingPongPosition One Loop 2.9 🌶️🌶️ A scanner light sweeps back and forth along a strip of lights numbered `0` to `len`. It s…
- isLuhnValid One Loop 2.9 🌶️🌶️🌶️ The Luhn algorithm is a checksum used to catch typos in card numbers. Write a method that…
- binaryToDecimal One Loop 2.9 🌶️🌶️ A row of light switches is either on (`"1"`) or off (`"0"`). Read from left to right, the…
- countCapitals Strings (no loops) 2.10 🌶️ Write a method `countCapitals` that takes a String `s` and returns how many of its charac…
- reverseString One Loop 2.10 🌶️ Write a method that takes one String parameter, s, and returns a new String with its char…
- longestRun Strings (no loops) 2.10 🌶️🌶️ A **run** is a stretch of the same character repeated in a row. Write a method `longestRu…
- runLengthEncode One Loop 2.10 🌶️🌶️🌶️ Extra Run-length encoding compresses a string by replacing each run of consecutive identical ch…
- Algorithm Shapes: The Sliding Window 2.10 Worksheet 🌶️🌶️🌶️ Challenge Every algorithm you have seen so far either walks a string/array once (one loop) or check…
- printRightTriangle One Loop 2.11 🌶️ Write a method that takes one int parameter, n, and prints a right triangle of asterisks …
- printRectangle One Loop 2.11 🌶️ A sign painter marks out a rectangle with `#` characters. Write a method `printRectangle`…
- printNumberTriangle One Loop 2.11 🌶️ Write a method `printNumberTriangle` that takes an int `n` (from 1 to 9) and prints `n` l…
- countBlueTiles One Loop 2.11 🌶️ A gym floor is tiled in `rows` rows of `cols` tiles. The rows and columns are numbered fr…
- handshakes One Loop 2.11 🌶️🌶️ At a meeting every person shakes hands once with every other person. Write a method `hand…
- printPyramid One Loop 2.11 🌶️🌶️ Write a method that takes one int parameter, n, and prints a centered pyramid of asterisk…
- countBalancedTimes One Loop 2.11 🌶️🌶️ A digital clock shows the hour and the minute, like 7:05 or 12:21. Call a time **balanced…
- countBookendSubstrings One Loop 2.11 🌶️🌶️🌶️ A substring is a **bookend** when its first and last characters are the same letter. A si…
- hasDuplicateLetter Strings (no loops) 2.11 🌶️🌶️ Write a method `hasDuplicateLetter` that takes a String `s` and returns `true` if some ch…
- pythagoreanTriples One Loop 2.11 🌶️🌶️🌶️ A **Pythagorean triple** is three positive whole numbers `a < b < c` with `a * a + b * b …
- printCheckerboard One Loop 2.11 🌶️🌶️ Write a method `printCheckerboard` that takes an int `n` and prints an `n` by `n` checker…
- Vector2D.Magnitude Objects & Classes 3.5 🌶️ Below is the definition of a Vector2D class, representing a two dimensional vector - an …
- BankAccount.deposit Objects & Classes 3.5 Below is a partially written BankAccount class. It already has a balance field and a cons…
- rotateArrayByN One Loop 4.5 🌶️🌶️ Write a method that takes an int array, nums, and an int, n, and returns a new int array …
- sumFile Files & I/O 4.6 Given a File containing one integer per line, construct your own Scanner over it and retu…
- populateArrayFromFile Files & I/O 4.6 🌶️🌶️ A file's first line is an integer n, the number of values that follow after it. Each int …
- populateListFromFile Files & I/O 4.6 🌶️🌶️ Write a method that takes a File, constructs a Scanner over it, and returns an ArrayList<…
- fileContainsValue Files & I/O 4.6 🌶️🌶️ Write a method that takes a File (one int per line) and a target int, constructs a Scanne…
- countWords Files & I/O 4.6 Extra Given an open BufferedReader over a text file, return the total number of whitespace-sepa…
- capitalizeAll One Loop 4.9 Given an ArrayList<String> of words, return a new ArrayList<String> containing each word …
- honorRoll One Loop 4.10 🌶️🌶️ Write a method that takes an ArrayList<Student> parameter, students, and returns an Array…
- transpose 2D Arrays 4.13 🌶️🌶️🌶️ Extra Given a rectangular 2D array grid, return its transpose: a new 2D array where rows and co…