-1
  1. LecturerとSubjectの2つのクラスがあり、既知のオブジェクトも指定されたタイムテーブルプロジェクトがあります。

  2. そしてそれらはすべてオブジェクトまたはarraylistの配列に含まれている必要があります

  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

「対象」は「講師」の子クラスにする必要がありますか?

その場合、次の方法でメイン メソッドで講師の配列リストを作成できます。

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 オブジェクトを作成し、選択した List に講師を追加します。

remove講師の削除は、講師オブジェクトを取得して削除するメソッドを使用して簡単に行うことができます。例えば:

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

これにより、リストから最初の講師が削除されます。

講師を検索する場合containsと同様の方法でメソッドを使用できます。remove

最後に、Lecturer オブジェクトを更新するには、setメソッドを使用します。このメソッドは、Lecturer の詳細を更新するインデックスを取得し、Lecturer オブジェクトを新しい詳細で取得します。

例えばset(0,lec2)

これが出発点になることを願っています。

于 2013-03-17T18:23:15.050 に答える