发布网友 发布时间:2022-04-23 07:28
共4个回答
热心网友 时间:2022-05-13 05:09
package p1.p2;
public class Point {
public Point() {
}
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
package p1.p2;
public class Rectangle {
private Point point1;
private Point point2;
public Rectangle(){}
public Rectangle(Point point1,Point point2){
this.point1=point1;
this.point2=point2;
}
public Point getPoint1() {
return point1;
}
public void setPoint1(Point point1) {
this.point1 = point1;
}
public Point getPoint2() {
return point2;
}
public void setPoint2(Point point2) {
this.point2 = point2;
}
}
package p1.p2;
import java.util.Scanner;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
try {
System.out.println("请输入第一个点的坐标X和Y:");
x1 = input.nextInt();
y1 = input.nextInt();
System.out.println("请输入第一个点的坐标X和Y(第二个点的X值必须大于第一个):");
x2 = input.nextInt();
while (x2 < x1&&x2!=0) {
System.out.println("第二个点的X值必须大于第一个点:");
x2 = input.nextInt();
}
y2 = input.nextInt();
} catch (Exception e) {
System.out.println(e);
}
Point point1 = new Point(x1, y1);
Point point2 = new Point(x2, y2);
Rectangle re = new Rectangle(point1, point2);
int m = Math.abs((point1.getX() - point2.getX()))
* Math.abs((point1.getY() - point2.getY()));
System.out.println("此矩形的面积为:" + m);
}
}
热心网友 时间:2022-05-13 06:27
package p1.p2;
class Point {
protected double x = 0.0;
protected double y = 0.0;
Point() {}
Point(double x, double y) {
this.x = x;
this.y = y;
}
}
class Rectangle extends Point {
Point a;
Point b;
Rectangle(Point a, Point b) {
this.a = a;
this.b = b;
}
public double getArea() {
double kuan = 0.0;
if (a.x * b.x >= 0) {
kuan = Math.abs(a.x - b.x);
} else {
kuan = Math.abs(a.x) + Math.abs(b.x);
}
double chang = 0.0;
if (a.y * b.y >= 0) {
chang = Math.abs(a.y - b.y);
} else {
chang = Math.abs(a.y) + Math.abs(b.y);
}
return chang * kuan;
}
}
public class TestPoint {
public static void main(String[] args) {
Point a = new Point(3, 5);
Point b = new Point(-3, -6);
Rectangle r = new Rectangle(a, b);
System.out.println(r.getArea());
}
}
热心网友 时间:2022-05-13 08:02
package test.area;
public class Point {
private double x,y;
public Point(double x, double y) {
super();
this.x = x;
this.y = y;
}
public Point() {
super();
}
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;
}
}
package test.area;
public class Rectangle {
private Point p1, p2;
public Rectangle(Point p1, Point p2) {
super();
this.p1 = p1;
this.p2 = p2;
}
public double area(){
return Math.abs((p1.getX()-p2.getX())*(p1.getY()-p2.getY()));
}
}
package test.area;
public class Test {
public static void main(String[] args) {
Point p1 = new Point(0, 0);
Point p2 = new Point(-5, 4);
Rectangle r = new Rectangle(p1, p2);
System.out.println(r.area());
}
}
热心网友 时间:2022-05-13 09:53
public class Test {
public static void main(String [] args){
Rectangle rc = new Rectangle(new Point(3,5),new Point(7,8));
System.out.println(rc.getArea());
rc = new Rectangle(3,5,7,8);
System.out.println(rc.getArea());
}
}
class Point{
private int x;
private int y;
public Point(){}
public Point(int x,int y){
this.x=x;this.y=y;
}
public int getX() {return x;}
public void setX(int x) {this.x = x;}
public int getY() {return y;}
public void setY(int y) {this.y = y;}
}
class Rectangle{
private Point lt;
private Point rb;
public Rectangle(Point lt,Point rb){
this.lt=lt;this.rb = rb;
}
public Rectangle(int x1,int y1,int x2,int y2){
this.lt = new Point(x1,y1);
this.rb = new Point(x2,y2);
}
public int getArea(){
return Math.abs((this.lt.getX()-this.rb.getX())*(this.lt.getY()-this.rb.getY()));
}
}
_____________________________________________________
这样够全的吧,包命自己添吧。