<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/** An instance represents one frame of a bowling game */
public class Frame {
    
    private boolean isTenth= false;   // = "this is the tenth frame"   
    // The number of pins knocked down on the throws in this frame
    // (If there are only one or two throws, the other variables are 0.)
    private int firstBall= 0;   
    private int secondBall= 0;  
    private int thirdBall= 0; 
    
    
    
     /** = the score for this frame, given the two following frames f1 and f2.
          Precondition: If this is the ninth frame, f2 is null.
                        If this is the tenth frame, f1 and f2 are null*/
    public int score(Frame f1, Frame f2) {
        if (firstBall + secondBall &lt; 10) {
            return firstBall + secondBall;
        }
        
        // it's a spare or strike
        if (isTenth) {
            return firstBall + secondBall + thirdBall;
        }
        
        // it's a spare or strike in a frame in 1..9
        if (firstBall == 10  &amp;&amp;  f2 == null) {  // strike in frame 9
            return 10 + f1.firstBall + f1.secondBall;
        }
        
        // it's a spare in a frame in 1..9 or a strike in a frame in 1..8
        if (firstBall == 10) {
            return 10 + f1.firstBall +
                (f1.firstBall &lt; 10 ? f1.secondBall : f2.firstBall);
        }
        
        // it's a spare in a frame in 1..9
        return 10 + f1.firstBall;
        
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    /** Constructor: a one-ball frame (a strike) --not the tenth frame*/
    public Frame() {
        this.firstBall= 10;
    }
    
    /** Constructor: a two-ball frame (spare) --not the tenth frame 
        The first ball is firstBall; the second is 10 - firstBall*/
    public Frame(int firstBall) {
        this.firstBall= firstBall;
        this.secondBall= 10 - firstBall;
    }
    
    /** Constructor: a three ball frame.
     *  It is the tenth frame iff isTenth is true */
    public Frame(int firstBall, int secondBall, int thirdBall, boolean isTenth) {
        this.firstBall= firstBall;
        this.secondBall= secondBall;
        this.thirdBall= thirdBall;
        this.isTenth= isTenth;
    }
       
   
    
    /** = a representation of this frame: the balls thrown, separated
          by commas and enclosed in parentheses. Do not put blanks anywhere.*/
    public String toString() {
        String res= "(" + firstBall;   // Accumulate the result in res
        // Append second ball to res if necessary
        if (isTenth || firstBall&lt;10) 
            res= res + "," + secondBall;
        // Append third ball if necessary
        if (firstBall + secondBall &lt; 10 || isTenth)
            res= res + "," + thirdBall; 
        return res + ")";
    }
    
    /** = score card for this frame. This is a 4-character string. Use these
          symbols: "X" for strike, "/" for spare; otherwise, number of balls
          Pad the end with blank, if necessary, to make it 4 characters.
          In the examples shown below, # is used for a blank or space character.
          Of course, you should output a blank, and not #.
          Example of three balls thrown, say 5, 2, 7: "527#"
          Example of three balls thrown, say 0, 0, 10: "0010"
          Example of a spare in any but the tenth frame: "4/##"
          Example of a spare in any but the tenth frame: "X###"
          Example of a spare in tenth frame, last ball is 8: "4/8#"
          Example of a spare and strike in tenth frame: "4/X#"
          Example of a strike and spare in tenth frame: "X9/#" */
    
    
    
    
    
    
    
    
    
    public String card() {
        if (isTenth) { return card10(); }
        
        // { it is not the tenth frame }
        if (firstBall == 10) {
            return "X   ";
        }
        
        if (secondBall + firstBall == 10) { 
            return firstBall + "/  ";  
        }
      
        return "" + firstBall + secondBall + fixThirdBall(thirdBall);
    }
    
    // = graphics for a tenth frame --see spec on function card
    private String card10() {
        if (firstBall + secondBall &lt; 10) {
            return  "" + firstBall + secondBall + fixThirdBall(thirdBall);
        }
        
        // { firstBall + secondBall &gt;= 10 }
        // Return the value for the case that the first 2 balls are a spare
        if (firstBall != 10) {
            return firstBall + "/" + (thirdBall == 10 ? "X" : "" + thirdBall) + " ";
        }
        
        // { firstBall is a strike }
        // Return a value for the case that the second ball is a strike
        if (secondBall == 10) {
            return "XX" + (thirdBall == 10 ? "X" : "" + thirdBall) + " ";
        }
        
        // { first ball is a strike and the second ball is not }
        // Return a value for the case that the second ball is not a strike
        return "X" + secondBall + 
                (secondBall + thirdBall == 10 ? "/" : "" + thirdBall) + " ";
    }
    
    /** If thirdBall is 10, return it as a String.
     *  If thirdBall is &lt; 10, return it as a string with a blank appended */
    private static String fixThirdBall(int thirdBall) {
        if (thirdBall == 10)
            return "" + 10;
        return thirdBall + " ";
    }
    
}</pre></body></html>