第一题:
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...}
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...}
解决方案 »
- Connection对象close()以后,是不是Statement对象自动也关闭了??
- 小弟跪求 当数据库内容更新的时候 jtable 内容实时更新
- 求《Java参考大全(J2SE5 Edition)》书上源码下载地址
- 谁能跟我细讲讲这3种同步的区别
- 这个 java.lang.NullPointerException 是什么原因啊?
- 各位高手,我使用ECLIPSE开发JSP时,测试时特别麻烦,有什么好的办法吗?
- 大家来讨论一下发展方向!!!
- 在java中只用一个Connection ,如何保证我执行多条sql语句,其中有几条失败而不回滚全部,我只需要让他回滚这几条
- java.sql.SQLException: 試圖使用已關閉的陳述式。什麼時候會產生這個錯誤?
- 关于java编码规范
- hufman编码 java版本
- 监听器问题 java实现网站在线人员名单的如何区分开?
this.first = first;
this.last = last;
}
第二个我觉得没问题
在first和last没有附已字符串时,
public String toString() {
return "Name is: "+first+" "+last;
}
会返回"Name is: null null"; 第二题:
计算时没有对numberOfMolecules做非0chack,另外计算后可能会超过double的范围。以上是我的一点看法,可能还有别的问题,但我没能力继续找了。
public Name(String first, String last) {
this.first = first;
this.last = last;
}
附初始值的。另外,我上面的打错了字母,不是"非0chack"是“非0的check”
@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()才能实现?以上是我的个人看法,肯定很多不对。自己也不是很明白,大家一起讨论。
long 和double的位数不同
这些常量应该是外部不可见的建议定义在类中或者使用枚举
还是c好
private final String first,last;
此行错了因为在将一个变量定义为final型是必须将它初始化
一个final型的变量就相当是一个常量所以在定好之后不能改变。第二个题目没错