【发布时间】:2013-05-11 03:26:18
【问题描述】:
我不知道为什么我们的教授让我们做这种奇怪的打印,但它是为了我们的期末考试,它会在一个小时后到期,我一直试图让它永远运行,但我无法到达任何地方。所以我有四个类,MyWord、Section、Page 和 Newspaper。 MyWord 就是 MyWord。 Section 包含 Section 和 MyWord。 Page 包括 Section、Page 和 MyWord。报纸包含了所有这些。我有一个主要方法,它只打印您输入的信息。这是提供的唯一示例。我的所有代码都包含在课程中,以表明我实际上已经完成了工作并需要帮助。谢谢。
Word[] w = new Word[3]; //example
w[0] = new Word(“hello”);
w[1] = new Word(“bob”);
w[2] = new Word(“smith”);
Section s = new Section(w, 20);
s.print();
//the above call to print should print off the following or something
//very similar to the following:
//Section 20: hello bob smith
my classes are also here
public class MyWord
{
private String word;
public MyWord(){ //constructor
word = "";
}
public MyWord(String word){ //using this keyword to assign word
this.word=word;
}
public String getWord(){ //accessor
return word;
}
public void setWord(String word){ //mutator
this.word=word;
}
public void print(){
System.out.print(word);
}
}
public class Section
{
private MyWord[] words; //taking in MyWord
private int sectionNumber;
public Section(){ //default constructor
words = new MyWord[0];
sectionNumber = 0;
}
public Section(MyWord[] m, int num){ //constructor
words = m;
sectionNumber = num;
}
public int getSectionNumber(){ //accessor
return sectionNumber;
}
public void setSectionNumber(int num){ //mutator
sectionNumber = num;
}
public MyWord[] getWords(){ //accessor
return words;
}
public void setWords(MyWord[] m){ //mutator
words = m;
}
public void print(){
System.out.print("Section " + sectionNumber + ": ");
for(int i =0; i <words.length; i++){
words[i].print();
System.out.print(" ");
}
}
}
public class Page
{
private Section[] sections; //taking in other class
private int pageNumber;
public Page(){ //setting defaults
sections = new Section[0];
pageNumber = 0;
}
public Page(Section[] m, int num){ //passing in sections[]
sections = m;
pageNumber = num;
}
public int getPageNumber(){ //accessor
return pageNumber;
}
public void setPageNumber(int num){ //mutator
pageNumber = num;
}
public Section[] getSections(){ //accessor
return sections;
}
public void setSections(Section[] m){ //mutator
sections = m;
}
public void print(){
System.out.print("Page " + pageNumber + ": ");
for(int i =0; i <sections.length; i++){
sections[i].print();
System.out.print(" ");
}
System.out.println(" ");
}
}
public class Newspaper
{
private Page[] pages; //taking in Page[]
private String title;
private double price;
public Newspaper(){
pages = new Page[0];
title = "Comp Sci Newspaper"; //default title
price = 2.50; //default price
}
public Newspaper(Page[] m, double num, String t){
pages = m;
price = num; //assigning values
title = t;
}
public String getTitle(){ //accessor
return title;
}
public void setTitle(String t){ //mutator
title = t;
}
public Page[] getPages(){ //accessor
return pages;
}
public void setPages(Page[] m){ //mutator
pages = m;
}
public double getPrice(){ //accessor
return price;
}
public void setPrice(double num){ //mutator
price = num;
}
public void print(){
System.out.print("Newspaper " + title + " " + "Price: " + price);
for(int i =0; i <pages.length; i++){
pages[i].print();
System.out.print(" ");
}
System.out.println(" ");
}
}
【问题讨论】:
-
我需要打印课程。报纸包含标题和价格和页面。然后页面将包含部分。部分将包含单词,它们都必须链接在一起
-
我知道这有点难以解释,但这里有一个例子。可以说它有 2 页。第 1 页有 3 个部分,第 2 页有 2 个部分。每个部分有三个单词。所以它会显示“报纸标题价格”第1页第1字123页1第2字123页1第3字123......第2页第1字123页2第2字123
-
一小时后到期?大声笑,你只是在这里浪费了你的宝贵时间
-
@user2372045 你的代码似乎没问题你面临的问题是什么?
标签: java methods printing chaining