<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;   
    
    /** Constructor: a one-ball frame (a strike) --not the tenth frame*/
    public Frame() {
        this.firstBall= 10;
    }
    
    /** Constructor: a two-ball frame --not the tenth frame*/
    public Frame(int firstBall, int secondBall) {
        this.firstBall= firstBall;
        this.secondBall= secondBall;
    }
    
    /** Constructor: the tenth frame, with up to three balls.
        Make the thirdBall 0 if only two are thrown*/
    public Frame(int firstBall, int secondBall, int thirdBall) {
        this.firstBall= firstBall;
        this.secondBall= secondBall;
        this.thirdBall= thirdBall;
        isTenth= true;
    }
       
    /** = the score for this frame, given the two following frames f1 and f2.
          If this is the ninth frame, make parameter f2 null.
          If this is the tenth frame, make parameters f1 and f2 null*/
    public int score(Frame f1, Frame f2) {
        
        // Return the value for the tenth frame
        if (isTenth) {
            return firstBall + secondBall + thirdBall;
        }
        
        // { it's not the tenth frame }
        // Return the value if it is not a strike or spare
        if (firstBall + secondBall &lt; 10) {
            return firstBall + secondBall;
        }
        
        // Return the value for a spare --in other than 10th frame
        if (firstBall &lt; 10 &amp;&amp; firstBall + secondBall == 10) {
            return firstBall + secondBall + f1.firstBall;
        }
        
        // { it's a strike }
        // Return a value for a strike in the ninth frame
        if (f2 == null) {
            return firstBall + f1.firstBall + f1.secondBall;
        }
        
        // Return a value for a strike in frame in the range 1..8
        if (f1.firstBall &lt; 10) {
            return 10 + f1.firstBall + f1.secondBall;
        }
        // { The next frame is a strike }
        return firstBall + f1.firstBall + f2.firstBall;
    }
    
    /** = a representation of this frame: the balls thrown, separated
          by commas and enclosed in parentheses*/
    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 (isTenth &amp;&amp; firstBall+secondBall&gt;=10)
            res= res + "," + thirdBall; 
        return res + ")";
    }
    
    /** = score card for this frame. This is a 3-character string. Use these
          symbols: "X" for strike, "/" for spare; otherwis, number of balls
          Pad the end with a blank, if necessary.
          Examples: Two balls thrown, say 5 and 2: "52 "
          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 + " ";
    }
    
    // = graphics for a tenth frame --see spec on function card
    private String card10() {
        if (firstBall + secondBall &lt; 10) {
            return  "" + firstBall + secondBall + " ";
        }
        
        // { 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);
        }
        
        // Return a value for the case that the second ball is not a strike
        return "X" + secondBall + 
                (secondBall + thirdBall == 10 ? "/" : "" + thirdBall);
    }
    
}</pre></body></html>