第一题:
Is there something wrong with this class (follows)? If yes, how would you fix it?public final class Name {
    private final String first, last;    public Name(String first, String last) {
        this.first = first;
        this.last = last;
    }    /**
     * @return Returns the first.
     */
    public String getFirst() {
        return first;
    }    /**
     * @return Returns the last.
     */
    public String getLast() {
        return last;
    }      @Override
    public boolean equals(Object obj) {        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final Name other = (Name) obj;
        if (this.first == null) {
            if (other.first != null)
                return false;
        }
        else if (!this.first.equals(other.first))
            return false;
        if (this.last == null) {
            if (other.last != null)
                return false;
        }
        else if (!this.last.equals(other.last))
            return false;
        return true;
    }    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return "Name is: "+first+" "+last;
    }}
第二题:
Is there something wrong with the following design of an interface? If yes, what is it?interface PhysicalConstants {
    // Avogadro's number (1/mol)
    static final double AVOGADROS_NUMBER = 6.02214199e23;    // Boltzmann constant (J/K)
    static final double BOLTZMANN_CONSTANT = 1.3806503e-23;    // Mass of the electron (kg)
    static final double ELECTRON_MASS = 9.10938188e-31;
}public class PhysicalMethods implements PhysicalConstants {    public double getIdealGasVolume(double pressure, long numberOfMolecules,
            double temperature) {
        return ((numberOfMolecules / AVOGADROS_NUMBER) * AVOGADROS_NUMBER
                * BOLTZMANN_CONSTANT * temperature)
                / pressure;
    }    // some other methods doing physical calculations go here...}

解决方案 »

  1.   

    第一个 这个地方错了 我个人认为  因为 first 和last都是final类型的  不可以改变的public   Name(String   first,   String   last)   { 
                    this.first   =   first; 
                    this.last   =   last; 
            } 
    第二个我觉得没问题
      

  2.   

    第一题: 
    在first和last没有附已字符串时, 
            public   String   toString()   { 
                    return   "Name   is:   "+first+"   "+last; 
            } 
    会返回"Name   is:   null   null"; 第二题: 
    计算时没有对numberOfMolecules做非0chack,另外计算后可能会超过double的范围。以上是我的一点看法,可能还有别的问题,但我没能力继续找了。
      

  3.   

    我的看法与2楼不同,final的变量是可以通过
    public       Name(String       first,       String       last)       {   
                                    this.first       =       first;   
                                    this.last       =       last;   
                    }   
    附初始值的。另外,我上面的打错了字母,不是"非0chack"是“非0的check”
      

  4.   

    第一题感觉重载equal不需要那么麻烦:
     @Override
        public boolean equals(Object obj) {
        Name ne = null;
            if (obj instanceof Name)
                ne = (Name)obj;
            else return false;
            if ((ne.first == this.first)&&(ne.last == this.last))
                return true;
            else return false;
        }
    第三题:
    1.接口内的成员变量修饰符static final是不是可以去掉?接口默认是成员是public static final的。
    2.实现接口是不是接口内要有成员方法getIdealGasVolume()才能实现?以上是我的个人看法,肯定很多不对。自己也不是很明白,大家一起讨论。 
      

  5.   

    问题太多了,楼主放到eclipse就一目了然了,另外发代码的时候用上csdn的源代码识别器,你这个看起来太不舒服了
      

  6.   

    第2题没错啊
    long 和double的位数不同 
      

  7.   

    第二题既然提到design,那么就不要在interface里定义常量
    这些常量应该是外部不可见的建议定义在类中或者使用枚举
      

  8.   

    每想到java的代码又臭有长啊!
    还是c好
      

  9.   

    第一个题目错了:
       private final String first,last; 
       此行错了因为在将一个变量定义为final型是必须将它初始化
    一个final型的变量就相当是一个常量所以在定好之后不能改变。第二个题目没错