★★ Java の宿題ここで答えます Part 67 ★★

このエントリーをはてなブックマークに追加
270デフォルトの名無しさん
【 課題 】Stack ADT 、array 、linked list の実装
http://hp3.blog42.fc2.com/
閲覧パスワード: java
【 形態 】1. Javaアプリケーション(main()で開始)
         Eclipse
【 GUI  】4. 制限なし
【 期限 】 締切日        Apr.08 : 03:00 am 日本時間 
       ドラフト解答希望日 Apr.03 : 03:00 am 日本時間
       1)(必須)ArrayStack.java  途中でもいいです。Main Interfaceとつながっていれば
       2)(必須)クラスディスカッション用の質問(ネタ)
        ‐ソースの中のポイントとなる箇所と、なぜそうしたかなど。
         普通のコメントのような説明でいいです。
        ‐Writeup Questions の答えでわかるところがあれば。  
       3.(できれば)Eclipseでテスト可能なドライバの作成
         わかればお願いします。こうすればよい、的なのでもいいです。
【 Ver  】java version "1.6.0_13"
【 補足 】丸投げです。急で申し訳ないのですが不完全でいいのでお願いします。
データ構造クラスの宿題です。ライブラリを使用します。
ファイルをアップロードする方法がなかったので、
宿題で提供される2つのクラスを貼り付けます。
DStack.java
Reverse.java(main)
bot.wav, bot.dat.の二つのファイルも提供されたのですが、
必要であれば、アップロードする場所教えていただければ
そこにアップロードします。
271デフォルトの名無しさん:2009/04/02(木) 16:40:57
DStack.java
/**
* Interface for a stack of primitive doubles.
* @version CSE326 Sp09
*
* NOTE: The comments for this interface are horrible! You will
* need to write something better for your implementations.
*/
interface DStack {
/**
* is empty?
*/
boolean isEmpty();
/**
* push
*/
void push(double d);
/**
* pop
* @return the deleted value
* @throws EmptyStackException if stack is empty
*/
double pop();
/**
* peek
* @throws EmptyStackException if stack is empty
*/
double peek();
}
272デフォルトの名無しさん:2009/04/02(木) 16:49:35
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 CSE326, Wi09
*/
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);
}
273デフォルトの名無しさん:2009/04/02(木) 16:51:55
Reverse.java(2)行数オーバーしたので...
try {
//
// Set up the input file to be read, and the output file to be written
//
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. We want to store the
// "sample rate" in a variable, but we can ignore the rest
// of the line. We 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"
sampleRate = Integer.parseInt(str.nextToken()); // Read in sample rate
274デフォルトの名無しさん:2009/04/02(木) 16:53:03
Reverse.java(3)行数オーバーしたので...

//
// Read in the file and place values from the second column
// in the stack. The first column values are thrown away.
// We 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);
timestep = str.nextToken(); // Read in time step value from first column
data = Double.parseDouble(str.nextToken()); // Read in data value from second column
s.push(data);
count++;
}

System.out.println(count+" samples in file");
275デフォルトの名無しさん:2009/04/02(木) 16:54:54
Reverse.java(4)行数オーバーしたので...
//
// Now we are ready to output the data values to output file.
// But first, we need to 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. Uniform spacing will be accomplished by printing a tab.

while (!s.isEmpty()) {
fileOut.println((double) numSteps / sampleRate + "\t" +
s.peek());
s.pop();
numSteps++;
}
276デフォルトの名無しさん:2009/04/02(木) 16:55:39
Reverse.java(5)最後です
//
// 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);
}
}
}