Java Homework
Soundreader/.classpath
Soundreader/.project
Soundreader org.eclipse.jdt.core.javabuilder org.eclipse.jdt.core.javanature
Soundreader/.settings/org.eclipse.jdt.core.prefs
eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve org.eclipse.jdt.core.compiler.compliance=1.8 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.source=1.8
Soundreader/bin/ArrayStack.class
public synchronized class ArrayStack { private Object[] data; private int size; public void ArrayStack(); public boolean isEmpty(); protected boolean isFull(); public Object peek(); public Object pop(); public void push(Object); protected void stretch(); }
Soundreader/bin/DStack.class
public abstract interface DStack { public abstract boolean isEmpty(); public abstract void push(double); public abstract double pop(); public abstract double peek(); }
Soundreader/bin/gtd/stack/ListStackNode.class
package gtd.stack; public final synchronized class ListStackNode { private static final EpsilonStackNode EMPTY; private final String nodeName; private final AbstractStackNode[] children; private final AbstractStackNode emptyChild; public void ListStackNode(int, int, AbstractStackNode, String, boolean); private void ListStackNode(ListStackNode, int); AbstractStackNode[] generateChildren(AbstractStackNode); private AbstractStackNode generateEmptyChild(); public String getName(); public AbstractStackNode getCleanCopy(int); public AbstractStackNode[] getChildren(); public boolean canBeEmpty(); public AbstractStackNode getEmptyChild(); public String toString(); }
Soundreader/bin/ListStack.class
public synchronized class ListStack { private ListNode topOfStack; public void ListStack(); public boolean isEmpty(); public void makeEmpty(); public void push(Object); public void pop(); public Object top(); public Object topAndPop(); }
Soundreader/bin/Reverse.class
public synchronized class Reverse { public void Reverse(); public static void main(String[]); }
Soundreader/Screenshot (14).png
Soundreader/Screenshot (15).png
Soundreader/Screenshot (16).png
Soundreader/src/ArrayStack.java
Soundreader/src/ArrayStack.java
import
java
.
util
.
EmptyStackException
;
public
class
ArrayStack
<
E
>
implements
DStack
<
E
>
{
/** Array of items in this Stack. */
private
E
[]
data
;
/** Number of items currently in this Stack. */
private
int
size
;
/** The Stack is initally empty. */
public
ArrayStack
()
{
data
=
(
E
[])(
new
Object
[
1
]);
// This causes a compiler warning
size
=
0
;
}
public
boolean
isEmpty
()
{
return
size
==
0
;
}
/** Return true if data is full. */
protected
boolean
isFull
()
{
return
size
==
data
.
length
;
}
public
E peek
()
{
if
(
isEmpty
())
{
throw
new
EmptyStackException
();
}
return
data
[
size
-
1
];
}
public
E pop
()
{
if
(
isEmpty
())
{
throw
new
EmptyStackException
();
}
size
--
;
return
data
[
size
];
}
public
void
push
(
E target
)
{
if
(
isFull
())
{
stretch
();
}
data
[
size
]
=
target
;
size
++
;
}
/** Double the length of data. */
protected
void
stretch
()
{
E
[]
newData
=
(
E
[])(
new
Object
[
data
.
length
*
2
]);
// Warning
for
(
int
i
=
0
;
i
<
data
.
length
;
i
++
)
{
newData
[
i
]
=
data
[
i
];
}
data
=
newData
;
}
}
}
Soundreader/src/DStack.java
Soundreader/src/DStack.java
/**
* Interface for a stack of primitive doubles.
*
@version
*
* NOTE: The comments for this interface are horrible! You will
* need to write something better for your implementations.
*/
public
interface
DStack
{
/**
* is empty?
*/
public
boolean
isEmpty
();
/**
* push
*/
public
void
push
(
double
d
);
/**
* pop
*
@return
the deleted value
*
@throws
EmptyStackException if stack is empty
*/
public
double
pop
();
/**
* peek
*
@throws
EmptyStackException if stack is empty
*/
public
double
peek
();
}
Soundreader/src/gtd/stack/ListStackNode.java
Soundreader/src/gtd/stack/ListStackNode.java
package
gtd
.
stack
;
public
final
class
ListStackNode
extends
AbstractExpandableStackNode
{
private
final
static
EpsilonStackNode
EMPTY
=
new
EpsilonStackNode
(
DEFAULT_LIST_EPSILON_ID
,
0
);
private
final
String
nodeName
;
private
final
AbstractStackNode
[]
children
;
private
final
AbstractStackNode
emptyChild
;
public
ListStackNode
(
int
id
,
int
dot
,
AbstractStackNode
child
,
String
nodeName
,
boolean
isPlusList
){
super
(
id
,
dot
);
this
.
nodeName
=
nodeName
;
this
.
children
=
generateChildren
(
child
);
this
.
emptyChild
=
isPlusList
?
null
:
generateEmptyChild
();
}
private
ListStackNode
(
ListStackNode
original
,
int
startLocation
){
super
(
original
,
startLocation
);
nodeName
=
original
.
nodeName
;
children
=
original
.
children
;
emptyChild
=
original
.
emptyChild
;
}
private
AbstractStackNode
[]
generateChildren
(
AbstractStackNode
child
){
AbstractStackNode
listNode
=
child
.
getCleanCopy
(
DEFAULT_START_LOCATION
);
listNode
.
markAsEndNode
();
listNode
.
setProduction
(
new
AbstractStackNode
[]{
listNode
,
listNode
});
return
new
AbstractStackNode
[]{
listNode
};
}
private
AbstractStackNode
generateEmptyChild
(){
AbstractStackNode
empty
=
EMPTY
.
getCleanCopy
(
DEFAULT_START_LOCATION
);
empty
.
setProduction
(
new
AbstractStackNode
[]{
empty
});
empty
.
markAsEndNode
();
return
empty
;
}
public
String
getName
(){
return
nodeName
;
}
public
AbstractStackNode
getCleanCopy
(
int
startLocation
){
return
new
ListStackNode
(
this
,
startLocation
);
}
public
AbstractStackNode
[]
getChildren
(){
return
children
;
}
public
boolean
canBeEmpty
(){
return
(
emptyChild
!=
null
);
}
public
AbstractStackNode
getEmptyChild
(){
return
emptyChild
;
}
public
String
toString
(){
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
nodeName
);
sb
.
append
(
getId
());
sb
.
append
(
'('
);
sb
.
append
(
startLocation
);
sb
.
append
(
')'
);
return
sb
.
toString
();
}
}
Soundreader/src/ListStack.java
Soundreader/src/ListStack.java
// ListStack class
//
// CONSTRUCTION: with no initializer
//
// ******************PUBLIC OPERATIONS*********************
// void push( x ) --> Insert x
// void pop( ) --> Remove most recently inserted item
// AnyType top( ) --> Return most recently inserted item
// AnyType topAndPop( ) --> Return and remove most recent item
// boolean isEmpty( ) --> Return true if empty; else false
// void makeEmpty( ) --> Remove all items
// ******************ERRORS********************************
// top, pop, or topAndPop on empty stack
public
class
ListStack
<
AnyType
>
implements
DStack
<
AnyType
>
{
/**
* Construct the stack.
*/
public
ListStack
(
)
{
topOfStack
=
null
;
}
/**
* Test if the stack is logically empty.
*
@return
true if empty, false otherwise.
*/
public
boolean
isEmpty
(
)
{
return
topOfStack
==
null
;
}
/**
* Make the stack logically empty.
*/
public
void
makeEmpty
(
)
{
topOfStack
=
null
;
}
/**
* Insert a new item into the stack.
*
@param
x the item to insert.
*/
public
void
push
(
AnyType
x
)
{
topOfStack
=
new
ListNode
<
AnyType
>
(
x
,
topOfStack
);
}
/**
* Remove the most recently inserted item from the stack.
*
@throws
UnderflowException if the stack is empty.
*/
public
void
pop
(
)
{
if
(
isEmpty
(
)
)
throw
new
UnderflowException
(
"ListStack pop"
);
topOfStack
=
topOfStack
.
next
;
}
/**
* Get the most recently inserted item in the stack.
* Does not alter the stack.
*
@return
the most recently inserted item in the stack.
*
@throws
UnderflowException if the stack is empty.
*/
public
AnyType
top
(
)
{
if
(
isEmpty
(
)
)
throw
new
UnderflowException
(
"ListStack top"
);
return
topOfStack
.
element
;
}
/**
* Return and remove the most recently inserted item
* from the stack.
*
@return
the most recently inserted item in the stack.
*
@throws
UnderflowException if the stack is empty.
*/
public
AnyType
topAndPop
(
)
{
if
(
isEmpty
(
)
)
throw
new
UnderflowException
(
"ListStack topAndPop"
);
AnyType
topItem
=
topOfStack
.
element
;
topOfStack
=
topOfStack
.
next
;
return
topItem
;
}
private
ListNode
<
AnyType
>
topOfStack
;
}
Soundreader/src/Reverse.java
Soundreader/src/Reverse.java
import
java
.
io
.
BufferedReader
;
import
java
.
io
.
BufferedWriter
;
import
java
.
io
.
FileReader
;
import
java
.
io
.
FileWriter
;
import
java
.
io
.
IOException
;
import
java
.
io
.
PrintWriter
;
import
java
.
util
.
StringTokenizer
;
/**
* Read a .dat file and reverse it.
*
@version
*/
public
class
Reverse
{
public
static
void
main
(
String
[]
args
)
{
if
(
args
.
length
!=
3
)
{
System
.
err
.
println
(
" Incorrect number of arguments"
);
System
.
err
.
println
(
" Usage: "
);
System
.
err
.
println
(
"\tjava Reverse <stack type> <input file> <output file>"
);
System
.
exit
(
1
);
}
boolean
useList
=
true
;
if
(
args
[
0
].
compareTo
(
"list"
)
==
0
)
useList
=
true
;
else
if
(
args
[
0
].
compareTo
(
"array"
)
==
0
)
useList
=
false
;
else
{
System
.
err
.
println
(
"\tSaw "
+
args
[
0
]
+
" instead of list or array as first argument"
);
System
.
exit
(
1
);
}
try
{
//
// Set up the input file to read, and the output file to write to
//
BufferedReader
fileIn
=
new
BufferedReader
(
new
FileReader
(
args
[
1
]));
PrintWriter
fileOut
=
new
PrintWriter
(
new
BufferedWriter
(
new
FileWriter
(
args
[
2
])));
//
// Read the first line of the .dat file to get sample rate.
// We want to store the sample rate value in a variable,
// but we can ignore the "; Sample Rate" part of the line.
// Step through the first line one token (word) at a time
// using the StringTokenizer. The fourth token is the one
// we want (the sample rate).
//
StringTokenizer
str
;
String
oneLine
;
int
sampleRate
;
String
strJunk
;
oneLine
=
fileIn
.
readLine
();
str
=
new
StringTokenizer
(
oneLine
);
strJunk
=
str
.
nextToken
();
// Read in semicolon
strJunk
=
str
.
nextToken
();
// Read in "Sample"
strJunk
=
str
.
nextToken
();
// Read in "Rate"
// Read in sample rate
sampleRate
=
Integer
.
parseInt
(
str
.
nextToken
());
//
// Read in the remainder of the file on line at a time.
// The values in the first column are thrown away.
// Place values from the second column on the stack.
// Stop reading if we reach the end of the file.
//
DStack
s
;
if
(
useList
)
s
=
new
ListStack
();
else
s
=
new
ArrayStack
();
String
timestep
;
double
data
;
int
count
=
0
;
while
((
oneLine
=
fileIn
.
readLine
())
!=
null
)
{
if
(
oneLine
.
charAt
(
0
)
==
';'
)
{
continue
;
}
str
=
new
StringTokenizer
(
oneLine
);
// Read in time step value from first column
timestep
=
str
.
nextToken
();
// Read in data value from second column
data
=
Double
.
parseDouble
(
str
.
nextToken
());
s
.
push
(
data
);
count
++
;
}
System
.
out
.
println
(
count
+
" samples in file"
);
//
// Print the data values to output .dat file.
// First, output the header line:
// "; Sample Rate <sample rate>"
//
fileOut
.
println
(
"; Sample Rate "
+
sampleRate
);
// Since the first column consists of numbers which start
// at 0 and increase by 1/sampleRate every time slice, we'll
// just use numSteps to recalculate these numbers.
int
numSteps
=
0
;
// Finally, we print the values in reverse order (by popping
// them off the stack). The first column consists of numbers
// which start at 0 and increase by 1/sampleRate per row, so
// we'll use numSteps/sampleRate to recalculate the appropriate
// values. Print a tab for uniform spacing.
while
(
!
s
.
isEmpty
())
{
fileOut
.
println
((
double
)
numSteps
/
sampleRate
+
"\t"
+
s
.
pop
());
numSteps
++
;
}
//
// Close the files
//
fileIn
.
close
();
fileOut
.
close
();
}
catch
(
IOException
ioe
)
{
System
.
err
.
println
(
"Error opening/reading/writing input or output file."
);
System
.
exit
(
1
);
}
catch
(
NumberFormatException
nfe
)
{
System
.
err
.
println
(
nfe
.
toString
());
System
.
err
.
println
(
"Error in file format"
);
System
.
exit
(
1
);
}
}
}