1. 下面Spot为描述点的类,其中midSpot用来求得两点中点。
1)请将midSpot的函数体补充完整。
2)编写完整的程序,求得点(3.5,7.5)与点(8.0,2.0)的中点。(要求另外定义一个带main的类)
package cn.whb.test;
public class Work01 {
public static void main(String[] args) {
Spot s1 = new Spot(3.5,7.5);
Spot s2 = new Spot(8.0,2.0);
Spot ss = s1.midSpot(s2);
System.out.println("X:"+ss.getX()+" Y:"+ss.getY());
}
}
class Spot {
private double x, y;
Spot (double u, double v) {
setX(u); setY(v);
}
void setX(double x1) {
x=x1;}
void setY(double y1) {
y=y1;}
double getX() {
return x;}
double getY() {
return y;}
Spot midSpot(Spot s){
// 求中点
double disx = (this.x + s.getX())/2;
double disy = (this.y + s.getY())/2;
Spot ss = new Spot(disx,disy);
return ss;
}
}
2. 阅读下面程序,编译运行之。然后将Frame和Label替换为swing中的对应组件实现相
同的功能。
import java.awt.*;
import java.awt.event.*;
public class HelloWorldAWT {
public static void main(String[] args) {
Frame f = new Frame("AWT1");
Label label = new Label("Hello!");
f.add(label);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.setSize(200, 200); f.setVisible(true);
}
}
package cn.whb.test;
import javax.swing.*;
import java.awt.event.*;
public class HelloWorldAWT {
public static void main(String[] args) {
// 创建JFrame窗口通用容器对象
JFrame f = new JFrame("AWT1");
// 创建标签对象
JLabel label = new JLabel("Hello!");
// 向窗口中添加内容
f.getContentPane().add(label);
// 添加监听器
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
// 设置窗口大小,将窗口可视化
f.setSize(200, 200); f.setVisible(true);
}
}
3. 相等之比较:
(1)
String s1=“abc”;
String s2=“abc”;
String s3=new String(“abc”);
String s4=new String(“abc”);
String s5=s4;
自编写程序,对s1与s2,s1与s3,s3与s4,s4与s5判断是否相等,分别用"=="和equals方法。
根据输出信息对比分析,并写出你的结论。
(2)
自定义一个简单类A,
并生成对象 A a1 = new A();
A a2 = new A();
A a3 = a1;
自编写程序,对a1与a2,a1与a3判断是否相等,分别用"=="和equals方法。
package cn.whb.test;
public class EQTest {
public static void main(String[] args) {
/**
* 第(1)题
*/
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
String s4 = new String("abc");
String s5 = s4;
// "==" 比较的是两个变量指向的是否是同一个引用
// s1 与 s2的指向的"abc"在常量区(方法区)只创建一次,所以s1,s2指向的是同一个
// s1 与 s3是两个对象用 "==" 是判断他们是否指向了同一个引用
// 上面s5 = s4是将s4指向的引用的地址赋值给了s5
// 所以此时s5和s4指向的是同一个引用,故为true
System.out.println("s1==s2? " + (s1==s2)); // true
System.out.println("s1==s3? " + (s1==s3)); // false
System.out.println("s3==s4? " + (s3==s4)); // false
System.out.println("s4==s5? " + (s4==s5)); // true
System.out.println("=======================");
// String类继承了Object类,所以也继承了equals方法
// String类中的equals方法只比较两个变量存储的值是否相同
System.out.println("s1.equals(s2)? " + s1.equals(s2)); // true
System.out.println("s1.equals(s3)? " + s1.equals(s3)); // true
System.out.println("s3.equals(s4)? " + s3.equals(s4)); // true
System.out.println("s4.equals(s5)? " + s4.equals(s5)); // true
System.out.println("==========================");
/**
* 第(2)题
*/
A a1 = new A("aaa");
A a2 = new A("aaa");
System.out.println("a1==a2? "+ (a1==a2)); // false
System.out.println("a1.equals(a2)? " + a1.equals(a2)); // true
}
}
class A{
private String content;
public A(String content) {
super();
this.content = content;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
// 重写equals方法
@Override
public boolean equals(Object obj) {
A obj1 = (A)obj;
// TODO Auto-generated method stub
return this.content == obj1.getContent();
}
}