2013-03-17 104 views
-1
  1. 我有一个带有两个类Lecturer和Subject的时间表项目,还指定了已知对象。一种可以创建对象的类方法

  2. ,他们都必须在对象的数组或ArrayList中

  3. ,项目应让用户能够创建或添加,删除,搜索和类讲师的更新对象。

我真的不知道如何去了解这一点,我可以创建一个可以做这个任务上面它应该是一个类的方法,或在主类中的方法的方法?

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

我创建了类,对象和ArrayList我坚持

对不起我能也可以由用户到ArrayList中添加创建的对象是,甚至有可能?

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; 


} 


} 
+7

我们无法真正为你做你的工作。我们能做的就是看看你已经尝试过的代码并就事情提供建议。所以,如果你可以请发布代码,你尝试创建一个新的对象。 – 2013-03-17 17:55:11

+0

继承人的代码.... Thankxxx – adrain 2013-03-17 18:09:31

+2

而不是只是downvoting和关闭,我们如何给这个新用户有机会先解决他的问题? – 2013-03-17 18:10:00

回答

1

应该 '主体' 是一个子类的“讲师?

如果是这样,你可以在下面的方式您的主要方法创建讲师的ArrayList:

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

+0

主题不是一个讲座的小孩类其自己的一个类!我的问题是,该项目是交互式的,用户应该能够在类讲师中键入变量的名称和所有其他值! – adrain 2013-03-17 18:26:27

+1

好的,这是一个控制台应用程序吗?你以前使用过actionlisteners吗? 把这个项目中你尚未完成的部分放在项目要点中,这样我就不会让你实施已经实施的东西。 – planty182 2013-03-17 18:29:57

+0

@ planty182:软件类习惯于表示真实生活中物体的关系。让'Subject'从'Lecturer'继承是一种非常直观的设计。数学应该永远不会像琼斯先生那样属于同一个数学家...... – thkala 2013-03-17 18:33:33

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

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

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

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

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

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

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

例如set(0,lec2)

希望这给你一个起点。

+0

thankx thanksx thanksx **像一个5岁的男孩**一样跳舞,我想把这些添加删除搜索的方法,所以我只是给他们打电话是可能的? – adrain 2013-03-17 18:40:40

+0

请我可能只需要设置和删除的例子,也包含~~~ thankxxx ... – adrain 2013-03-17 18:47:35

相关问题