我应该编写一个 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.
接下来,我应该在我的 TocEntry 类中开发必要的代码,以读取章节名称和页码,直到输入“<code>**”。由此,我的输出应如下所示:
我的故事开始......................................1
成长..................................35
征服世界............103
这应该是我使用名为 useTocEntry 的驱动程序运行的示例
输入章节标题:Camelot
输入起始页码:1
输入章节标题:亚瑟王宫廷
输入起始页码:3
输入章节标题:圆桌骑士
输入起始页码:8
输入章节标题:幽默家迪纳丹爵士
输入起始页码:12
输入章节标题:灵感
输入起始页码:14
输入章节标题:The Eclipse
输入起始页码:23
输入章节标题:克拉伦斯的后记
输入起始页码: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
我需要帮助,尤其是创建一个**
与章节标题和页码对齐的方法。对我的代码的任何帮助或建议将不胜感激。