Skip to main content

Question 1: Methods and Control Structures

Network outages and hidden email addresses

  • Units 1 and 2
  • 7 points
  • About 25 minutes

You write two methods, or a constructor and a method, of a class you're given. Part A needs loops and/or if statements plus calls to the class's own methods; Part B needs String methods. New since the course was revised for 2025–26 (first tested in May 2026): it's worth 7 points instead of 9, and points are no longer taken off as penalties. On the exam: Question 1 of 4, 7 points (Part A 4 points, Part B 3 points). Section II has 4 free-response questions in 90 minutes (45% of the score), all assessing Practice 2 (Develop Code). The exam is fully digital in Bluebook: you type your Java, and the Java Quick Reference is provided. No calculator. Suggested time is our split of the 90 minutes by points; College Board gives no per-question timing.

The question and its sources

A school's network monitor checks once a minute whether the internet connection is working. The minutes are numbered 0 through getMinutes() - 1. You will write two methods of the NetworkMonitor class.

The NetworkMonitor class

Java
public class NetworkMonitor
{
    /** Returns the number of minutes that were checked. */
    public int getMinutes()
    { /* implementation not shown */ }
 
    /**
     * Returns true if the connection was working during minute m;
     * false otherwise.
     * Precondition: 0 <= m < getMinutes()
     */
    public boolean isOnline(int m)
    { /* implementation not shown */ }
 
    /** Described in part (a) */
    public int countOutages(int minLength)
    { /* to be implemented in part (a) */ }
 
    /** Described in part (b) */
    public static String hideEmail(String email)
    { /* to be implemented in part (b) */ }
 
    // There may be instance variables, constructors, and methods that are not shown.
}

Example for part (a): results for 12 minutes

Minute01234567891011
isOnline(minute)truetruefalsefalsefalsetruefalsetruefalsefalsetruefalse

Source: Hypothetical example

Example calls for part (a) on the results above

CallValue returnedWhy
countOutages(1)4The offline stretches are minutes 2–4, minute 6, minutes 8–9 and minute 11.
countOutages(2)2Only minutes 2–4 (3 minutes) and minutes 8–9 (2 minutes) last at least 2 minutes.
countOutages(3)1Only minutes 2–4 last at least 3 minutes.
countOutages(4)0No stretch lasts 4 minutes.

Source: Hypothetical example

Suggested time: 25 minutes

Your answers are saved in this browser as you type.

Something wrong with this question?

What's wrong?

Please don't include personal details.

Part (a)

4 points

An *outage* is a stretch of one or more consecutive minutes during which the connection is not working, with a working minute (or the start or end of the checked period) on each side. Write the method countOutages, which returns the number of outages that last at least minLength minutes. Assume minLength >= 1. You must use getMinutes and isOnline appropriately to receive full credit. Complete method countOutages.public int countOutages(int minLength)

Tab indents. Press Esc, then Tab, to move on.

0 lines · 0 / 2,500 characters

Part (b)

3 points

When the monitor sends an alert, it hides part of each email address. Every character before the "@" except the first is replaced by "*", and the "@" and everything after it stay the same. For example, hideEmail("maria@site.org") returns "m****@site.org", hideEmail("jo@x.net") returns "j*@x.net" and hideEmail("a@b.com") returns "a@b.com". Write the static method hideEmail. Assume email contains exactly one "@" and at least one character before it. Complete method hideEmail.public static String hideEmail(String email)

Tab indents. Press Esc, then Tab, to move on.

0 lines · 0 / 2,500 characters

Checking scoring…

Scoring it yourself shows you the rubric, examples and a model answer. Try writing your answer first.