0

我不知道为什么我们的教授让我们做这种奇怪的打印,但它是为了我们的期末考试,一个小时后到期,我一直试图让它永远运行,但我什么也做不到。所以我有四个类,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(" ");
    }
}
4

1 回答 1

1

我认为您应该将数组设置为 MyWord 而不是 Word,因此:

我的字[] w = 新的我的字[3];

那么据我所知,它可能有效吗?你能说当你尝试运行/编译它时会发生什么吗?

于 2013-05-11T04:15:26.350 回答