Contents
- Random/Random.java
- Random/Random.out
- Dice/Dice.java
- Dice/Dice.out
- ShellIO/ShellIO.java
- ShellIO/ShellIO.out
- FileIO/FileIO.java
- FileIO/foobar.txt
- FileIO/FileIO.out
Random/Random.java 1/9
[top][prev][next]
/** File: Random.java
*
* Created by: Daniel J. Hood
* Created on: Tuesday, February 25, 2003
*
* Description: Most basic Random Numbers in Java
*/
class Random
{
public final static int ROWS = 10;
public static void main(String[]args)
{
// print ROWS number of raw random numbers
for (int i = 0; i < ROWS; i++)
{
// Per the API documentation: Returns a double value with a
// positive sign, greater than or equal to 0.0 and less than 1.0.
// In mathimatical terms... [ 0, 1.0 )
System.out.println( Math.random() );
}
}
}
Random/Random.out 2/9
[top][prev][next]
linux3-(01:16pm): javac Random.java
linux3-(01:16pm): java Random
0.5077436934997478
0.30539047967887767
0.35608032471905526
0.9674459538096128
0.5438140310566452
0.24126924376522318
0.11192205425184532
0.2506292045636671
0.09332214409929629
0.048374102717184786
linux3-(01:16pm): java Random
0.8840820146035463
0.29757300029621103
0.9968446390244041
0.7931441689357417
0.6113608880651145
0.5317647999931486
0.7076155989847002
0.05802285910273164
0.8180000736661341
0.5208212131188885
linux3-(01:16pm):
Dice/Dice.java 3/9
[top][prev][next]
/** File: Dice.java
*
* Created by: Daniel J. Hood
* Created on: Tuesday, February 25, 2003
*
* Description: Basic Dice Rolling Program
*/
// This is where DecimalFormat lives...
import java.text.*;
class Dice
{
// symbol constants for min and max faces of die...
public final static int MIN = 1;
public final static int MAX = 6;
public static void main( String[] args )
{
// how many samples we want to do - 1/2 million
final int SAMPLES = 500000;
// how wide do we want the bar graph
final int COLS = 30;
///////// Let's first do single dice rolls /////////
///////// we expect and even distribution /////////
// ugh!!! Java does not like this delaration... int totals[ MAX + 1 ];
//
// Dice.java:21: Can't specify array dimension in a type expression.
// int totals[ MAX ];
// ^
// so we have to do it like this...
int [] totals = new int [ MAX + 1 ];
// initialize the totals array
for ( int i = MIN ; i <= MAX ; i++ )
{
totals[i] = 0;
}
// tally the rolls
for ( int i = 0 ; i < SAMPLES ; i++ )
{
totals[ RollDie() ]++ ;
}
// let's do some formatted output
DecimalFormat f1 = new DecimalFormat("00.000%");
// print the chart header
System.out.println("\n Fairness Chart \n====================");
// print out the totals
for ( int i = MIN ; i <= MAX ; i++ )
{
System.out.println( i + "'s: " + totals[i] + " " +
f1.format( (double) totals[i]/SAMPLES ) );
}
///////// now lets do sum of 2 dice //////////
///////// should be a bell curve //////////
// get new array 2x as big...
totals = new int [ 2 * MAX + 1 ];
// initialize the totals array
for ( int i = MIN ; i <= 2 * MAX ; i++ )
{
totals[i] = 0;
}
// tally the rolls
for ( int i = 0 ; i < SAMPLES ; i++ )
{
totals[ RollDie() + RollDie() ]++ ;
}
// let's do some formatted output
f1 = new DecimalFormat("00");
// assume we have not seen any totals yet...
int max = 0;
// lets find the largest tally
for ( int i = 2 * MIN ; i <= 2 * MAX ; i++ )
{
if ( totals[i] > max )
{
max = totals[i];
}
}
// print header info...
System.out.println("\n\n Bar Graph - Number of Times Rolled ");
System.out.println("-----+------------------------------+");
// lets print the bar graph...
for ( int i = 2 * MIN ; i <= 2 * MAX ; i++ )
{
// print the number that was tallied
System.out.print( f1.format( i ) + "'s |" );
// how many chars should we print...
for ( int j = 0 ; j < COLS ; j++ )
{
// if we had at least that many then print *
if ( j < (int) ( (double) totals[i] / max * COLS ) )
{
System.out.print("*");
}
// otherwise whitespace
else
{
System.out.print(" ");
}
}
// close graph and endline
System.out.println("|");
}
// print the bottom of the graph
System.out.println("-----+------------------------------+\n");
}
/** Rolls the dice and gets a value between MIN and MAX.
*/
public static int RollDie ( )
{
// Math.random() returns a double in the range [ 0.0 , 1.0 )
// so we need to scale that into our range...
return ( (int) (Math.random() * (MAX - MIN + 1) + MIN) );
}
}
Dice/Dice.out 4/9
[top][prev][next]
linux3-(01:17pm): javac Dice.java
linux3-(01:17pm): java Dice
Fairness Chart
====================
1's: 83385 16.677%
2's: 84285 16.857%
3's: 83505 16.701%
4's: 83399 16.680%
5's: 82642 16.528%
6's: 82784 16.557%
Bar Graph - Number of Times Rolled
-----+------------------------------+
02's |***** |
03's |********** |
04's |************** |
05's |******************** |
06's |************************ |
07's |******************************|
08's |************************* |
09's |******************** |
10's |************** |
11's |********* |
12's |***** |
-----+------------------------------+
linux3-(01:17pm):
ShellIO/ShellIO.java 5/9
[top][prev][next]
/** File: ShellIO.java
*
* Created by: Daniel J. Hood
* Created on: Tuesday, February 25, 2003
*
* Description: Basic Shell IO, String manipulation,
* as well as String to number conversion
*/
// where our readers and IOException lives...
import java.io.*;
class ShellIO
{
public static void main( String[] args )
{
// lets set to do buffered reading from stdin
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
// where we are going to store the line that we read in
String buffer;
//////////////// let's try to get some text /////////////////////
try
{
// prompt the user...
System.out.print("\nEnter some text: ");
// slurp in a whole line
buffer = br.readLine();
// let's see what we got...
System.out.println("\nbr.readLine() got: \"" + buffer + "\"");
// The result of the for loop
System.out.print("\nThe for loop produces: \"");
// reverse the buffer...
for ( int i = buffer.length() - 1 ; i >= 0 ; i-- )
{
System.out.print( buffer.charAt(i) );
}
// end the for loop output...
System.out.println("\"\n");
// we can change the String into a array of chars (C style)...
char [] array = buffer.toCharArray();
// print each index
for ( int i = 0 ; i < buffer.length() ; i++ )
{
System.out.println( "array[" + i + "] = " + array[i] );
}
}
// catch IOException in case of error...
catch ( IOException e )
{
System.err.println("Error: " + e.getMessage() );
}
///////////////// let's try to get an Int ///////////////////////
try
{
// prompt the user...
System.out.print("\n\nEnter an Integer: ");
// slurp in a whole line
buffer = br.readLine();
// let's see what we got...
System.out.println("\nbr.readLine() got: \"" + buffer + "\"");
// our number conversion could fail...
try
{
// number is a string, so we need to convert to an int...
int num = Integer.valueOf(buffer).intValue();
// tell the user what we got
System.out.println("\nInteger.valueOf(buffer).intValue() got: "
+ num + "\n" );
}
// and if it does...
catch ( NumberFormatException e )
{
System.err.println("Error: " + e.getMessage() + "\n" );
}
}
// catch IOException in case of error...
catch ( IOException e )
{
System.err.println("Error: " + e.getMessage() );
}
////////////////// let's try to get a float /////////////////////
try
{
// prompt the user...
System.out.print("\nEnter an Float: ");
// slurp in a whole line
buffer = br.readLine();
// let's see what we got...
System.out.println("\nbr.readLine() got: \"" + buffer + "\"");
// our number conversion could fail...
try
{
// number is a string, so we need to convert to an int...
float num = Float.valueOf(buffer).floatValue();
// tell the user what we got
System.out.println("\nFloat.valueOf(buffer).floatValue() got: "
+ num + "\n" );
}
// and if it does...
catch ( NumberFormatException e )
{
System.err.println("Error: " + e.getMessage() );
}
}
// catch IOException in case of error...
catch ( IOException e )
{
System.err.println("Error: " + e.getMessage() );
}
}
}
ShellIO/ShellIO.out 6/9
[top][prev][next]
linux3-(01:19pm): javac ShellIO.java
linux3-(01:19pm): java ShellIO
Enter some text: I will not
br.readLine() got: "I will not"
The for loop produces: "ton lliw I"
array[0] = I
array[1] =
array[2] = w
array[3] = i
array[4] = l
array[5] = l
array[6] =
array[7] = n
array[8] = o
array[9] = t
Enter an Integer: 1234
br.readLine() got: "1234"
Integer.valueOf(buffer).intValue() got: 1234
Enter an Float: 567.890
br.readLine() got: "567.890"
Float.valueOf(buffer).floatValue() got: 567.89
linux3-(01:19pm):
FileIO/FileIO.java 7/9
[top][prev][next]
/** File: FileIO.java
*
* Created by: Daniel J. Hood
* Created on: Tuesday, February 25, 2003
*
* Description: Some Basic File IO...
*/
// This is where DecimalFormat lives...
import java.text.*;
// where our readers and IOException lives...
import java.io.*;
class FileIO
{
public static final String filename = "foobar.txt";
public static void main( String[] args )
{
// all this for buffered file read...ugh!
File f = new File(filename);
// attempt to open the file
try
{
FileInputStream fin = new FileInputStream(f );
DataInputStream din = new DataInputStream(fin);
InputStreamReader isr = new InputStreamReader(din);
BufferedReader br = new BufferedReader(isr);
// our tmp buffer that is our read in line
String buffer;
// let's attempt to read from the file
try
{
// start line numbering at 1
int i = 1;
// let's do some formatted output
DecimalFormat f1 = new DecimalFormat("0000");
while ( (buffer = br.readLine()) != null )
{
// print out the numbered line...
System.out.println( f1.format(i) + ": " + buffer );
// increment i for the next line
i++;
}
}
// if we were unable to read
catch ( IOException e )
{
System.err.println("Error: " + e.getMessage() );
}
}
// catch File Not Found exception..
catch ( FileNotFoundException e )
{
System.err.println("Error: " + e.getMessage() );
}
}
}
FileIO/foobar.txt 8/9
[top][prev][next]
It's nine o'clock on a Saturday
The regular crowd shuffles in
There's an old man sitting next to me
Makin' love to his tonic and gin
He says, "Son, can you play me a memory?
I'm not really sure how it goes
But it's sad and it's sweet and I knew it complete
When I wore a younger man's clothes"
La la la, de de da
La la, de de da da da
Chorus:
Sing us a song, you're the piano man
Sing us a song tonight
Well, we're all in the mood for a melody
And you've got us feelin' alright
Now John at the bar is a friend of mine
He gets me my drinks for free
And he's quick with a joke or to light up your smoke
But there's someplace that he'd rather be
He says, "Bill, I believe this is killing me."
As the smile ran away from his face
"Well I'm sure that I could be a movie star
If I could get out of this place"
Oh, la la la, de de da
La la, de de da da da
Now Paul is a real estate novelist
Who never had time for a wife
And he's talkin' with Davy who's still in the navy
And probably will be for life
And the waitress is practicing politics
As the businessmen slowly get stoned
Yes, they're sharing a drink they call loneliness
But it's better than drinkin' alone
Chorus
It's a pretty good crowd for a Saturday
And the manager gives me a smile
'Cause he knows that it's me they've been comin' to see
To forget about life for a while
And the piano, it sounds like a carnival
And the microphone smells like a beer
And they sit at the bar and put bread in my jar
And say, "Man, what are you doin' here?"
Oh, la la la, de de da
La la, de de da da da
Chorus
FileIO/FileIO.out 9/9
[top][prev][next]
linux3-(01:20pm): javac FileIO.java
linux3-(01:20pm): ls
FileIO.class FileIO.java foobar.txt
linux3-(01:20pm): java FileIO
0001: It's nine o'clock on a Saturday
0002: The regular crowd shuffles in
0003: There's an old man sitting next to me
0004: Makin' love to his tonic and gin
0005:
0006: He says, "Son, can you play me a memory?
0007: I'm not really sure how it goes
0008: But it's sad and it's sweet and I knew it complete
0009: When I wore a younger man's clothes"
0010:
0011: La la la, de de da
0012: La la, de de da da da
0013:
0014: Chorus:
0015: Sing us a song, you're the piano man
0016: Sing us a song tonight
0017: Well, we're all in the mood for a melody
0018: And you've got us feelin' alright
0019:
0020: Now John at the bar is a friend of mine
0021: He gets me my drinks for free
0022: And he's quick with a joke or to light up your smoke
0023: But there's someplace that he'd rather be
0024: He says, "Bill, I believe this is killing me."
0025: As the smile ran away from his face
0026: "Well I'm sure that I could be a movie star
0027: If I could get out of this place"
0028:
0029: Oh, la la la, de de da
0030: La la, de de da da da
0031:
0032: Now Paul is a real estate novelist
0033: Who never had time for a wife
0034: And he's talkin' with Davy who's still in the navy
0035: And probably will be for life
0036:
0037: And the waitress is practicing politics
0038: As the businessmen slowly get stoned
0039: Yes, they're sharing a drink they call loneliness
0040: But it's better than drinkin' alone
0041:
0042: Chorus
0043:
0044: It's a pretty good crowd for a Saturday
0045: And the manager gives me a smile
0046: 'Cause he knows that it's me they've been comin' to see
0047: To forget about life for a while
0048: And the piano, it sounds like a carnival
0049: And the microphone smells like a beer
0050: And they sit at the bar and put bread in my jar
0051: And say, "Man, what are you doin' here?"
0052:
0053: Oh, la la la, de de da
0054: La la, de de da da da
0055:
0056: Chorus
linux3-(01:20pm): rm foobar.txt
rm: remove `foobar.txt'? y
linux3-(01:20pm): ls
FileIO.class FileIO.java
linux3-(01:20pm): java FileIO
Error: foobar.txt (No such file or directory)
linux3-(01:20pm):
Generated by GNU enscript 1.6.1.