-1
  1. 我有一个时间表项目,其中包含 Lecturer 和 Subject 两个类,还指定了已知对象。

  2. 并且它们都必须在对象数组或数组列表中

  3. 并且该项目应该允许用户能够创建或添加、删除、搜索和更新班级讲师的对象。

我真的不知道该怎么做我可以创建一个可以完成上述任务的方法,它应该是类方法还是主类中的方法?

如何让用户能够使用方法或任何其他方式创建对象?

我已经创建了类、对象和数组列表被卡住了

对不起,我也可以将用户创建的对象添加到数组列表中吗?

class Lecturer   {                
static int lec_id;                 
static String lec_name;
static String lec_rank;
static int lec_salary;
static String lec_department;

}

class Subject   {                  /* initialisation for the class subject*/
String sub_code;
String sub_name;
String sub_department;
int sub_lec_id;                 /*sub lec id represent the lecturer taking the subject*/
String sub_day;
String sub_time;
String sub_venue;

}

/* intialisation for the class lecturer*/

public class Timetable {

public static void main(String[] args) {
    /* the code below represent a created list of objects  */                       /* `Lecturer l =Lecturer ()(System.in);*/`


    Lecturer l1 =new Lecturer (1, "Dadson",  "Dr",   8000,  "Computing"); 
    Lecturer l2 =new Lecturer (2, "Hall",   "Prof", 12000,  "Computing");
    Lecturer l3 =new Lecturer (2, "Berry",   "Dr",   7500,  "Computing");
    Lecturer l4 =new Lecturer (2, "Bull",    "Dr",   9000,  "Engineering");
    Lecturer l5 =new Lecturer (2, "Viles",  "Prof", 13500,  "Engineering");
    Lecturer l6 =new Lecturer (2, "Dyson",   "Dr",   7200,  "Engineering");

    /*The code below represent the array list holding objects of the class Lecturer*/

   List<Lecturer> L=new ArrayList<Lecturer>();
   L.add(l1); 
   L.add(l2);
   L.add(l3);
   L.add(l4);
   L.add(l5);
   L.add(l6);




   /* the code below is to create the object for the class Subject*/ 
   Subject s1=new Subject ("EC3000", "Computing Basics",         "Computing",   1, "Mon",      "8-10am",  "S310");
   Subject s2=new Subject ("EE3000", "Engineering Basics",       "Engineering", 2, "Tuesday",  "10-12pm", "S310");
   Subject s3=new Subject ("EC3100", "Programming Fundamentals", "Engineering", 1, "Monday",   "1-3pm",   "S203");
   Subject s4=new Subject ("EE3100", "Engineeering Math",        "Computing",   4, "Thursday", "3-5pm",   "s420");
   Subject s5=new Subject ("EC3200", "Problem Solving",          "Computing",   3, "Friday",   "8-10pm",  "S208");
   Subject s6=new Subject ("EE3200", "Circuit",                  "Enginerring", 4, "Monday",   "10-12pm", "S104");
   Subject s7=new Subject ("EC3300", "Networking",               "Computing",   2, "Tuesday",  "1-3pm",   "S310");
   Subject s8=new Subject ("EE3300", "Electromagnetic",          "Engineering", 5, "Wednesday","1-3pm",   "S330");
   Subject s9=new Subject ("EC3400", "Project",                  "Computing",   3, "Thursday", "3-5pm",   "S312");
  Subject s10 =new Subject("EE3400", "Project",                  "Engineering", 6, "Tuesday",  "10-12pm", "S415");

  /*The code below represent the array list holding objects of the class Subject*/

  Subject[]S=new Subject[10];
  S[0]=s1;
  S[1]=s2;
  S[2]=s3;
  S[3]=s4;
  S[4]=s5;
  S[5]=s6;
  S[6]=s7;
  S[7]=s8;
  S[8]=s9;
  S[9]=s10;


} 


}
4

2 回答 2

1

“主题”应该是“讲师”的子类吗?

如果是这样,您可以通过以下方式在您的 main 方法中创建讲师数组列表:

ArrayList <Lecturer> al_lecturers = new ArrayList<Lecturer>();

//create 10 lecturers
for(int i = 0; i < 10; i++){
    al_lecturers.add(new Lecturer());
}

通过使用 ArrayList,您可以利用该类提供的add()remove()contains()方法来获得您所请求的一些功能。

我希望对你有一点帮助。

为了允许用户添加自己的讲师,您可以创建自己的方法来执行此操作:

private void addLecturer(/*lecturer info vars*/){
    al_lecturers.add(new Lecturer());
}

然后从用户输入此信息的位置调用此方法,大概是某种ActionListener

于 2013-03-17T18:17:23.103 回答
0

如果您想要一种允许您创建Lecturer对象的方法,这里是您如何实现该目标的一个示例(可以有其他方法)

public void createLecturer(int num, String name,String title,int num2,String department)
{
   Lecturer lec = new Lecturer(num,name,title,num2,department);
   aList.add(lec);
}

以上将从您传递给方法的参数创建一个新的 Lecturer 对象,并将讲师添加到您选择的列表中。

删除讲师可以简单地使用该remove方法完成,该方法接受 Lecturer 对象并将其删除。例如:

Lecturer lec = aList.get(0);
aList.remove(lec);

这将从列表中删除第一位讲师。

您可以contains以类似的方式使用该方法remove来搜索讲师。

最后,更新 Lecturer 对象是使用该set方法,该方法获取要更新 Lecturer 详细信息的索引,以及具有新详细信息的 Lecturer 对象。

例如set(0,lec2)

希望这能给你一个起点。

于 2013-03-17T18:23:15.050 回答