java - 打印多个类

标签 java methods printing chaining

我不知道为什么我们的教授让我们做这种奇怪的打印,但它是为了我们的期末考试,一小时后到期,我一直试图让它永远运行,但我哪儿也去不了。所以我有四个类:MyWord、Section、Page 和 Newspaper。 MyWord 只是 MyWord。节包含节和 MyWord。页面包括部分、页面和MyWord。报纸上都包含了它们。我有一个主要方法,只打印您输入的信息。这是提供的唯一示例。我的所有代码都包含在类(class)中,以表明我实际上已经完成了工作并且需要帮助。谢谢。

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(" ");
    }
}

最佳答案

我认为你应该将数组设置为 MyWord 而不是 Word,所以:

MyWord[] w = new MyWord[3];

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

关于java - 打印多个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16493434/

相关文章:

java - 如何在 OAuth 2 中向 JWT token 添加环境

java - 这段代码是否正确使用了 ReentrantReadWriteLock?

java - 对匿名内部类使用最终的 1 元素数组

methods - 页面重用 SqlConnection 的正确方法

java - java.util.Collections.reverse() 如何工作?

java - 比较两个对象时 JUnit assertEquals() 不起作用

Java toString() 方法不适用于我的类

c# - 与打印机交谈

linux - 网络打印机不接受来自 Debian Linux 的作业,error_log 中没有错误

java - 不使用反射访问类的字段?