《Java程序设计》孙聪 第3章较难课后习题的解析
##3-2
class NewRectangle{
double width;
double height;
public NewRectangle() {
this.width=this.height=0.0;
}
public NewRectangle(double a,double b) {
this.width=a;
this.height=b;
}
public double getArea(){
double s=this.width*this.height;
return s;
}
public double getPerimeter() {
double c=2*(this.width+this.height);
return c;
}
}
public class ch3_2 {
public static void main(String[] args) {
NewRectangle ground= new NewRectangle(1,2);//用1,2举例
System.out.println(ground.getPerimeter());
}
}
##3-3
package ch3_3;
class Point{
double x,y;
public Point(){
this.x=this.y=0.0;
}
public Point(double x,double y) {
this.x=x;
this.y=y;
}
public double distance(Point p){
double d=Math.sqrt(Math.pow(p.x-this.x,2)+Math.pow(p.y-this.y, 2));
return d;
}
}
class NewRectangle{
double width;
double height;
Point p;
public NewRectangle() {
this.width=this.height=0.0;
}
public NewRectangle(double a,double b,double x,double y) {
this.width=a;
this.height=b;
this.p=new Point(x,y);
}
public void say()
{
System.out.println("width:"+this.width+";height:"+this.height+";x:"+this.p.x+";y:"+this.p.y);
}
public double getArea(){
double s=this.width*this.height;
return s;
}
public double getPerimeter() {
double c=2*(this.width+this.height);
return c;
}
public boolean bPoint(Point p) {
if((this.p.x<=p.x&&p.x<=this.p.x+this.width)&&(this.p.y<=p.y&&p.y<=this.p.y+this.height))
return true;
else
return false;
}
public String bRectangle(NewRectangle r) {//判断r是否在this里面
if(this.bPoint(r.p)==true||this.bPoint(new Point(r.p.x+r.width,r.p.y+r.height))==true||this.bPoint(new Point(r.p.x+r.width,r.p.y))||this.bPoint(new Point(r.p.x,r.p.y+r.height))) {
if(this.bPoint(r.p)&&this.bPoint(new Point(r.p.x+r.width,r.p.y+r.height))==true&&this.bPoint(new Point(r.p.x+r.width,r.p.y))&&this.bPoint(new Point(r.p.x,r.p.y+r.height)))
return "包含";
else return "重叠";
}
else if(r.bPoint(this.p))
return "被包含";
else return "无关";
}
}
public class ch3_3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
NewRectangle a= new NewRectangle(1,1,1,3);
a.say();
NewRectangle b= new NewRectangle(100,100,0,0);
b.say();
System.out.println(a.bRectangle(b));
}
}
##3-9
package ch3_9;
class Cycle{
public static void ride(Cycle p) {
if(p instanceof Unicycle) {
Unicycle n=(Unicycle)p;
System.out.println("Unicycle's wheel:"+n.wheel());
n.balance();
}
else if(p instanceof Bicycle){
Bicycle n=(Bicycle)p;
n.balance();
System.out.println("Bicycle's wheel:"+n.wheel());
}
else if(p instanceof Tricycle) {
Bicycle n=(Bicycle)p;
System.out.println("Tricycle's wheel:"+n.wheel());
}
}
public int wheel() { //用于区分父类和子类
return 0;
}
}
class Bicycle extends Cycle{
public int wheel() {
return 2;
}
public void balance() {
System.out.println("Balance is so hard!");
}
}
class Tricycle extends Cycle{
public int wheel() {
return 3;
}
public void balance() {
System.out.println("Balance is so hard!");
}
}
class Unicycle extends Cycle{
public int wheel() {
return 1;
}
public void balance() {
System.out.println("Balance easily!");
}
}
public class ch3_9{
public static void main(String[] args) {
Bicycle car=new Bicycle();
Cycle.ride(car);
}
}