Home page da disciplina MAC 324 Mapa do sítio da disciplina


MAC 324: Fontes dos programas Java

Capítulo 1 de Arnold e Gosling

class HelloWorld { 
    public static void main(String[] args) { 
        System.out.println("Hello, world"); 
    } 
}

% 2 =============================================================

class Fibonacci {
    /** Print out the Fibonacci sequence for values < 50 */
    public static void main(String[] args) {
        int lo = 1;
        int hi = 1;

        System.out.println(lo);
        while (hi < 50) {
            System.out.println(hi);
            hi = lo + hi;       // new hi
            lo = hi - lo;       /* new lo is (sum - old lo)
                                   i.e., the old hi */
        }
    }
}

% 3 ===============================================================

class CircleStuff {
       static final double pi = 3.1416;
}

% 4 ================================================================

class Suit {
    final static int CLUBS    = 1;
    final static int DIAMONDS = 2;
    final static int HEARTS   = 3;
    final static int SPADES   = 4;
}

% 5 =================================================================

class ImprovedFibonacci {
    /** Print out the first few Fibonacci
      * numbers, marking evens with a '*' */
    static final int MAX_INDEX = 10;

    public static void main(String)[] args) {
        int lo = 1;
        int hi = 1;
        String mark;

        System.out.println("1: " + lo);
        for (int i = 2; i < MAX_INDEX; i++) {
            if (hi % 2 == 0)
                mark = " *";
            else
                mark = "";
            System.out.println(i + ": " + hi + mark);
            hi = lo + hi;                // new hi
            /* new lo is (sum - old lo) i.e., the old hi */
            lo = hi - lo;
        }
    }
}

% 6 =================================================================

class Point {
    public double x, y;
}

% 7 ==================================================================

public void clear() {
    x = 0;
    y = 0;
}

% 8 ===================================================================

public void clear () {
    this.x = 0;
    this.y = 0;
}

% 9 ===================================================================

public void move(double x, double y) {
    this.x = x;
    this.y = y;
}

% 10 ==================================================================

public double distance(Point that) {
    double xdiff = x - that.x;
    double ydiff = y - that.y;
    return Math.sqrt(xdiff * xdiff + ydiff * ydiff);
}

% 11 ==================================================================

class Deck {
    final int DECK_SIZE = 52;

    Card[] cards = new Card[DECK_SIZE];

    public void print() {
        for (int i = 0; i < cards.lenght; i++)
            System.out.println(cards[i]);
    }
    // ...
}

% 12 ===============================================================
    
class StringsDemo {
    static public void main(String args[]) {
        String   myName = "Petronius";

        myName = myName + " Arbiter";
        System.out.println("Name = " + myName);
    }
}

% 13 =================================================================      

class BetterStringsDemo {
    static public void main(String args[]) {
        String myName = "Petronius";
        String occupation = "Reorganization Specialist";

        myName = myName + " Arbiter";
        myName += " ";
        myName += "(" + occupation + ")";
        System.out.println("Name = " + myName);
    }
}

% 14 ==================================================================

class Pixel extends Point {
    Color color;

    public void clear() {
        super.clear();
        color = null;
    }
}

Capítulo 2 de Arnold e Gosling

% 1 ============================================================

class Body {
    public long idNum;
    public String nameFor;
    public Body orbits;

    public static long nextID = 0;
}

% 2 =============================================================

Body sun = new Body();
sun.idNum = Body.nextID++;
sun.nameFor = "Sol";
sun.orbits = null; 
      // in solar system, sun is middle

Body earth = new Body();
earth.idNum = Body.nextID++;
earth.nameFor = "Earth";
earth.orbits = sun;

% 3 ===============================================================

class Body {
    public long idNum;
    public String name = "";
    public Body orbits = null;

    private static long nextID = 0;

    Body() {
        idNum = nextID++;
    }
}

% 4 ================================================================

Body sun = new Body(); // idNum is 0
sun.name = "Sol";

Body earth = new Body(); // idNum is 1
earth.name = "Earth";
earth.orbits = sun;



Body(String bodyName, Body orbitsAround) {
    this();
    name = bodyName;
    orbits = orbitsAround;
}



Body sun = new Body("Sol", null);
Body earth = new Body("Earth", sun);



public class SimpleClass {
    /** Same as default constructor */
    public SimpleClass() {
    }
}

% 5 =================================================================

public String toString() {
    String desc = idNum + " (" + name + ")";
    if (orbits != null)
        desc += " orbits " + orbits.toString();
    return desc;
}

% 6 =================================================================

System.out.println("Body " + sun);
System.out.println("Body " + earth);



Body 0 (Sol)
Body 1 (Earth) orbits 0 (Sol)



public class Permissions {
    public boolean canDeposit;
                   canWithdraw,
                   canClose;
}

% 7 ==================================================================

class PassByValue {
    public static void main(String[] args) {
        double one = 1.0;

        System.out.println("before: one = " + one);
        halveIt(one);
        System.out.println("after:  one = " + one);
    }
   
    public static void halveIt(double arg) {
        arg /= 2.0;   // divide arg by two
        System.out.println("halved: arg = " + arg);
    }
}

% 8 ===================================================================

class PassRef {
    public static void main(String[] args) {
        Body sirius = new Body("Sirius", null);

        System.out.println("before: " + sirius);
        commonName(sirius);
        System.out.println("after:  " + sirius);
    }

    public static void commonName(Body bodyRef) {
        bodyRef.name = "Dog Star";
        bodyRef = null;
    }
} 

% 9 ===================================================================

class Body {
    private long idNum; // now "private"

    public String name = "";
    public Body orbits = null;

    private static long nextID = 0;

    Body() {
        idNum = nextID++;
    }

    public long getID() {
        return idNum;
    }

    // ...
}

% 10 ================================================================

class Name {
    public String str;

    Name() {
        str = "";
    }
}



class Moose {
    String hairdresser;

    Moose(String hairdresser) {
        this.hairdresser = hairdresser;
    }
}

% 11 =================================================================


public boolean orbitsAround(Body other) {
    return (orbits == other);
}

public boolean orbitsAround(long id) {
    return (orbits != null && orbits.idNum == idNum);
}

% 12 ===============================================================

class Value {
    public static double UNSET = Double.NaN;

    private double V;

    public void unset() {
        V = UNSET;
    }
    
    // ...
}

% 13 =================================================================      

class Primes {
    protected static int[] knownPrimes = new int[4];

    static {
        knownPrimes[0] = 2;
        for (int i = 1; i < knownPrimes.length; i++)
                knownPrimes[i] = nextPrime();
    }
}

% 14 ==================================================================

public class ProcessFile {
    private Stream file;

    public ProcessFile(String path) {
        file = new Stream(path);

    }

    // ...

    public void close() {
        if (file != null) {
            file.clos();
            file = null;
        }
    }

    protected void finalize() throws Throwable {
        try {
            close();
        } finally {
            super.finalize();
        }
    }
}

% 15 ====================================================================

static void displayBodies(Body[] bodies) {
    for (int i = 0; i < bodies.length; i++)
        System.out.println(i + ": " + bodies[i]);
}

Capítulo 3 do Standish

/*--------------------------------------------------------------------------*/

   /** Program Strategy 3.15 Inserting a Second Node for BRU in a List (DUS,ORD,SAN) */

   |  void insertNewSecondNode(BRU) {    // let the initial list L be given by
   |                                                    // L = (DUS, ORD, SAN)
   |
   |    (declare a pointer variable newNode, that can point to list nodes)
   |
 5 |    (construct a new node and let the pointer variable newNode point to it)
   |    (set the airport field of the newNode to BRU)
   |    (change the link field of newNode to point to L's second node)
   |    (change the link of L's first node to point to the newNode)
   |  }


/*--------------------------------------------------------------------------*/

   /** Refinement of Program Strategy 3.15 into a program given on page 76 */

   |  void insertNewSecondNode() {
   |
   |    ListNode newNode;                // let newNode be a ListNode variable
   |
 5 |    newNode = new ListNode();              // store a new node in newNode
   |    newNode.airport = "BRU";             // set newNode's airport to "BRU"
   |    newNode.link = L.link;          // let newNode link to L's second node
   |    L.link = newNode;            // let L's first node link to the newNode
   |  }


/*--------------------------------------------------------------------------*/

   /** Program 3.16 The LinkedList Class Definition */

   |  public class LinkedList {
   |  
   |   // the two data fields of a LinkedList are:
   |    
 5 |      int       length;                // the number of nodes in the list.
   |      ListNode  firstNode;         // contains a pointer to the first node
   |                                        // of its list of linked ListNodes
   |  
   |   // definitions of methods that manipulate LinkedLists are given next.
10 |   // for example, the size() method gives the number of items in the list
   |  
   |      public int size() {       // returns the number of items in the list
   |        return length;
   |      }
15 |  
   |   // insert here the texts of the following LinkedList methods given in:
   |      // Program 3.17  insertNewSecondNode(airportCode)
   |      // Program 3.19  listSearch(airportCode)
   |      // Program 3.21  deleteLastNode()
20 |      // Program 3.22  insertNewLastNode(airportCode)
   |      // Program 3.23  print()
   |  }


/*--------------------------------------------------------------------------*/

   /** Program 3.17 General Method for Inserting a New Second Node */

   |  public void insertNewSecondNode(String airportCode) {
   |  
   |   // if the list has no first node, the result is undefined.
   |   // in this case, exit the method instead of trying to insert a second node
5  |      if (length == 0) return;
   |  
   |   // declare a pointer variable newNode that can point to ListNodes
   |   // and initialize it to an initially empty ListNode object
   |      ListNode newNode = new ListNode();
10 |  
   |   // set the airport of the newNode to the method's airportCode argument
   |      newNode.airport = airportCode;
   |
   |   // change the link field of the newNode to point to the list's second node
15 |      newNode.link = firstNode.link;
   |
   |   // change the link field of the firstNode to point to the newNode
   |      firstNode.link = newNode;
   |    
20 |   // increase the length of the list by one
   |      length++;
   |  }


/*--------------------------------------------------------------------------*/

   /** Program Strategy 3.18 Strategy for List Searching */

   |  public ListNode listSearch(String airportCode) {
   |  
   |    (declare a variable N that can point to ListNodes)
   |  
 5 |    (initially, set N to point to the first node of the list)
   |    
   |    while (N points to a non-null node on the list) {
   |  
   |      if (N's airport field contains the airportCode) {
10 |    
   |        (exit the method and return the node pointer in N)
   |      
   |      } else {
   |  
15 |        (advance the pointer N to point to the next node on the list)  
   |      }      
   |    }
   |    
   |    (return N's value, null, as the result of the list search)
20 |
   |  }


/*--------------------------------------------------------------------------*/

   /** Program 3.19 List Searching Method */

   |  public ListNode listSearch(String airportCode) {
   |  
   |   // declare a variable N that can point to ListNodes
   |      ListNode N;
 5 |  
   |   // initially, set N to point to the first node of the list
   |      N = firstNode;
   |    
   |   // while N points to a non-null node on the list
10 |   // examine N's airport field
   |      while (N != null) {
   |        if (airportCode.equals(N.airport)) {     // if node N contains the
   |          return N;                             // airportCode, return the
   |        } else {                          // node pointer in N; otherwise,
15 |          N = N.link;                             // advance N to point to
   |        }                                     // the next node on the list
   |      }
   |    
   |   // return null if no node's airport equals the airportCode
20 |      return N;
   |  }


/*--------------------------------------------------------------------------*/

   /** Program Strategy 3.20 Strategy for Deleting the Last Node of a List */

   |  public void deleteLastNode() {    // a method that deletes the last node
   |                                                 // of a LinkedList object
   |  
   |    (let previousNode and currentNode contain pointers to ListNodes)
 5 |    
   |    if (the list is not the empty list) {  
   |    
   |      if (the list has exactly one node) {
   |      
10 |        (set the list to be empty)
   |        (and decrease its length by one)
   |      
   |      } else {    // otherwise the list must have two or more nodes
   |      
15 |        (initialize a pair of pointers, (previousNode, currentNode) )
   |        (to point to the first and second nodes)
   |        
   |        (advance the pointer pair along the list until)
   |        (currentNode points to the last node)
20 |
   |          while (currentNode does not point to the last node) {
   |            (advance the pair of pointers to the next pair of nodes)
   |          }
   |        
25 |        (now previousNode points to the next-to-last node on the list)
   |        (and currentNode points to the last node on the list)
   |  
   |        (finally, change the next-to-last node into the new last node)
   |        (and decrease the list length by one)
30 |  
   |      }
   |    
   |    }
   |  
   |  }


/*--------------------------------------------------------------------------*/

   /** Program 3.21 Deleting the Last Node of a List */

   |  public void deleteLastNode() {
   |  
   |   // let previousNode and currentNode contain pointers to ListNodes
   |      ListNode previousNode, currentNode;
 5 |    
   |    if (firstNode != null) {           // do nothing if the list was empty
   |    
   |      if (firstNode.link == null) {             // if the list had exactly
   |                                                         // one node, then
10 |        firstNode = null;                 // set the list to be empty, and
   |        length--;                            // decrease its length by one
   |      
   |      } else {           // otherwise the list must have two or more nodes
   |      
15 |        // initialize a pair of pointers (previousNode, currentNode)
   |        // to point to the first and second nodes
   |           previousNode = firstNode;
   |           currentNode = firstNode.link;
   |        
20 |        // advance the pointer pair along the list until
   |        // currentNode points to the last node on the list
   |           while (currentNode.link != null) {
   |             previousNode = currentNode;
   |             currentNode = currentNode.link;
25 |           }
   |        
   |        // now previousNode points to the next-to-last node on the list
   |        // and currentNode points to the last node on the list
   |           previousNode.link = null;     // set null link in new last node
30 |           length--;                        // decrease list length by one
   |        
   |      }
   |    
   |    }
35 |  
   |  }


/*--------------------------------------------------------------------------*/

   /** Program 3.22 Inserting a New Last Node on a List */

   |  public void insertNewLastNode(String airportCode) {
   |  
   |   // construct a new node N with airport == airportCode and link == null
   |      ListNode N = new ListNode();
 5 |      N.airport = airportCode;
   |      N.link = null;
   |    
   |   // insert N as the new last node on the list
   |      if (firstNode == null) {                    // if the list was empty
10 |      
   |        firstNode = N;                  // let N become the new first node
   |      
   |      } else {
   |      
15 |        // locate the last node of the list, using the node pointer P
   |           ListNode P = firstNode;
   |           while (P.link != null){
   |             P = P.link;
   |           }
20 |        
   |        // finally, link node N onto the end of the list
   |           P.link = N;
   |        
   |      }
25 |  
   |   // increase the length of the list by one
   |      length++;
   |    
   |  }


/*--------------------------------------------------------------------------*/

   /** Program 3.23 Printing a List */

   |  public void print() {
   |  
   |   ListNode N;                 // N points to successive nodes on the list
   |  
 5 |   // first, print a left parenthesis
   |      System.out.print( "(" );
   |  
   |   // let N start by pointing to the first node on the list
   |      N = firstNode;
10 |  
   |   // provided N doesn't point to an empty node, print N's airport
   |   // and advance N to point to the next node on the list
   |
   |      while (N != null) {
15 |        System.out.print(N.airport);                 // print airport code
   |        N = N.link;                           // make N point to next node
   |        if (N != null) {
   |          System.out.print(", ");        // print comma between list items
   |        }
20 |      }
   |    
   |   // finally, print a closing right parenthesis
   |      System.out.println( ")" );
   |  }


/*--------------------------------------------------------------------------*/

   /** Program 3.24 An Example That Puts Some Pieces Together */

   |  import java.io.*;
   |  import java.applet.Applet;
   |
   |
 5 |  public class LinkedListApplet extends Applet {
   |    
   |    public void init() {
   |  
   |     // let L be a new linked list
10 |        LinkedList L = new LinkedList();
   |    
   |     // first, construct the list L = (DUS, ORD, SAN)
   |        L.insertNewLastNode("DUS");
   |        L.insertNewLastNode("ORD");
15 |        L.insertNewLastNode("SAN");
   |    
   |     // now, print the list to show what it looks like before changing it
   |        L.print();
   |    
20 |     // then, insert a new second node with the airport code BRU
   |        L.insertNewSecondNode("BRU");
   |      
   |     // print the modified list
   |        L.print();
25 |    
   |     // delete the last node of the list
   |        L.deleteLastNode();
   |      
   |     // finally, print the shortened list
30 |        L.print();
   |
   |    } // end init()
   |  
   |  
35 |  } // end class LinkedListApplet
   |  
   |  // to finish the program, insert the full texts for the class
   |  // definitions of ListNode and LinkedList here.
   |  


/*--------------------------------------------------------------------------*/

Home page da disciplina MAC 324 Mapa do sítio da disciplina

e-mail: Imre Simon <is@ime.usp.br>

Last modified: Sun Mar 22 20:17:08 EST 1998