Sprint View

FRQ 2 - Sign Class

2 min read

FRQ 2: Sign Class

This question involves methods that distribute text across lines of an electronic sign. The Sign class contains:

  • A constructor with two parameters: a String message and an int width
  • numberOfLines() - returns the number of lines needed to display the message
  • getLines() - returns the message broken into lines separated by semicolons

Key Rules:

  • The message is split among lines without regard to spaces or punctuation
  • Only the last line may contain fewer characters than the width
  • getLines() returns null if the message is empty

Code Runner Challenge

FRQ 2 - Sign Class Challenge

View IPYNB Source
// CODE_RUNNER: FRQ 2 - Sign Class Challenge

class Sign {
    private String message;
    private int width;

    // TODO: Write the constructor
    public Sign(String message, int width) {
        // Your code here
    }

    // TODO: Returns the number of lines needed
    public int numberOfLines() {
        // Your code here
        return 0;
    }

    // TODO: Returns message split by semicolons, null if empty
    public String getLines() {
        // Your code here
        return null;
    }
}

class Main {
    public static void main(String[] args) {
        Sign sign1 = new Sign("ABC222DE", 3);
        System.out.println("sign1 numberOfLines(): " + sign1.numberOfLines()); // Expected: 3
        System.out.println("sign1 getLines(): " + sign1.getLines()); // Expected: ABC;222;DE

        Sign sign2 = new Sign("ABCD", 10);
        System.out.println("sign2 numberOfLines(): " + sign2.numberOfLines()); // Expected: 1
        System.out.println("sign2 getLines(): " + sign2.getLines()); // Expected: ABCD

        Sign sign3 = new Sign("ABCDEF", 6);
        System.out.println("sign3 numberOfLines(): " + sign3.numberOfLines()); // Expected: 1
        System.out.println("sign3 getLines(): " + sign3.getLines()); // Expected: ABCDEF

        Sign sign4 = new Sign("", 4);
        System.out.println("sign4 numberOfLines(): " + sign4.numberOfLines()); // Expected: 0
        System.out.println("sign4 getLines(): " + sign4.getLines()); // Expected: null

        Sign sign5 = new Sign("AB_CD_EF", 2);
        System.out.println("sign5 numberOfLines(): " + sign5.numberOfLines()); // Expected: 4
        System.out.println("sign5 getLines(): " + sign5.getLines()); // Expected: AB;_C;D_;EF

        Sign sign6 = new Sign("Everything on sale, please come in", 15);
        System.out.println("sign6 numberOfLines(): " + sign6.numberOfLines()); // Expected: 3
        System.out.println("sign6 getLines(): " + sign6.getLines()); // Expected: Everything on s;ale, please com;e in
    }
}

Main.main(null);

Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Expected Output

If the implementation is correct, running the code should produce:

sign1 numberOfLines(): 3
sign1 getLines(): ABC222DE
sign2 numberOfLines(): 1
sign2 getLines(): ABCD
sign3 numberOfLines(): 1
sign3 getLines(): ABCDEF
sign4 numberOfLines(): 0
sign4 getLines(): null
sign5 numberOfLines(): 4
sign5 getLines(): AB_CD_EF
sign6 numberOfLines(): 3
sign6 getLines(): Everything on sale, please come in

Course Timeline