Hogwarts School Of Computer Science

From Huben's Wiki
Jump to: navigation, search

Welcome To Hogwarts.jpg

GLENDOWER: I can call spirits from the vasty deep.
HOTSPUR: Why, so can I, or so can any man; But will they come when you do call for them?
Henry IV, Shakespeare

Welcome to Hogwarts School Of Computer Science!

Computers are types of perverse and hostile Demons: we command them as our servants and they perform only exactly what we commanded, even if it was not our intention.

Computer demons speak many inhuman languages: our goal will be to learn fluency in one of those languages: JAVA.

  • We will read and write lengthy incantations called programs in the arcane language JAVA, and understand how these demons and their servants can be bent to our will.
  • We will study the weird processes of demonic computation: how to invoke smaller demons called objects and how to make them dance for us.
  • We will learn the secret words that create and compel them.
  • Any little mistake in our incantations will result in our demons ceasing work and cursing us with dread error messages.
  • Thus, it behooves us to continually perfect our understanding of their demonic tongue and repair our program incantations where they show weakness.

Let us start with:

The Mystery Of The Curly Braces


// This applet displays a message moving horizontally
// across the screen.

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

public class Banner extends JApplet
  implements ActionListener
  private int xPos, yPos;  // hold the coordinates of the banner

  public void init()
    Container c = getContentPane();
    xPos = c.getWidth();
    yPos = c.getHeight() / 2;
    Timer clock = new Timer(30, this);  // fires every 30 milliseconds 

  // Called automatically after a repaint request
  public void paint(Graphics g)
    g.drawString("Hello, World!", xPos, yPos); 

  // Called automatically when the timer fires
  public void actionPerformed(ActionEvent e)
    Container c = getContentPane();

    // Adjust the horizontal position of the banner:
    xPos = xPos - 1;
    if (xPos < -100)
      xPos = c.getWidth();

    // Set the vertical position of the banner:
    yPos = c.getHeight() / 2;

  • What do you notice about the braces and the words of this incantation?

Braces delimit blocks. A block is a series of statements, and statements are easy to spot because they usually are one per line and end with a semicolon.

  • How many blocks are there in this incantation?
  • How many statements are there in this incantation?
  • Guess how many statements can be in a block: smallest number and largest number.

This incantation is called a class because it has the magic word class in it. Muggles call thes magic words reserved words.

There should be a program on your desktop called Dr. Java.

  • Start Dr. Java.
  • Cut and paste the incantation Banner.java into Dr. Java.
  • Use the file menu to save it to your H drive. Don't change the name from Banner. Dr. Java automatically knows what to call the file: you just tell it what folder.
  • Use the Edit/Preferences menu to check the "Show All Line Numbers" box. Click apply, and close the preferences box. Go "oooo, ahhhhhhh!"
  • Click "compile", then click "run".
  • Enjoy the results!

Now we play with it.

  • Look for numbers, and try to change them to make it run differently. You will need to compile and run to see your changes.
  • What numbers do what?
  • Bonus if you can find how to make it hop instead of smooth scrolling.
  • Double bonus if you can do it the right way.

Here is another incantation:

//  This program shows simple graphics in a window.

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

public class HelloGraphics extends JPanel
public void paintComponent(Graphics g)
super.paintComponent(g);  // Call JPanel's paintComponent method
                          //  to paint the background

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


// Draw a string of text starting at x = 60, y = 25:
g.drawString("Hello, Graphics!", 55, 65);

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 100
window.setBounds(300, 300, 200, 150);

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


This incantation has not followed the rules of the curly braces, but will still work.

  • Cut and paste it into Dr. Java, and save it as HelloGraphics.
  • Run it: it should work.
  • Now indent it properly so that it looks like Banner.java above.
  • Run it: it should still work.
  • What does that tell us about spaces in these incantations?
  • Why then do we indent according to curly braces?

Let's do it the easy way now!

  • Cut and paste it again into Dr. Java, so there is no indentation.
  • Select all the code.
  • Press the Tab key. What magic has resulted?
  • If you have time, you can play with this program too.
Personal tools