CSC212: Computer Science.
TEXTBOOK: Object-Oriented Data Structures Using Java
TEXTBOOK: Source Code
|
//----------------------------------------------------------------------
// BlobApp.java by Dale/Joyce/Weems Chapter 4
//
// Instantiates a Grid based on a percentage probability provided by the
// user and reports the number of blobs in the Grid.
//----------------------------------------------------------------------
import java.util.Scanner;
public class BlobApp
{
public static void main(String[] args)
{
Scanner conIn = new Scanner(System.in);
final int GRIDR = 10; // number of grid rows
final int GRIDC = 40; // number of grid columns
// Get percentage probability of blob characters
int percentage;
System.out.print("Input percentage probability (0 to 100): ");
if (conIn.hasNextInt())
percentage = conIn.nextInt();
else
{
System.out.println("Error: you must enter an integer.");
System.out.println("Terminating program.");
return;
}
System.out.println();
// create grid
Grid grid = new Grid(GRIDR, GRIDC, percentage);
// display grid and blob count
System.out.println(grid);
System.out.println("\nThere are " + grid.blobCount() + " blobs.\n");
}
}
|
|
//import java.util.*;
public class Factorial2
{
private static int factorial(int n)
{
int retValue = 1; // return value
while (n != 0)
{
retValue = retValue * n;
n = n - 1;
}
return(retValue);
}
public static void main(String[] args)
{
System.out.println("\n9! is " + factorial(9));
}
}
|
|
//----------------------------------------------------------------------
// Grid.java by Dale/Joyce/Weems Chapter 4
//
// Supports grid objects consisting of blob and non-blob characters.
// Allows the number of blobs it contains to be counted.
//----------------------------------------------------------------------
import java.util.Random;
public class Grid
{
protected int rows; // number of grid rows
protected int cols; // number of grid columns
protected boolean [][] grid; // the grid containing blobs
boolean [][] visited; // used by blobCount
public Grid(int rows, int cols, int percentage)
// Preconditions: rows and cols > 0
// 0 <= percentage <= 100
//
// Instantiates a grid of size rows by cols, where locations are set to
// indicate blob characters based on the percentage probability.
{
this.rows = rows;
this.cols = cols;
grid = new boolean [rows][cols];
// to generate random numbers
int randInt;
Random rand = new Random();
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
{
randInt = rand.nextInt(100); // random number 0 .. 99
if (randInt < percentage)
grid[i][j] = true;
else
grid[i][j] = false;
}
}
public String toString()
{
String gridString = "";
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (grid[i][j])
gridString = gridString + "X";
else
gridString = gridString + "-";
}
gridString = gridString + "\n"; // end of row
}
return gridString;
}
public int blobCount()
// returns the number of blobs in this grid
{
int count = 0;
visited = new boolean [rows][cols]; // true if location visited
// initialize visited
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
visited[i][j] = false;
// count blobs
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (grid[i][j] && !visited[i][j])
{
count++;
markBlob(i, j);
}
return count;
}
private void markBlob(int row, int col)
// Mark position row, col as having been visited.
// Check and if appropriate mark locations above, below, left,
// and right of that position.
{
visited[row][col] = true;
// check above
if ((row - 1) >= 0) // if its on the grid
if (grid[row - 1][col]) // and has a blob character
if (!visited[row - 1][col]) // and has not been visited
markBlob(row - 1, col); // then mark it
// check below
if ((row + 1) < rows) // if its on the grid
if (grid[row + 1][col]) // and has a blob character
if (!visited[row + 1][col]) // and has not been visited
markBlob(row + 1, col); // then mark it
// check left
if ((col - 1) >= 0) // if its on the grid
if (grid[row][col - 1]) // and has a blob character
if (!visited[row][col - 1]) // and has not been visited
markBlob(row, col - 1); // then mark it
// check right
if ((col + 1) < cols) // if its on the grid
if (grid[row][col + 1]) // and has a blob character
if (!visited[row][col + 1]) // and has not been visited
markBlob(row, col + 1); // then mark it
}
}
|
|
public class Kids
{
private static int countKids(int girlCount, int boyCount)
{
int totalKids;
totalKids = girlCount + boyCount;
return(totalKids);
}
public static void main(String[] args)
{
int numGirls;
int numBoys;
int numChildren;
numGirls = 12;
numBoys = 13;
numChildren = countKids(numGirls, numBoys);
System.out.println("Number of children is " + numChildren);
}
}
|
|
//----------------------------------------------------------------------
// LinkedStack2.java by Dale/Joyce/Weems Chapter 4
//
// Extends LinkedStack with a printReversed method.
//----------------------------------------------------------------------
import ch03.stacks.*;
import support.LLNode;
public class LinkedStack2 extends LinkedStack
{
private void revPrint(LLNode listRef)
{
if (listRef != null)
{
revPrint(listRef.getLink());
System.out.println(" " + listRef.getInfo());
}
}
public void printReversed()
{
revPrint(top);
}
}
|
|
//----------------------------------------------------------------------
// LinkedStack3.java by Dale/Joyce/Weems Chapter 4
//
// Extends LinkedStack with a non-recursive printReversed method.
//----------------------------------------------------------------------
import ch03.stacks.*;
import support.LLNode;
public class LinkedStack3 extends LinkedStack
{
public void printReversed()
{
UnboundedStackInterface stack = new LinkedStack();
LLNode listNode;
listNode = top;
while (listNode != null) // Put references onto the stack
{
stack.push(listNode.getInfo());
listNode = listNode.getLink();
}
// Retrieve references in reverse order and print elements
while (!stack.isEmpty())
{
System.out.println(" " + stack.top());
stack.pop();
}
}
}
|
|
//----------------------------------------------------------------------
// Towers.java by Dale/Joyce/Weems Chapter 4
//
// Driver class for doTowers method that gets initial values and
// calls the method.
//----------------------------------------------------------------------
import java.util.Scanner;
public class Towers
{
private static String indent = ""; // indentation for trace
public static void main(String[] args)
{
Scanner conIn = new Scanner(System.in);
// Number of rings on starting peg.
int n;
System.out.print("Input the number of rings: ");
if (conIn.hasNextInt())
n = conIn.nextInt();
else
{
System.out.println("Error: you must enter an integer.");
System.out.println("Terminating program.");
return;
}
System.out.println("Towers of Hanoi with " + n + " rings\n");
doTowers(n, 1, 2, 3);
}
public static void doTowers(
int n, // Number of rings to move
int startPeg, // Peg containing rings to move
int auxPeg, // Peg holding rings temporarily
int endPeg ) // Peg receiving rings being moved
{
if (n > 0)
{
indent = indent + " ";
System.out.println(indent + "Get " + n + " rings moved from peg " +
startPeg + " to peg " + endPeg);
// Move n - 1 rings from starting peg to auxiliary peg
doTowers(n - 1, startPeg, endPeg, auxPeg);
// Move nth ring from starting peg to ending peg
System.out.println(indent + "Move ring " + n + " from peg " + startPeg
+ " to peg " + endPeg);
// Move n - 1 rings from auxiliary peg to ending peg
doTowers(n - 1, auxPeg, startPeg, endPeg);
indent = indent.substring(2);
}
}
}
|
|
//----------------------------------------------------------------------
// Towers2.java by Dale/Joyce/Weems Chapter 4
//
// Driver class for doTowers method that gets initial values and
// calls the method. Tracing statements removed.
//----------------------------------------------------------------------
import java.util.Scanner;
public class Towers2
{
public static void main(String[] args)
{
Scanner conIn = new Scanner(System.in);
// Number of rings on starting peg.
int n;
System.out.print("Input the number of rings: ");
if (conIn.hasNextInt())
n = conIn.nextInt();
else
{
System.out.println("Error: you must enter an integer.");
System.out.println("Terminating program.");
return;
}
System.out.println("Towers of Hanoi with " + n + " rings\n");
doTowers(n, 1, 2, 3);
}
public static void doTowers(
int n, // Number of rings to move
int startPeg, // Peg containing rings to move
int auxPeg, // Peg holding rings temporarily
int endPeg ) // Peg receiving rings being moved
{
if (n > 0)
{
// Move n - 1 rings from starting peg to auxiliary peg
doTowers(n - 1, startPeg, endPeg, auxPeg);
// Move nth ring from starting peg to ending peg
System.out.println("Move ring " + n + " from peg " + startPeg
+ " to peg " + endPeg);
// Move n - 1 rings from auxiliary peg to ending peg
doTowers(n - 1, auxPeg, startPeg, endPeg);
}
}
}
|
|
//import java.util.*;
public class TraceFactorial
{
private static String indent = ""; // indentation for trace
private static int factorial(int n)
{
int retValue; // return value
System.out.println(indent + "Enter factorial " + n);
indent = indent + " ";
if (n == 0)
retValue = 1;
else
retValue = (n * factorial (n - 1));
indent = indent.substring(2);
System.out.println(indent + "Return " + retValue);
return(retValue);
}
public static void main(String[] args)
{
System.out.println("\n9! is " + factorial(9));
}
}
|
|
import java.io.*;
public class tryComb
{
private static int count;
private static int combinations(int group, int members)
// Pre: group and members are positive
// Post: Return value = number of combinations of members size
// that can be constructed from the total group size
{
count++;
if (members == 1)
return group; // Base case 1
else if (members == group)
return 1; // Base case 2
else
return (combinations(group - 1, members - 1) +
combinations(group - 1, members));
}
public static void main(String[] args) throws IOException
{
count = 0;
System.out.println("comb(20,5) = " + combinations(20,5));
System.out.println("method entered " + count + "times");
count = 0;
System.out.println("comb(4,3) = " + combinations(4,3));
System.out.println("method entered " + count + "times");
count = 0;
System.out.println("comb(10,3) = " + combinations(10,3));
System.out.println("method entered " + count + "times");
}
}
|
|
import java.io.*;
public class tryFact
{
private static int factorial(int n)
{
if (n == 0)
return 1;
else
return (n * factorial (n - 1));
}
private static int factorial2(int n)
{
int fact = 1;
for (int count = 2; count <= n; count++)
{
fact = fact * count;
}
return fact;
}
public static void main(String[] args) throws IOException
{
System.out.println("5! = " + factorial(5));
System.out.println("5! = " + factorial2(5));
}
}
|
|
//----------------------------------------------------------------------------
// UseStack2.java by Dale/Joyce/Weems Chapter 2
//
// Driver for Stack2
//----------------------------------------------------------------------------
import ch03.stacks.*;
public class UseStack2
{
public static void main(String[] args)
{
LinkedStack2 myStack2 = new LinkedStack2();
myStack2.push("first");
myStack2.push("second");
myStack2.push("third");
myStack2.push("fourth");
myStack2.printReversed();
}
}
|
|
//----------------------------------------------------------------------------
// UseStack3.java by Dale/Joyce/Weems Chapter 4
//
// Driver for LinkedStack3
//----------------------------------------------------------------------------
import ch03.stacks.*;
public class UseStack3
{
public static void main(String[] args)
{
LinkedStack3 myStack3 = new LinkedStack3();
myStack3.push("first");
myStack3.push("second");
myStack3.push("third");
myStack3.push("fourth");
myStack3.printReversed();
}
}
|