您的位置:程序门 -> java -> j2se / 基础类



关于java.lang里的一个问题 需要回看类图 orz


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


关于java.lang里的一个问题 需要回看类图 orz[已结贴,结贴人:sthishere]
发表于:2007-06-10 08:09:56 楼主
做一道题目的时候有个几个方法不知道编写?请高手看过题目后再看看我写的指导下orz     问题有点长请见谅啊

题目:   (the   person   and   student   class)creat   the   class   as   shown   in   figure   ***(即待会我打的类图)

implement   the   compareto   method   in   the   person   class   to   compare   persons   in   the   compareto   methed   to   compare   students   in   alphabetical   order   of   their   major   of   their   major,last   name,first   name,and   middle   initial.
write   a   test   program   with   the   following   three   metheds
/**sort   an   arry   of   comparable   objects   */
        public   static   void   sort(object[]   list)
/**   return   the   max   object   in   an   array   of   comparable   objects*/
        public   static   object   max(object[]   list)
main   method:   test   the   sort   and   max   methed   using   an   array   of   four   students,   an   array   of   four   strings,   an   array   of   one   houndred   random   rationals,   and   array   of   one   houndred   random   integers.


类图:

person         ....△java.lang.comparable
_____________
-name:name
_____________
+person   (name:name)
+getname():name
+setname(name:name):void
+tostring():string
+equals(object:object):boolean
+comparto(object:object):int


name         -----◆person
____________________
-firstname:string
-mi:char
-lastname:string
____________________
+name(firstname:string,mi:char,lastname:string)
+getfirstname():string
+setfirstname(firstname:string):void
+getmi():char
+setmi(mi:char):void
+getlastname():string
+setlastname(lastname:string):void
+getfullname():string


student         -------(實綫)△person
_______________
-major:string
_______________
+student(name:name,major:string)
+getmajor():string
+setmajor(major:string):void
+tostring():string
+compareto(object:objext):int

以下是我编写的

public   class   name   {

private   string   firstname;
private   char   mi;
private   string   lastname;

        public   name()   {
       
        }
       
        public   string   getfirstname(){
       
        return   firstname;
        }
       
        public   void   setfirstname(string   firstname){
       
        this.firstname   =   firstname;
        }
       
        public   char   getmi(){
       
        return   mi;
        }
       
        public   void   setmi(char   mi){
       
        this.mi   =   mi;
        }
       
        public   string   getlastname(){
       
        return   lastname;
        }
       
        public   void   setlastname(string   lastname){
        this.lastname   =   lastname;
        }
       
        public   string   getfullname(){
        return   firstname+ "   "+mi+ "   "+lastname;
        }
}

_______________________________________________

public   class   person   implements   comparable{

private   name   name;

public   person(){

}
public   person(name   name){
this.name   =   name;
}

public   name   getname(){

return   name;
}

public   void   setname(){
this.name   =   name;
}
public   string   tostring(){
return   "\n "+name.getfullname();
}
/*下面这2个方法不知道该怎么写...*/
public   int   compareto(object   object){

return       ;
}

public   boolean   equals(object   object){

return       ;
}

}

________________________________________________
public   class   student   extends   person{

private   string   major;

public   student(string   name,   string   major){

}

public   string   getmajor(){

return   major;
}

public   void   setmajor(string   major){

}
/*同上面以下这个也不会*/
public   int   compareto(object   object){

return       ;
}

public   string   tostring(){

return   super.tostring()   +
"\nmajor: "   +   major;
}
}

请高手解答下吧
发表于:2007-06-10 08:45:141楼 得分:0
implement   the   compareto   method   in   the   person   class   to   compare   persons   in   the   compareto   methed   to   compare   students   in   alphabetical   order   of   their   major   of   their   major,last   name,first   name,and   middle   initial.
------
就是按字母比较major、lastname、firstname、middle   initial
都是字符串类型的,直接用string.compareto方法就可以搞定了
lastname、firstname、middle   initial这三个是name类的fields,直接给nameo   verride   compareto方法,怎么写参考下面的代码
//person   class
public   int   compareto(object   object){
person   p   =   (person)object;
int   tmp   =   major.compareto(p.major)
return   tmp <0?-1:(tmp> 0?   1   :   name.compareto(p.name));
}
发表于:2007-06-10 11:30:592楼 得分:0
看下   http://www.enet.com.cn/eschool/zhuanti/java   第6课,讲得很详细。

发表于:2007-06-10 11:53:053楼 得分:0
believefym(feng)
那equals方法怎么写呢?
发表于:2007-06-10 12:04:584楼 得分:0
int   tmp   =   major.compareto(p.major)
major是在student   class中的你的代码是在person   class中的无法直接调用啊
发表于:2007-06-10 12:18:055楼 得分:80
没看仔细,呵呵,
person类不要要考虑major,直接比较name,然后student的compareto直接比较major并调用父类person的compareto方法比较name

equals看你怎么定义两个实例是相等的
比如class   a{int   i;}如果i相等就让a的实例equals的话,equals就直接return   i==((a)obj).i
发表于:2007-06-10 12:27:496楼 得分:0
public   int   compareto(object   object){
person   p   =   (person)object;
int   tmp   =   name.compareto(p.name)
return   tmp <0?-1:(tmp> 0?   1   :   name.compareto(p.name));
直接调用?会报错compareto(name)没有在这里定义
如果用int   tmp   =   p.compareto(p.name)到是没有错,不知道对不对,对这些方法不懂老师都没讲还要我们做-   -
发表于:2007-06-10 12:39:097楼 得分:0
你要先定义name类的compareto,写法都类似的

就是name有自己的比较方法compareto,person只需要比较name,也就是直接调用name的compareto即可,student除了要比较name,还优先比较major,那就结合major的比较和name的比较,具体怎么实现楼主参考一下上面的代码然后自己看书再想想吧
发表于:2007-06-10 12:48:598楼 得分:0
但是类图上显示在name类中并没有compareto方法..
发表于:2007-06-10 12:54:429楼 得分:0
唉。。。
既然一定要按类图来,那就不要给name定义这个方法了,直接在person、student中自己比较name,其实可以通过name的fullname直接对这个fullname比较好了
发表于:2007-06-10 12:56:5110楼 得分:0
我再去研究研究


快速检索

最新资讯
热门点击