@XQF
2016-10-27T22:14:19.000000Z
字数 4887
阅读 1528
java作业
package javaAssignment.second;
public class NewRectangle {
private double width;
private double height;
public NewRectangle() {
width = 0;
height = 0;
}
public NewRectangle(double w, double h) {
width = w;
height = h;
}
private double getArea() {
return height * width;
}
private double getPerimeter() {
return 2 * (height + width);
}
public static void main(String[] args) {
NewRectangle nr = new NewRectangle(10.2, 2.5);
NewRectangle nr1 = new NewRectangle();
System.out.println(nr.getArea());
System.out.println(nr.getPerimeter());
System.out.println(nr1.getArea());
System.out.println(nr1.getPerimeter());
}
}
运行结果:
25.5
25.4
0.0
0.0
新建point类
class Point {
private double x;
private double y;
public Point() {
x = 0.0;
y = 0.0;
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
private double distance(Point p) {
double dx = x - p.x;
double dy = y - p.y;
return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
}
}
关于点是否在矩形中,利用左下角的点进行平移
package javaAssignment.second;
class Point {
private double x;
private double y;
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public Point() {
x = 0.0;
y = 0.0;
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
private double distance(Point p) {
double dx = x - p.x;
double dy = y - p.y;
return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
}
}
public class NewRectangle {
private double width;
private double height;
private Point p;
public NewRectangle() {
width = 0;
height = 0;
}
public NewRectangle(double w, double h, double x, double y) {
p = new Point();
width = w;
height = h;
p.setX(x);
p.setY(y);
}
private double getArea() {
return height * width;
}
private double getPerimeter() {
return 2 * (height + width);
}
private boolean bPointIn(Point p1) {
double Dx = p1.getX() - p.getX();
double Dy = p1.getY() - p.getY();
if ((Dx <= 0 && Dx >= -width) && (Dy >= 0 && Dy <= height)) {
return true;
}
return false;
}
public static void main(String[] args) {
NewRectangle nr = new NewRectangle(10, 10, 0, 1);
NewRectangle nr1 = new NewRectangle();
System.out.println(nr.getArea());
System.out.println(nr.getPerimeter());
Point p1 = new Point(-1, 0);
System.out.println(nr.bPointIn(p1));
}
}
测试数据输出:
100.0
40.0
false
package javaAssignment.second;
class Cycle {
public void ride(Cycle c) {
System.out.println(c);
}
}
class Unicycle extends Cycle {
@Override
public String toString() {
// TODO Auto-generated method stub
return "Unicycle";
}
}
class Bicycle extends Cycle {
@Override
public String toString() {
// TODO Auto-generated method stub
return "Bicycle";
}
}
class Tricycle extends Cycle {
@Override
public String toString() {
// TODO Auto-generated method stub
return "Tricycle";
}
}
public class NO_9 {
public static void main(String[] args) {
Cycle c = new Cycle();
Unicycle uc = new Unicycle();
Bicycle bc = new Bicycle();
Tricycle tc = new Tricycle();
c.ride(uc);
c.ride(bc);
c.ride(tc);
}
}
测试结果:
Unicycle
Bicycle
Tricycle
package javaAssignment.second;
class Cycle {
public void ride(Cycle c) {
System.out.println(c + " " + c.wheel());
}
public int wheel() {
return 0;
}
}
class Unicycle extends Cycle {
private int wheels = 1;
@Override
public String toString() {
// TODO Auto-generated method stub
return "Unicycle";
}
@Override
public int wheel() {
// TODO Auto-generated method stub
return wheels;
}
}
class Bicycle extends Cycle {
private int wheels = 2;
@Override
public String toString() {
// TODO Auto-generated method stub
return "Bicycle";
}
@Override
public int wheel() {
// TODO Auto-generated method stub
return wheels;
}
}
class Tricycle extends Cycle {
private int wheels = 3;
@Override
public String toString() {
// TODO Auto-generated method stub
return "Tricycle";
}
@Override
public int wheel() {
// TODO Auto-generated method stub
return wheels;
}
}
public class NO_9 {
public static void main(String[] args) {
Cycle c = new Cycle();
Unicycle uc = new Unicycle();
Bicycle bc = new Bicycle();
Tricycle tc = new Tricycle();
c.ride(uc);
c.ride(bc);
c.ride(tc);
}
}
在ride()方法中使用一个instanceof
package javaAssignment.second;
class Cycle {
public void ride(Cycle c) {
if (c instanceof Unicycle) {
((Unicycle) c).balance();
} else if (c instanceof Bicycle) {
((Bicycle) c).balance();
} else {
System.out.println("This is a instance of Tricycle");
}
}
public int wheel() {
return 0;
}
}
class Unicycle extends Cycle {
private int wheels = 1;
@Override
public String toString() {
// TODO Auto-generated method stub
return "Unicycle";
}
@Override
public int wheel() {
// TODO Auto-generated method stub
return wheels;
}
public void balance() {
System.out.println("Unicycle balance");
}
}
class Bicycle extends Cycle {
private int wheels = 2;
@Override
public String toString() {
// TODO Auto-generated method stub
return "Bicycle";
}
@Override
public int wheel() {
// TODO Auto-generated method stub
return wheels;
}
public void balance() {
System.out.println("Bicycle balance");
}
}
class Tricycle extends Cycle {
private int wheels = 3;
@Override
public String toString() {
// TODO Auto-generated method stub
return "Tricycle";
}
@Override
public int wheel() {
// TODO Auto-generated method stub
return wheels;
}
}
public class NO_9 {
public static void main(String[] args) {
Cycle c = new Cycle();
Cycle uniCycle = new Unicycle();
c.ride(uniCycle);
}
}
结果:
Unicycle balance