次のデータ構造を使用して、目次を出力する Java プログラムを作成することになっています。
public class TocEntry
{
// Specify the needed methods
private String chapter;
private int page;
}
そして、私のドライバークラスで次のように定義されています:
public final int TOCSIZE = 100;
TocEntry toc[] = new TocEntry[TOCSIZE];
int toc_curlen = 0; //The toc_curlen is intended to keep track of the number of chapters entered by the user and it can be used as an index into the array of TocEntry objects.
次に、「<code>**」が入力されるまで章名とページ番号を読み込むために、TocEntry クラスで必要なコードを開発することになっています。これから、私の出力は次のようになります。
私の物語はじまる..................................................1
成長する....................................................35
世界征服……103
これは、useTocEntry というドライバーを使用して実行したサンプルです。
章のタイトルを入力してください: Camelot
開始ページ番号を入力してください: 1
章のタイトルを入力してください: King Arthur's Court
開始ページ番号を入力してください: 3
章のタイトルを入力してください: テーブルラウンドの騎士
開始ページ番号を入力してください: 8
章のタイトルを入力してください: ユーモリストのディナダン卿
開始ページ番号を入力してください: 12
章のタイトルを入力してください: インスピレーション
開始ページ番号を入力してください: 14
章のタイトルを入力してください: The Eclipse
開始ページ番号を入力してください: 23
章のタイトルを入力してください: A Postscript by Clarence
開始ページ番号を入力してください: 274
章のタイトルを入力してください:
**
キャメロット..................................................1
アーサー王の宮廷......................................3
テーブルラウンドのナイツ........8
サー・ディナダン・ザ・ユーモリスト.........12
ひらめき..................................................14
ザ・エクリプス..................................................23
クラレンスによるあとがき......................274
これは私がこれまでに持っているコードです:
import java.util.*;
public class TocEntry {
public TocEntry() { // Default Constructor
chapter = "";
page = 0;
}
public TocEntry(String c, int p) { // 2 Argument Constructor
chapter = c;
page = p;
}
public String getChapter() { //getChapter() and getPage() are accessor methods
return chapter;
}
public int getPage() {
return page;
}
public void setChapter(String title) { ////setChapter() and setPage() are mutator methods
chapter = title;
}
public void setPage(int numPage) {
page = numPage;
}
private String chapter;
private int page;
public String toString() { // toString method to print out contents
return chapter + "**" + page;
}
}// End of class TocEntry
そして、これは私のドライバークラスです:
import java.util.Scanner;
public class useToEntry {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of contents you would like to display: ");
int TOC_NUM = input.nextInt();
final int TOCSIZE = 100;
TocEntry toc[] = new TocEntry[TOCSIZE];
int toc_curlen = 0; // The toc_curlen is intended to keep track of the number
// of chapters entered by the user and it can be used as an
// index into the array of TocEntry objects.
for(int i = 0; i < TOC_NUM; i++) {
System.out.print("Enter chapter title: ");
String ch = input.next();
System.out.print("Enter starting page number: ");
int y = input.nextInt();
}// End of for loop
} // End of main method
}// End of class useTocEntry
**
特に、章のタイトルとページ番号を揃えるメソッドを作成するには、助けが必要です。私のコードに関するヘルプやアドバイスをいただければ幸いです。