/** Class methods that do useful things with numbers. */
public class MathOp { /** Return the value of 2 to the power expo. */
public static int powerOf2 (int expo) { int power = 1; for (; expo > 0; expo--) power = power * 2; return power; } //======================
/** Return sum divided by count, rounded to the nearest int.*/
public static int average (int sum, int count) { if (sum >= 0) return (sum + count / 2) / count; else return (sum - count / 2) / count; } //====================== }
public class Person extends Object { private static int theNumPersons = 0; // initialize num ///////////////////////////////// private String itsFirstName; private String itsLastName; private int itsBirthYear;
/** Construct a new Person with given names and birth year. */
public Person (String first, String last, int year) { super(); theNumPersons++; // update num itsFirstName = first; itsLastName = last; // initialize last name itsBirthYear = year; } //=======================
/** Tell how many different Persons exist. */
public static int getNumPersons() // access num { return theNumPersons; } //======================
/** Return the birth year. */
public int getBirthYear() { return itsBirthYear; } //=======================
/** Return the first name. */
public String getFirstName() { return itsFirstName; } //=======================
/** Return the last name. */
public String getLastName() // access last name { return itsLastName; } //=======================
/** Replace the last name by the specified value. */
public void setLastName (String name) // update last name { itsLastName = name; } //======================= }
public class Vic extends Object { private static final String NONE = "0"; ///////////////////////////////// private String itsSequence = ""; private int itsPos = 1; private final int itsID; // assigned by the constructor
public String getPosition() { return itsPos + "," + itsID; } //======================
public static void say (String message) { System.out.println ("SAYS: " + message); } //======================
private void trace (String action) { System.out.println ("Vic# " + itsID + ": " + action + itsPos + "; sequence= " + itsSequence); } //======================
public void backUp() { if (itsPos == 1) System.exit (0); itsPos--; trace ("backUp to slot "); } //======================
public void moveOn() { if ( ! seesSlot()) System.exit (0); itsPos++; trace ("moveOn to slot "); } //======================
public boolean seesSlot() { return itsPos < itsSequence.length(); } //======================
public boolean seesCD() { if ( ! seesSlot()) System.exit (0); String s = itsSequence.substring (itsPos, itsPos + 1); return ! s.equals (NONE); } //======================
// public class Vic continued: using the stack
private static String theStack = ""; // initially empty
public static boolean stackHasCD() { return theStack.length() > 0; } //======================
public void takeCD() { if (seesCD()) { theStack = theStack + itsSequence.substring (itsPos, itsPos + 1); itsSequence = itsSequence.substring (0, itsPos) + NONE + itsSequence.substring (itsPos + 1, itsSequence.length()); } trace ("takeCD at slot "); } //======================
public void putCD() { if ( ! seesCD() && stackHasCD()) { int atEnd = theStack.length() - 1; itsSequence = itsSequence.substring (0, itsPos) + theStack.substring (atEnd, atEnd + 1) + itsSequence.substring (itsPos + 1, itsSequence.length()); theStack = theStack.substring (0, atEnd); } trace ("putCD at slot "); } //======================
// public class Vic completed: constructor and reset
private static final java.util.Random randy = new java.util.Random(); private static final String LETTERS = " abcdefghijkl"; private static int theNumVics = 0; private static String[ ] theTableau = {"", "", ""};
public static void reset (String[ ] args) { if (theNumVics == 0 && args.length > 0) theTableau = args; } //======================
public Vic() { super(); // 1 if (theNumVics < theTableau.length) // 2 { itsSequence = theTableau[theNumVics]; // 3 if (itsSequence.length() == 0) // 4 { for (int k = 3 + randy.nextInt (6); k >= 1; k--) // 5 { if (randy.nextInt (2) == 0) // 6 itsSequence = NONE + itsSequence; // 7 else // 8 itsSequence = LETTERS.substring (k, k + 1)// 9 + itsSequence; // 10 } // 11 } // 12 itsSequence = " " + itsSequence; // 13 } // 14 theNumVics++; // 15 itsID = theNumVics; // 16 trace ("constructed "); // 17 } //====================== }
import javax.swing.JOptionPane;
class NetApp { /** List all nodes and tell how many there are. */
public static void main (String[ ] args) { Network school = new Network(); int count = 0; for (Position pos = school.nodes(); pos.hasNext(); pos.moveOn()) { Node current = pos.getNext(); JOptionPane.showMessageDialog (null, current.getName() + " is one of the nodes."); count++; } JOptionPane.showMessageDialog (null, "The total number of nodes is " + count); System.exit (0); } //====================== }
/** Answer queries about one or two given nodes. * Precondition for all methods: no parameter is null. */ public class NodeOp { /** Tell whether par connects only to blue nodes. */
public static boolean seesOnlyBlue (Node par) { for (Position p = par.nodes(); p.hasNext(); p.moveOn()) { if ( ! p.getNext().isBlue()) return false; } return true; } //=======================
/** Tell whether from connects to target. */
public static boolean connected (Node from, Node target) { for (Position p = from.nodes(); p.hasNext(); p.moveOn()) { if (p.getNext().equals (target)) return true; } return false; } //=======================
/** Tell whether par connects to any node of the same color.*/
public static boolean seesSameColor (Node par) { return par.isBlue() && ! seesOnlyNonBlue (par) || ! par.isBlue() && ! seesOnlyBlue (par); } //=======================
/** Tell whether par connects to no blue node. */
public static boolean seesOnlyNonBlue (Node par) { for (Position p = par.nodes(); p.hasNext(); p.moveOn()) { if (p.getNext().isBlue()) return false; } return true; } //======================= }
import javax.swing.JOptionPane;
public class SmartNet extends Network { /** Tell whether par connects to all other nodes in this * network.*/
public boolean connectsToAll (Node par) { for (Position pos = nodes(); pos.hasNext(); pos.moveOn()) { Node current = pos.getNext(); if ( ! (current.equals (par) || NodeOp.connected (current, par))) return false; } return true; } //======================
/** Return the total number of nodes in this network. */
public int getNumNodes() { int count = 0; for (Position pos = nodes(); pos.hasNext(); pos.moveOn()) count++; return count; } //======================
/** List all nodes in this network that connect to * some node of the same color. */
public void printSameColorConnections() { for (Position pos = nodes(); pos.hasNext(); pos.moveOn()) { Node current = pos.getNext(); if (NodeOp.seesSameColor (current)) JOptionPane.showMessageDialog (null, current.getName()); } } //======================
/** Tell whether every node is reachable from * the given node. */
public boolean allReachableFrom (Node source) { checkOut (source); // sets a mark of 2 on source boolean foundNodeToCheckOut; do { foundNodeToCheckOut = false; for (Position pos= nodes(); pos.hasNext(); pos.moveOn()) { Node current = pos.getNext(); if (current.getMark() == 1) { foundNodeToCheckOut = true; checkOut (current); } } }while (foundNodeToCheckOut); return allNodesAreMarked(); } //======================
/** Mark 2 on par; mark 1 on all nodes reachable from par * except for those already marked 1 or 2. * Precondition: par is not null. */
private static void checkOut (Node par) { par.setMark (2); for (Position p = par.nodes(); p.hasNext(); p.moveOn()) { Node current = p.getNext(); if (current.getMark() == 0) current.setMark (1); } } //======================
/** Tell whether all the nodes have a non-zero mark. * Also set all marks back to zero. */
private boolean allNodesAreMarked() { boolean valueToReturn = true; for (Position pos = nodes(); pos.hasNext(); pos.moveOn()) { if (pos.getNext().getMark() == 0) valueToReturn = false; else pos.getNext().setMark (0); } return valueToReturn; } //======================
/** Tell whether it is possible to travel through n nodes, * all different and all marked 0, starting from Node base. * Precondition: base is marked 0. */
public static boolean canTravelFrom (Node base, int n) { if (n <= 1) return true; base.setMark (1); for (Position p = base.nodes(); p.hasNext(); p.moveOn()) { Node current = p.getNext(); if (current.getMark() == 0 && canTravelFrom (current, n - 1)) { base.setMark (0); // restore original state return true; } } base.setMark (0); // restore original state return false; } //====================== }
public class Position extends Object { private int itsCurrent; // Node at current position on list private int itsLast; // Node at last position on list
public Position (int first, int last) { super(); itsCurrent = first; itsLast = last; } //======================
/** Return the next Node in the sequence. Return null * if you have come to the end of the sequence. */
public Node getNext() { if (itsCurrent > itsLast) return null; else return new Node (itsCurrent % Network.NUM_NODES); } //======================
/** Tell whether there is another Node in the sequence. */
public boolean hasNext() { return itsCurrent <= itsLast; } //======================
/** Go one Node further in the sequence. */
public void moveOn() { itsCurrent++; } //====================== }
public class Network extends Object { public static final int NUM_NODES = 100; ///////////////////////////////////////
/** Return an object that can produce all Nodes 1 at a time. */
public Position nodes() { return new Position (0, NUM_NODES - 1); } //====================== }
public class Node extends Object { private int itsID; // ranges from 0 to NUM_NODES - 1
public Node (int index) { super(); itsID = index; } //======================
/** Return an object that can produce all Nodes that * are connected to the executor, 1 at a time. */
public Position nodes() { return new Position (itsID + 1, itsID + 4); } //======================
/** Tell whether the parameter has the same contents as * the executor. */
public boolean equals (Node par) { return par != null && this.itsID == par.itsID; } //======================
/** Tell whether this Node has the property of being blue. */
public boolean isBlue() { return itsID % 2 == 1; } //======================
/** Return the name of this Node. */
public String getName() { return "Darryl"; } //======================
private int itsMark = 0;
/** Assign a value to the mark on this Node. */
public void setMark (int par) { itsMark = par; } //======================
/** Return the current value marked on this Node. */
public int getMark() { return itsMark; } //====================== }
|