Exercises - The ACM Libraries

The exercises below require the use of additional libraries:

The first library above was initially created by the Java Task Force of the Association for Computing Machinery (ACM) as a "stable collection of pedagogical resources that will make it easier to teach Java to first-year computing students." The second and third libraries were created at Oxford College with the intent of extending these students' ability to create interesting graphics-based programs as early as possible.

  1. Write a class named HelloAcmGraphics that extends the class GraphicsProgram and creates and adds to the canvas of it's window a rectangle whose upper left corner is at (0,0) and has width and height are 50 and 100 pixels, respectively. This class should then also construct an oval whose upper left corner is at (25, 25), also with a width and height of 50 and 100, respectively. This oval should be shown with its interior filled in so that the entire oval appears red in color.  

    
    import acm.program.*;
    import acm.graphics.*;
    import java.awt.Color;  //needed for the constant "Color.RED" below
    
    public class HelloAcmGraphics extends GraphicsProgram {
    
        public void run() {
            
            GRect myRect = new GRect(0,0,50,100);
            add(myRect);
    
            GOval myOval = new GOval(25,25,50,100);
            myOval.setFilled(true);
            myOval.setColor(Color.RED);
            add(myOval);
        }
    }
    

  2. Complete the following starter code to draw a clown face of your own design. The only restrictions are that you must use one or more of each of the following classes, found in the acm.jar package (and well-documented here):

    import acm.graphics.*;
    import acm.breadboards.OneButtonBreadboard;
    import java.awt.Color;
    
    public class FaceDrawer extends OneButtonBreadboard {
    
    	public void run() {
    	
    	    // put some text describing this work of art in the text box
    	    // at the top of the window
    	    this.getTextArea().setText("Clearly, the next Picasso!");
    			
                //code to create face...
    	}		
    }
    
  3. Write a class named SquareSpiralDrawer that extends the class OneButtonBreadboard and prompts the user (via the text area at the top of the breadboard) to enter a positive integer in the text area, and then when the button (which should be labeled "Go") is clicked a square spiral with n loops should be drawn. A sample run is shown below:

     
    
    import java.util.Scanner;
    import acm.breadboards.OneButtonBreadboard;
    import acm.toys.Turtle;
    
    public class SquareSpiralDrawer extends OneButtonBreadboard {
        
      public void run() {
        // display instructions to user
        this.getTextArea().setText("Enter a positive integer n " + 
                 "below and click 'Go' to draw a square spiral " +
                 "with n loops");
        this.getButton().setText("Go");
      }
        
      public void onButtonClick() {
        // get user input
        String userInput = this.getTextField().getText();
        Scanner scanner = new Scanner(userInput);
            
        // if the user typed an integer..
        if (scanner.hasNextInt()) {
          int numLoops = scanner.nextInt();
                
          // create a turtle..
          Turtle turtle = new Turtle();
          turtle.setLocation(250,200);
          this.add(turtle);
        
          // have the turtle draw a spiral with the desired 
          // number of loops..
          for (int i = 0; i < 4 * numLoops; i++) {
            turtle.forward(10 * i);
            turtle.left(90); // degrees
          }
        }
        // if the user didn't type an integer, complain
        else {
          this.getTextArea().setText("Input not an integer! Try again.");
        }
      }
    }
    

  4. Write a class named ManuallyControlledTurtle that extends the OneButtonBreadboard class of the com.oxfordmathcenter.breadboards package. When run, this class should first display a dialog box to the user identical to the one shown below

    Each time the user follows the instructions given, and enters some number of degrees to rotate counter-clockwise and some number of units to move forward, and then clicks the "Move!" button -- two things should happen:

    1. The turtle should move accordingly, drawing its path as it goes.

    2. The text area at the top of the window (which serves as a log of all commands given) should be updated to reflect the latest command.

    An example is shown below. In the example, the "Move!" button has already been pressed three times after three different commands.

  5. Write a class named HelloBreadboardAndToys that extends the OneButtonBreadboard class and takes the following actions, in order:

    1. Displays the message "Hi, I'm a text area!" in the text area at the top of the breadboard window

    2. Displays the message "I'm more text" in the text field at the bottom of the breadboard window

    3. Changes the background color of the breadboard's canvas to a color whose red, green, and blue components are 71, 113, and 72, respectively. (Note: color component values must always fall between 0 and 255, inclusive)

    4. Display on the canvas a new poker card, showing the ace of spades, and with its upper left corner at (0, 0); wait one second; and then move the card so its upper left corner is at (100, 100)

    5. Create a new die (as in the singular version of "dice"); wait one second; and then roll the die so that the value shown on its face potentially changes.

    6. Wait one second and then display a (LOGO-inspired) turtle at (100, 300). Then move the turtle forward 50 pixels, turn it counter-clockwise 60 degrees, and then go forward an additional 50 pixels.

    7. Wait one second and then display a new 10 by 10 grid that occupies an area 98 pixels square, with 2 pixels of spacing between grid elements. Then, change the color of the element in the 3rd row and 4th column of the pixel grid to yellow.

    8. Whenever the breadboard's button is clicked, the text found in the text field at the bottom of the window should be shown in the the text area at the top of the window, while the text in the text field should be changed to "You clicked the button!"

     
    
    import java.awt.Color;
    
    import acm.breadboards.OneButtonBreadboard;
    import acm.graphics.GRect;
    import acm.toys.Card;
    import acm.toys.Die;
    import acm.toys.PixelGrid;
    import acm.toys.Turtle;
    
    public class HelloBreadboardAndToys extends OneButtonBreadboard {
        
        public void run() {
            this.setSize(60,700);
            this.getTextArea().setText("Hi, I'm a text area!");
            this.getTextField().setText("I'm more text");
            
            //GRect rect = new GRect(50,100);
            //this.add(rect);
            
            this.setBackground(new Color(71,113,72));
            Card card = new Card(Card.ACE, Card.SPADES);
            this.add(card);
            this.pause(1000);
            card.setLocation(100,100);
            this.pause(1000);
            card.flipOver();
            
            this.pause(1000);
            Die die = new Die();
            this.add(die);
            this.pause(1000);
            die.roll();
            
            this.pause(1000);
            Turtle turtle = new Turtle();
            turtle.setLocation(100,300);
            this.add(turtle);
            turtle.forward(50);
            turtle.left(60);
            turtle.forward(50);
            
            this.pause(1000);
            PixelGrid pg = new PixelGrid(98,98,10,10,2);
            pg.setLocation(250,50);
            this.add(pg);
            pg.plot(3, 4, Color.YELLOW);
            
        }
    
        public void onButtonClick() {
            this.getTextArea().setText(this.getTextField().getText());
            this.getTextField().setText("You clicked the button!");
        }
    
    }
    
    

  6. Write a class named SandCastle that extends the OneButtonBreadboard class and draws a picture of a sandcastle on the canvas of the breadboard, subject to the following:


    Hint: To get the correct height for the "sand", you will need to know the height of the visible portion of the canvas. If the entire window is 400 pixels high, the height of the visible portion of the canvas 400 minus the sum of the height of the upper region (where the text area is) and the height of the lower region (where the text field and button are). These heights can be found with this.getRegionPanel(NORTH).getHeight() and this.getRegionPanel(SOUTH).getHeight(), respectively.