-2

Hey all i have a quick question about objects of a class... im working on a hw assignment, i only want a hint in the right direction, not the whole answer.... basically we have five classes and there are 3 im working with.... the main class reads in a text file which im doing fine, the other is a Files.class Homework.class and a Name.class when i call a new homework i also create a new name new files

these are the methods that i have for creating a new homework

    homework.class
private int id;
private Name name;
private int section;
private Files files;
private int dateSubmitted;

public Homework(int id, Name name, int section, Files files,int dateSubmitted){
    this.id =id;
    this.name = name;
    this.section = section;         // initialize the homework to given params
    this.files = files;
    this.dateSubmitted = dateSubmitted;

}//end public hwk

public Homework(int id, Name name, int section, int dateSubmitted){
    this.id = id;
    this.name =name;        // the second constructor for the homework class
    this.section = section;
    this.dateSubmitted = dateSubmitted;
    this.files = null;
}// end second init homework

public Homework(String first, String last, int section, int dateSubmitted){ 
    this.id = nextAvailableUid();
    this.section = section;
    this.dateSubmitted = dateSubmitted;     
    this.name = new Name(first,last);
    this.files = null;
}

what im trying to do is pass in a first and last section and date which is the third HW method.......

my question is in the main class how do i add a file from the main..... or in main do i have to extend the file and name class and construct it from there and pass it in as a new homework?

ie in main

Homework []homework = new homework[size];
Files []files = new Files[size]; 
Name[]name = new Name[size];
//add appropriate code to fill in from here....

or is there an easier way in main to implement all the classes... other note im not allowed to modify Homework.class, name.class, or files.class

thanks in advance

4

2 回答 2

0

不太确定我是否正确理解了您的问题,但您可以为您的字段创建 setter//getter。

//Exemple for ID
private int id;
public int getId(){
  return id;
}
public void setId(int value){
  id = value;
}

并以这种方式使用它们:

Homework h = new Homework();
h.setId(valueToSet);
于 2013-10-17T02:48:58.753 回答
0

我认为您正在寻找的是导入语句。请参阅http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html

导入语句允许您让一个类的文件了解另一个类文件中的方法/字段。

但是这是不可能的,因为你的问题“在主类中我如何从主类中添加一个文件”没有任何意义。

于 2013-10-17T03:15:32.070 回答