gpt4 book ai didi

java - 打印多个类

转载 作者:行者123 更新时间:2023-12-01 14:35:52 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com