Entering Starfleet Academy

From Huben's Wiki
Jump to: navigation, search

Spock here. Welcome to Starfleet! Where everybody is sexy, good-looking, healthy, brilliant, able to fight with their fists and has terrible fashion sense. And don't get me started on acting ability or plausibility of scripts.

Contents

Starfleet Technology

Transport this technology into your Dr. Java Junior Starfleet Tricorder and scan with it.

//  Your introduction to Starfleet technology!

import java.awt.*;
import javax.swing.*;

public class Recurse extends JPanel
{
  public void recursive(Graphics g, int x, int y, int w, int h)
  {
    // if ((w <= 0) || (h <= 0))
    //  return;
    g.drawRect(x, y, w, h);
    // recursive(g, x + 3, y + 3, w - 6, h - 6);
  }
  
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);  // Call JPanel's paintComponent method
                              //  to paint the background
    g.setColor(Color.RED);

    // Draw a 150 by 45 rectangle with the upper-left
    // corner at x = 20, y = 40:
    // was g.drawRect(20, 40, 150, 45);
    recursive(g, 20, 40, 150, 45);
  }

  public static void main(String[] args)
  {
    JFrame window = new JFrame("Graphics Demo");
    // Set this window's location and size:
    // upper-left corner at 300, 300; width 200, height 150
    window.setBounds(300, 300, 200, 150);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Recurse panel = new Recurse();
    panel.setBackground(Color.WHITE);  // the default color is light gray
    Container c = window.getContentPane();
    c.add(panel);

    window.setVisible(true);
  }
}

Investigating Your Tricorder

  • The first rule of investigating your Tricorder is to not activate the Corbomite Device.
  • See that teeny little window? Make it bigger. What did you change?
  • The x and y coodinates in your tricorder are all first quadrant, but with a twist. Discover the twist. What is it?
  • That delicate little rectangle can also be changed. How do you change its size and location? Look here to see how to change the Color.

Recursive Technology

Since the days of early Earth Computer Science, recursion has been one of the most powerful tools for mathematics and programming. The fundamental idea is that a method calculates USING ITSELF. This is not circular: the logic is that a method breaks a problem into two simpler parts and then calls itself for one or both of those parts. Such a method could lead to infinite regress, calling itself forever, except that the method must check for when the calculation can stop. Usually the calculation can stop when it is so simple that no more recursion is needed, or because some limit is reached.

We will now change our program so that it draws boxes inside boxes.

  • Uncomment the three commented lines in the method recursive().
  • Run the program and note the difference!
  • Method recursive divides the problem of drawing multilple boxes into two pieces. It draws one box, and then uses itself to draw all the littler boxes inside. Which statement does which?
  • Method recursive would try to make smaller and smaller rectangles forever, except that it stops calling itself when the boxes get too small.
    • Write an explanation of how it judges that the boxes are too small.
    • Test your explanation by changing a line so that it draws multiple boxes, but stops before all the possible boxes are drawn.

Challenges!

  • In a Fibonacci sequence, each term is the sum of the previous two terms. That's recursion: to find a term, we find the preceding two terms and add them. That breaks the problem into two simpler parts, calling itself to calculate each of the simpler parts. In algebra, we say fib(n) = fib(n-1) + fib(n-2). So when do we stop? We are told fib(1) = 1 and fib(0) = 0. The sequence is 0, 1, 1, 2, 3, 5, 8, 13....
    • Write a class Fibonacci with a main() that prints fib(6).
    • Write a method public static int fib(int n). That method should return 1 if n is 1, and 0 if n is 0. Otherwise, it should return the sum of the preceding two Fibonacci numbers.
  • Change the spacing of the rectangles so that they are closer or further apart.
  • The rectangle shrink concentrically. Make them shrink eccentrically, to one corner or another.
  • Can you make the rectangles change color as they shrink?
Personal tools
translate