The Knowledge Of Sauron's Minions

From Huben's Wiki
Jump to: navigation, search

Rivendell.jpg

Welcome to Rivendell! You have completed your stay at Hogwarts and have come here to study Java even further, to learn the dark secrets of Sauron.

Contents

(A) Revealing The Knowledge

File:Robert As Gandalf.jpg
Gandalf speaking here...

Sauron knows whatever his servants, the RingWraiths, know, thus it is essential to learn of the types of knowledge RingWraiths and other fell classes can possess.

Examine well this newly formed, and thus simple RingWraith:

public class RingWraith
{
  // variables
  static public int iAmOneOf = 7;
  static public double myAgeIs= 2734.81;
  static public char exclaim = '!';
  static public boolean iBearARing = true;
  static public String iServeTheDarkLord = "Sauron";
  
  // methods
  static public void main(String[] args)
  {
      System.out.println(iAmOneOf);
      System.out.println(myAgeIs);
      System.out.println(exclaim);
      System.out.println(iBearARing);
      System.out.println(iServeTheDarkLord);
  }
}

There is only one method, main(), in this RingWraith class: the other statements at the beginning declare variables. Variables are places where the program can remember information. You can think of them as like little boxes, or perhaps whiteboards where you can write and erase things you want to remember. The five variables here are the most frequently used sorts.

There are many basic types, but here we shall concern ourselves with only four.

If you wish to represent an integer, then your variable should be of the basic type int. Yet even evil has its limits: an int variable can only represent numbers up to about 2 billion and down to negative 2 billion. We will delve into that mystery later.

If you wish to represent a real number, to have decimal points, to calculate fractions, to cleverly calculate the moon and sun, then you must use the basic type double.

The basic type boolean is the simplest of all yet represents the atom of all calculation, all knowledge: it can represent only true or false. All else can be built of boolean, but the labor is extremely difficult.

Betimes a single character must be represented, thus the basic type char.

Type String is not a basic type: it is a class and variables of type String are called objects. String is one of the important classes that come with Java, and one of the most frequently used classes.

(B) Further Revellations

  1. Trap the RingWraith code within Dr. Java.
  2. Note the different colors of the code. What could they signify?
  3. Pull down the Dr. Java menu and select Preferences. In the Categories section, select Display Options and then Colors.
  4. Make a note in your own words of what each color (from Normal Color to Brace-matching Color) designates in the code and kinds of locations it is in.
  5. Brace-matching Color is tricky. How do you reveal it in the code?
  6. I can hardly see the Type Color in the code. Click the ellipsis (...) button to the right of Type Color, and change the color to a dark red. Click Apply and OK. More visible now?
  7. Keyword means the same as reserved word. Thou shalt prepare a litany (list) of these reserved words for the purposes of further study.
  8. The basic types are also reserved words: what else then should we add to our list of reserved words?
  9. Why shouldn't we add String to the reserved words?
  10. Look at the variable names. What pattern of capitalization do you observe?
  11. Each of the variables is SET with a value. These written values are called literal constants.
  12. The exception is boolean: true and false are reserved words.

(C) The Arcane Knowledge

  1. Add another variable to RingWraith, of type double and named "compute".
  2. Inside main(), add 'compute = Math.sin(Math.PI/6);'
  3. Print out the value of compute with the proper incantation. What have we wrought?
  4. Look on my page for "References", and click on Math. Explore therein and find the secrets of exponentiation and other precalculus functions.

(D) Multitudinous Variables

As yet we have examined only singular variables. In mathematical lore, we often use variables that contain multitudes, as in matrices. In Java, we have row matrices, called arrays. Arrays can be of any type. We specify an array of a type by placing square brackets '[]' after the type.

  • Add 'static public int[] anArray = {8, 2, 5};' to the variables at the top of RingWraith.

In math, we would express this as:

anArray = [8 2 5]

In math, we would use subscripts to specify which of the numbers to select, subscripts from 1 to 3. But in Java subscripts start at zero and subscripts are numbers within square brackets. Each number in an array is called an element.

  1. Add 'System.out.println( anArray[0] + anArray[1] + anArray[2] );' into main(). What does this do?
  2. You might notice that the array was initialized to three values by placing the set of values in curly braces. Where else have we used curly braces?
  3. What is element 2 of the array?
  4. Add another value to the array by putting another number within the curly braces.
  5. Change one of the array values by adding this line in main(): 'anArray[1] = -3;'. Can you write a set of println() to show all the array values, and see which has changed?
  6. Look ye at the signature of main(). Where is the array in it? What is it an array of?
Personal tools
translate