Mon Oct 27, 2008 9:41 am
/*
Time: 3 hours 25 minutes
Grade: 98
Status: Fully operational with correct output
Comments:
*/
import java.io.*;
import java.text.*;
class rectype
{
/*
This class defines the structure of the data to be stored in the hash table.
*/
String name;
int age;
float weight;
public rectype()
{
name = "";
age = 0;
weight = 0;
} // rectype
} // clase rectype
class hashtbl
{
/*
This class provides the machinery necessary to implement a hash table
lookup mechanism.
*/
// constants
final int MAXHASH = 50;
final int MAXRECS = 25;
final int NIL = -1;
// types
private class tblrow
{
rectype rec;
int next;
}
// private data
private int avail;
private int[] htbl;
private tblrow[] rectbl;
// private functions
private int getnode()
{
/*
This routine retrieves from the pool of available storage a pointer to
a new node to link onto a hash chain. If no such node is available,
the value NIL is returned.
*/
int p;
p = avail;
if (p != NIL) avail = rectbl[avail].next;
return(p);
} // getnode
private void putnode(int p)
{
/*
This routine returns a node back to the pool of available storage
*/
rectbl[p].next = avail;
avail = p;
} // putnode
private int hash(String key)
{
/*
This hash function transforms a record key into an integer.
*/
int i,sum;
sum = 0;
for (i = 0; i < key.length(); i++) sum += key.charAt(i);
sum += key.length();
return (sum % MAXHASH);
} // hash
// public functions
public hashtbl()
{
/*
Consructor to establish an empty instance of a hash table.
*/
int i;
htbl = new int[MAXHASH];
rectbl = new tblrow[MAXRECS];
for (i = 0; i < MAXHASH; i++) htbl[i] = NIL;
avail = 0;
for (i = 0; i < MAXRECS; i++)
{
rectbl[i] = new tblrow();
rectbl[i].rec = new rectype();
rectbl[i].next = i + 1;
}
rectbl[MAXRECS-1].next = NIL;
} // hashtbl
public int insert(rectype rec)
{
/*
Inserts a record with the specified key into the hash table. The
status return value has the following meaning:
0 = record was inserted in table
1 = record is already present in the table
2 = no more space in table
(Note: name field of rec contains key value)
*/
int status;
/*??*/
return(status);
} // insert
public int find(rectype rec)
{
/*
Retrieves from the hash table the record having the specified key.
The status return value has the following meaning:
0 = record was found in table
1 = record is not in table
(Note: name field of rec contains key value)
*/
int status;
/*??*/
return(status);
} // find
public int remove(String key)
{
/*
Removes record with the specified key from the hash table. The
status return value has the following meaning:
0 = item was deleted from table
1 = item was not in the table
(Note: name field of rec contains key value)
*/
int status;
/*??*/
return(status);
} // remove
public void dumphash()
{
/*
Prints out a snapshot of the hash table data values
*/
int i;
DecimalFormat fmt = new DecimalFormat("####0.0");
System.out.println();
System.out.println("Dump of hash table:");
System.out.println(" avail: " + avail);
for (i = 0; i < MAXHASH; i++)
{
System.out.print(pad(Integer.toString(i),4,'R') +
pad(Integer.toString(htbl[i]),3,'R'));
if (i < MAXRECS)
{
System.out.print
(
pad(Integer.toString(i),6,'R') + " " +
pad(rectbl[i].rec.name,15,'L') +
pad(Integer.toString(rectbl[i].rec.age),5,'R') +
pad(fmt.format(rectbl[i].rec.weight),7,'R') +
pad(Integer.toString(rectbl[i].next),4,'R')
);
}
System.out.println();
}
} // dumphash
public String pad(String instr, int width, char justify)
/*
This function will left or right justify the input string in an
output string of length "width" by adding leading or trailing
blanks to the string as necessary.
*/
{
StringBuffer outstr = new StringBuffer(instr);
while (outstr.length() < width)
{
if (justify == 'L') outstr.append(' ');
else outstr.insert(0,' ');
}
return(outstr.toString());
} // pad
} // class hashtbl
class Prog08
{
public static void main(String[] args) throws IOException
{
/*
This function reads a data file named hash.dat that contains a number
of transactions to test the operation of the hash table machinery
implemented by the hashtbl class.
*/
int status;
String line;
String[] t;
char option;
String name;
int age;
float weight;
hashtbl tbl = new hashtbl();
rectype rec = new rectype();
DecimalFormat fmt = new DecimalFormat("####0.0");
// initialize
BufferedReader br = new BufferedReader(new FileReader("hash.dat"));
// process transactions
while ((line = br.readLine()) != null)
{
System.out.println(line);
t = line.split("\\s+");
option = t[0].charAt(0);
name = t[1]; age = 0; weight = 0;
if (option == 'I')
{
age = Integer.parseInt(t[2]);
weight = Float.parseFloat(t[3]);
}
switch (option)
{
case 'I':
rec.name = new String(name);
rec.age = age;
rec.weight = weight;
status = tbl.insert(rec);
System.out.println(" status: " + status);
if (name.compareTo("KOOL") == 0) tbl.dumphash(); /**/
break;
case 'F':
rec.name = new String(name);
status = tbl.find(rec);
System.out.println(" status: " + status);
if (status == 0)
{
System.out.println(" name: " + rec.name +
" age: " + rec.age +
" weight: " + fmt.format(rec.weight));
}
break;
case 'R':
status = tbl.remove(name);
System.out.println(" status: " + status);
if (name.compareTo("BARKER") == 0) tbl.dumphash(); /**/
break;
}
}
// finalize
br.close();
} // main
} // class Prog08
Mon Oct 27, 2008 11:18 am
Mon Oct 27, 2008 4:57 pm
Mon Oct 27, 2008 5:34 pm
Mon Nov 10, 2008 10:42 am
1 **************************************************************************
2 // Timertest.java
3
4 import java.io.*;
5 import java.util.*;
6 import java.text.*;
7
8 class Timer
9 {
10 private long starttick;
11
12 public void StartTimer()
13 // This function starts the timer
14 {
15 starttick = System.currentTimeMillis();
16 } // StartTimer
17 public double ElapsedSeconds()
18 /*
19 This function returns the number of seconds that have elapsed
20 (to the nearest millisecond) since the StartTimer function was
21 called.
22 */
23 {
24 return((System.currentTimeMillis() - starttick) / 1000.0);
25 } // ElapsedSeconds
26 } // class Timer
27 public class Timertest
28 {
29 public static void main(String[] args) throws IOException
30 // This function tests the functionality of the Timer class
31 {
32 int inbyte;
33 long mscount;
34 double seconds1,seconds,seconds3;
35 Timer t = new Timer();
36 DecimalFormat fmt = new DecimalFormat("###0.000");
37
38 System.out.println(
39 "Press one of the letters below, followed by Enter:");
40 System.out.println(" r = run timer");
41 System.out.println(" b = begin timer");
42 System.out.println(" s = stop timer");
43 System.out.println(" q = quit program");
44 do
45 {
46 do
47 {
48 inbyte = System.in.read();
49 } while (inbyte != 'b' && inbyte != 'q' && inbyte != 'r');
50 if (inbyte == 'r')
51 {
52 mscount = System.currentTimeMillis();
53 while (System.currentTimeMillis() - mscount < 50)
54 System.out.println(System.currentTimeMillis());
55 }
56 else if (inbyte != 'q')
57 {
58 System.out.println("Timing...");
59 t.StartTimer();
60 do
61 {
62 inbyte = System.in.read();
63 } while (inbyte != 's' && inbyte != 'q');
64 seconds = t.ElapsedSeconds();
65 System.out.println("Elapsed seconds: " + fmt.format(seconds));
66 }
67 } while (inbyte != 'q');
68 System.out.println("End of program.");
69 } // main
70 } // class Timertest
71 **************************************************************************
72 // Randomtest.java
73
74 import java.util.*;
75
76 public class Randomtest
77 {
78 public static void fillArray(int[] a)
79 /*
80 This function will fill an array with the numbers from 1 to
81 a.length and then will shuffle those numbers into a random
82 sequence.
83 */
84 {
85 int i,j,temp;
86 Random randnum = new Random();
87
88 for (i = 0; i < a.length; i++) a[i] = i + 1;
89 for (i = 0; i < a.length; i++)
90 {
91 j = randnum.nextInt(a.length);
92 temp = a[j]; a[j] = a[i]; a[i] = temp;
93 }
94 } // fillArray
95 public static void main(String[] args)
96 // This function tests the fillArray function
97 {
98 int i;
99 int[] a = new int[10];
100
101 fillArray(a);
102 System.out.println("Random shuffle of numbers from 1 to " + a.length);
103 for (i = 0; i < a.length; i++)
104 {
105 System.out.println(" "+a[i]);
106 }
107 } // main
108 } // class Randomtest
109 **************************************************************************
110 BUBBLE SORT EXAMPLE
111 **************************************************************************
112 The following illustrates how to translate pseudocode for the bubble sort
113 algorithm into Java code:
114 **************************************************************************
115
116 void bubblesort(int[] a, int n)
117 {
118 int i,j;
119 int temp;
120
121 for (i = 1; i < n; i++)
122 {
123 for (j = n-1; j >= i; j--);
124 {
125 if (a[j-1] > a[j])
126 {
127 temp = a[j-1];
128 a[j-1] = a[j];
129 a[j] = temp;
130 }
131 }
132 }
133 } // bubblesort
134
135 public static void main(String[] args)
136 {
137 final int MAX = 10000;
138 int n;
139 int[] a = new int[MAX];
140 double seconds;
141 Timer t = new Timer();
142
143 ...
144 fillArray(a);
145 ...
146 t.StartTimer();
147 bubblesort(a,n);
148 seconds = t.ElapsedSeconds();
149 ...
150 } // main
151 **************************************************************************
Mon Nov 10, 2008 12:15 pm
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.