问一个java编程问题:定义一个矩形类(Rectangle)

问一个java编程问题:定义一个矩形类(Rectangle) 已知对角线的坐标求面积和周长~

public class Rectangle{
private int left,top,right,bottom;
public Rectangle(int left,int top,int right,int bottom){
this.left=left;
this.top=top;
this.right=right;
this.bottom=bottom;
}
public int getWidth(){
return Math.abs(right-left);
}
public int getHeight(){
return Math.abs(top-bottom);
}
public int getPerimeter(){
return (getWidth()+getHeight())*2;
}
public int getArea(){
return getWidth()*getHeight();
}
public static void main(String []args){
Rectangle rect=new Rectangle(0,0,50,60);
System.out.println("Perimeter:"+rect.getPerimeter());
System.out.println("Area:"+rect.getArea());
}
}

public class Rectangle{
private int width;
private int height;
public Rectangle(){
this.width = 10;
this.height = 10;
}

public Rectangle(int width, int height){
this.width = width;
this.height = height;
}

public int area(){
return width * height;
}

//省略getter/setter
}

public class Rectangle {
// top, left 左上角那个顶点的坐标
// width: 宽
// heigth: 长
private double top, left, width, height;

// 构造函数
public Rectangle(double top, double left, double width, double height) {
this.top = top;
this.left = left;
this.width = width;
this.height = height;
}

// 改变顶点坐标,即改变矩形坐标位置
public void location(double top, double left) {
this.top = top;
this.left = left;
}

// 改变宽,高,即改变矩形宽高
public void size(double width, double height) {
this.width = width;
this.height = height;
}

// 计算面积,宽×高
public double area() {
return width * height;
}

// 判断某点是否在矩形内
public boolean isInside(double x, double y) {
// 这里采用的是数学上的坐标系,即向上向右为正
// 如果采用向下向右为正的话,则要改
// return x > this.left && x < this.left + this.width && y < this.top +
// this.height && y > this.top;
// 这里点不包括在边上,如果在边上也算的话,把小于号或大于号改成小于等于及大于等于
return x > this.left && x < this.left + this.width
&& y > this.top - this.height && y < this.top;
}
}

说明 有两个类Rectangle 和Point
=======
package rectangle;

public class Rectangle {

//确定一个矩形 至少需要3个点,假设一个矩形按逆时针的方向点为A、B、C、D取A、B、C3点
Point pointA;
Point pointB;
Point pointC;

public Point getPointA() {
return pointA;
}

public void setPointA(Point pointA) {
this.pointA = pointA;
}

public Point getPointB() {
return pointB;
}

public void setPointB(Point pointB) {
this.pointB = pointB;
}

public Point getPointC() {
return pointC;
}

public void setPointC(Point pointC) {
this.pointC = pointC;
}

/*
* 要求1: 提供可以改变矩形坐标位置的方法; 即移动一个矩形 矩形3点都会变化
* 要求2: 提供可以改变矩形宽高的方法; 移动矩形3点中的一点
* 要求3: 提供求矩形面积的方法; 求点距 并做乘法
* 要求4: 提供计算一个点是否在矩形内的方法. 如果矩形的的长宽为平行或垂直于XY 轴将极大的方便这一过程
*
* 思路若矩形的长宽为平行或垂直于X Y轴 非常有利于此问题的解决
* 先解决此种特殊情况
* 在考虑 X Y轴沿坐标原点选择对3点的坐标的实践表换过程
*/

//移动矩形
public void moveRectangle(int mx,int my){
//沿X轴正向移动为正数,反之负数
//沿Y轴正向移动为正数,反之负数
this.pointA.move(mx, my);
this.pointB.move(mx, my);
this.pointC.move(mx, my);
}

public void changeWidth(double distance){
//逆时针旋转坐标到x轴与B C两点的连线平行 并返回新坐标
Point pointAA=Point.turnTheCenterPoint(pointA, pointB, pointC);
Point pointBB=Point.turnTheCenterPoint(pointC, pointB, pointC);
Point pointCC=Point.turnTheCenterPoint(pointC, pointB, pointC);
//此时在新坐标系下 AB边与X轴锤子 BC边与X轴平行
//假定AB边为需要改变的宽 移动的点为A点 网上为增加整数 往下为减 负数
pointAA.setY(pointAA.getY()+distance);
//改变的只有A点 只需要将A点变回原坐标系下的点 也就是顺时针再转回坐标系
//鉴于你一分都不给 我就不写了 自己弄去吧
//反转坐标系 获取A的新坐标
}

//求面积
public double getMianJi(int mx,int my){
return Point.getDistance(pointA, pointB)*Point.getDistance(pointB, pointC);
}

//判断矩形框中是不是有某个点 假定矩形有一边平行于X轴
public boolean hasPoint(Point p){
//逆时针旋转坐标到x轴与B C两点的连线平行 并返回新坐标
Point pointAA=Point.turnTheCenterPoint(pointA, pointB, pointC);
Point pointCC=Point.turnTheCenterPoint(pointC, pointB, pointC);
Point pp=Point.turnTheCenterPoint(p, pointB, pointC);
if(pointAA.getX()<=pp.getX()&&pointCC.getX()>=pp.getX()&&pointAA.getY()>=pp.getY()&&pointCC.getY()<=pp.getY()){
return true;
}else{
return false;
}
}

}

===============
package rectangle;
public 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 void move(double mx,double my){
x+=mx;
y+=my;
}
//求两点点距
public static double getDistance(Point a,Point b){
return Math.sqrt(Math.pow(a.getX()-b.getX(), 2)+Math.pow(a.getY()-b.getY(), 2));
}

/*
*逆时针转动坐标原点,转到X轴于矩形的BC平行
*需要转动的角度A tan$=(pointB.y-pointC.y)/(pointA.x-pointC.x)
*
*转动后所有点和X轴的夹角都减小了$度
*返回转到后的新点(新坐标)
*/
public static Point turnTheCenterPoint(Point pointA,Point pointB,Point pointC){
double a=Math.atan(pointA.getY()/pointA.getX());
double b=Math.atan((pointB.getY()-pointC.getY())/(pointA.getX()-pointC.getX()));
double xx=Math.sin(a-b);
double newPointX=xx*Math.sqrt((Math.pow(pointA.getX(), 2)+Math.pow(pointA.getY(), 2)));
double yy=Math.cos(a-b);
double newPointY=yy*Math.sqrt((Math.pow(pointA.getX(), 2)+Math.pow(pointA.getY(), 2)));
Point point=new Point();
point.setX(newPointX);
point.setY(newPointY);
return point;
}
}

package s;

public class Rectangle {

/**
* @param args
*/
private int lx;//左上横坐标

private int ly;//左上纵坐标

private int rx;//右上下横坐标

private int ry;//右下纵坐标

public int getLx() {
return lx;
}

public void setLx(int lx) {
this.lx = lx;
}

public int getLy() {
return ly;
}

public void setLy(int ly) {
this.ly = ly;
}

public int getRx() {
return rx;
}

public void setRx(int rx) {
this.rx = rx;
}

public int getRy() {
return ry;
}

public void setRy(int ry) {
this.ry = ry;
}

public void setLeft(int x,int y){
this.setLx(x);
this.setLy(y);
}

public void setRight(int x,int y){
this.setRx(x);
this.setRy(y);
}

//判断长和宽
public boolean isBool(){
return Math.abs(this.getRx()-this.getLx())>=Math.abs(this.getRy()-this.getLy());
}

}

在java中编写程序,定义一个学生类
答:import java.util.List;public class Student { // 定义一个学生类,Student有姓名,学号,选学的课程列表 private String stuname;private String stuid;private List<Course> courses;public Student() { } public Student(String stuname, String stuid, List<Course> courses) { this.stuname = ...

JAVA的一个问题,定义一个长方形类,在类中定义两个成员属性分别为长和...
答:+ height);}}public class Test{public static void main(String[] args) {Rectangle rect1 = new Rectangle(100, 100);rect1.printRectangle();Rectangle rect2 = new Rectangle(200, 500);rect2.printRectangle();Rectangle rect3 = new Rectangle(400, 300);rect3.printRectangle();}} ...

java中如何定义一个类,定义一个类需要注意那些地方?
答:protected: 继承访问权限——使用protected关键字,意味着派生类可以访问基类的protected成员,非派生类则没有这样的权限。protected也提供包访问权限,相同包内的其他类可以访问protected成员。追问:public class Person { // extends java.lang.Object {这句话就是定义一个类..后面的注释就是说继承与java...

java编程 定义一个类Point,代表一个点,public属性有x和y,方法有显示点...
答:} public static MyPoint getMiddle(MyPoint p1,MyPoint p2){ MyPoint p = new MyPoint((p1.x+p2.x)/2,(p1.y+p2.y)/2); return p; }}文件Test.java public class Test{ public static void main(String[] args){ MyPoint p1 = new MyPoint(); MyPoint ...

用java编程定义两个类A和B, 类A中定义一个char类型属性x(将其赋值为...
答:class A{ private char x = 'A';public void Myprint(){ System.out.println("A[ x=" +x+" ]");} } class B extends A{ private int y = 16;private String s = "java";public void MyPrint(){ System.out.println("B[ y = "+y+" s= "+s+" ]");} public void Print...

java编程。定义一个教师类,有姓名职称工资和工龄,工资的初始值是1000...
答:return (this.money + (this.seniority * 10)) * 1.3;} return this.money;} public void info() { System.out.println("姓名: " + this.name);System.out.println("职称: " + this.title);System.out.println("工资: " + wages());System.out.println("工龄: " + this.seniority)...

java编程:编写程序,定义一个Person类,含有姓名name和年龄age两个成员变 ...
答:class Person{ private String name; private int age; public Person(String name){ this.name=name; } public Person(String name, int age){ this.name=name; this.age =age; } //get/set省略 public void show(){ System.out.println("name:"+name+...

Java问题,求教大神,在线等,定义类Box 定义double型的三个变量:length,w...
答:class Box { double length,width,height;public Box(double length,double width,double height){ this.length = length;this.width = width;this.height = height;} public void setLength(double x){ length = x;} public void setWidth(double x){ width = x;} public void setHeight(...

JAVA编程 新手小白求教 定义一个城市类 有城市名 面积 人口数量等属 ...
答://定义一个城市类 有城市名 面积 人口数量等属性 然后创建其两个对象并输出所有其属性值public class Test {public Test(){}public Test(String name, double mianji, int renshu){this.name = name;this.mianji = mianji;this.renshu = renshu;}private String name;private double mianji;pr...

java编程,声明一个类,定义一个方法以计算一维数组中的最大值并返回该...
答:public class Max { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5,10,11,15,100,-1,-4.5}; //定义一维数组 double num = myList[0]; //0为第一个数组下标 for (int i = 0; i < myList.length; i++) { //开...

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网