public class ProgramOne { // Draw an uppercase letter 'H', 12 pixels tall and 6 wide. // Put a circle around the outside of the 'H'.
public static void main (String[ ] args) { Turtle sam; // create the variable named sam sam = new Turtle(); // create the object sam refers to
sam.paint (90, 12); // draw the left side of the H sam.move (-180, 6); sam.paint (90, 6); // draw the crossbar of the H sam.move (90, -6); sam.paint (0, 12); // draw the right side of the H sam.move (150, 6); sam.swingAround (9); // draw a circle enclosing the H } // this right-brace marks the end of the main method } // this right-brace marks the end of the class
public class TwoSquares { // Draw two 40x40 squares side by side, 10 pixels apart.
public static void main (String[ ] args) { Turtle sue; sue = new Turtle();
sue.paint (90, 40); // draw the right side of square #1 sue.paint (90, 40); // draw the top of square #1 sue.paint (90, 40); // draw the left side of square #1 sue.paint (90, 40); // draw the bottom of square #1
sue.move (0, 50); // move 50 pixels to the right sue.paint (90, 40); // draw the right side of square #2 sue.paint (90, 40); // draw the top of square #2 sue.paint (90, 40); // draw the left side of square #2 sue.paint (90, 40); // draw the bottom of square #2 } //====================== }
public class SmartTurtle extends Turtle { // Make a 10x10 square; finish with the same position/heading.
public void makeSmallSquare() { paint (90, 10); paint (90, 10); paint (90, 10); paint (90, 10); } //======================
// Make a 40x40 square; finish with the same position/heading.
public void makeBigSquare() { paint (90, 40); paint (90, 40); paint (90, 40); paint (90, 40); } //====================== }
public class SquarePattern { // Make an X shape with one big 40x40 square in the center // and a small 10x10 square in each corner.
public static void main (String[ ] args) { SmartTurtle sue; sue = new SmartTurtle(); sue.makeBigSquare(); // draw the center square
sue.move (-90, 15); // go south sue.move (90, 15); // move to the southeast corner sue.makeSmallSquare(); // draw the southeast square
sue.move (90, 70); // move to the northeast corner sue.makeSmallSquare(); // draw the northeast square
sue.move (90, 70); // move to the northwest corner sue.makeSmallSquare(); // draw the northwest square
sue.move (90, 70); // move to the southwest corner sue.makeSmallSquare(); // draw the southwest square } //====================== }
public class GardenApp { // Draw 6 flowers all in a row, with a word title.
public static void main (String[ ] args) { FlowerMaker florist; florist = new FlowerMaker(); florist.drawTwoFlowers(); // the central two florist.sleep (300);
florist.move (0, 120); florist.drawTwoFlowers(); // the two right of center florist.sleep (300);
florist.move (0, -240); florist.drawTwoFlowers(); // the two left of center florist.sleep (300);
florist.move (40, 130); florist.switchTo (Turtle.BLUE); florist.say ("My flower garden"); // above the flowers } //====================== }
public class FlowerMaker extends Turtle { // Draw two flowers each 60 pixels tall. // Start and end facing east at the base of the left flower.
public void drawTwoFlowers() { drawFlower(); move (90, 61); drawFlower(); move (90, -59); } //======================
// Start facing east at the base of the flower, right side. // End facing south at the base of the flower, center.
public void drawFlower() { paint (90, 50); // right side of stem paint (90, 2); paint (90, 50); // left side of stem paint (90, 1); paint (90, 10); // one-fourth of the way up the stem paint (-45, 8); // draw the twig for the right leaf drawLeaf();
paint (45, 10); // one-half of the way up the stem paint (45, 8); // draw the twig for the left leaf drawLeaf();
paint (-45, 30); // to top of stem, in the center switchTo (RED); fillCircle (15); // draw the flower petals switchTo (BLACK); move (180, 50); // return to the base of the flower } //======================
// Draw one leaf on one flower.
public void drawLeaf() { switchTo (GREEN); fillCircle (3); move (0, 3); fillCircle (2); move (0, 2); fillCircle (1); move (0, -13); switchTo (BLACK); } //====================== }
public class InvestFor20 { public static void main (String[ ] args) { BuyAndHoldPortfolio wealth; // 1 wealth = new BuyAndHoldPortfolio(); // 2 wealth.describeInvestmentChoices(); // 3 wealth.waitForYears (20); // 4 wealth.displayCurrentValues(); // 5 } //====================== }
public class BuyAndHoldPortfolio { public void describeInvestmentChoices() { System.out.println ("You have 5 ways to invest."); } //======================
public void waitForYears (int years) { System.out.println ("Wait for your money to grow."); } //======================
public void displayCurrentValues() { System.out.println ("You now have lots of money."); } //====================== }
public class FractalApp { // Draw a Pythogorean tree.
public static void main (String[ ] args) { FractalTurtle pythagoras; pythagoras = new FractalTurtle(); pythagoras.move (90, -240); pythagoras.drawTree (80); } //====================== }
public class FractalTurtle extends Turtle { public void drawTree (double trunk) { paint (0, trunk); // go to top of trunk move (30, 0); // face to the left if (trunk > 1) drawTree (trunk * 0.7); // make branches on the left move (-60, 0); // face to the right if (trunk > 1) drawTree (trunk * 0.7); // make branches on the right move (30, -trunk); // go to bottom of trunk } //====================== }
|