「java矩形对象」java定义矩形

博主:adminadmin 2023-03-18 13:18:13 492

今天给各位分享java矩形对象的知识,其中也会对java定义矩形进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

用java编写矩形类

class Rectangle{

double x1, y1, x2, y2;

Rectangle(){

this(0,0,0,0);

}

Rectangle(double x1, double y1, double x2, double y2){

this.x1 = x1;

this.y1 = y1;

this.x2 = x2;

this.y2 = y2;

}

double getArea(){

return Math.abs((x1-x2)*(y1-y2))/2;

}

double getPemi(){

return Math.abs((x1-x2)+(y1-y2))*2;

}

}

public class Test{

public static void main(String[] args) {

Rectangle a = new Rectangle();

a.x1=2.1;

a.y1=3.2;

a.x2=5.2;

a.y2=6.3;

System.out.println("面积是"+a.getArea()+" 周长是"+a.getPemi());

Rectangle b = new Rectangle(1, 2, 6.8, 10.5);

System.out.println("面积是"+b.getArea()+" 周长是"+b.getPemi());

}

}

用java再定义一个主类,创建2个矩形类对象,为对象里面的变量赋值,求面积并输出

楼主 你的调用方法里面并没有使用你自己写的变量

你可以把下面的两个方法加上自己定义的变量 比如

public void call(){ System.out.println("我要打电话给"+color); }public void sent( ){ System.out.println("张三"+"号码"+number); }

或者 不要定义下面两个变量了

public String color;public int number;

直接把下面的2个方法写成

public void call(String name){ System.out.println("我要打电话" +"给"+name); }public void sent(int number ){ System.out.println("张三的号码是"+number); }

然后在main函数里面调用方法的时候 写成下面的形式

p.call("张三");//调用方法p.sent("123456");//调用方法

备注 我回答的可能不对 上面的只是一个参考的形式 另外 楼主 可以把方法名字定义的好理解一些 (sent 是 send 吗 ?)

用JAVA创建一个矩形类Rectangle

这个问题是java最基础的了,希望你自己可以好好地看一下书本,上面都是有的,而且自己看书解决,比来这提问有效的多。

关键是自己注意你所需要定义的类有哪些属性,然后按照这些慢慢去写就好了。

定义矩形类,利用类生成二个矩形对象,并计算面积的java题

public class RectArea{

public static void main(String args[]) {

Rect a = new Rect(7,9);

Rect b = new Rect(4,6);

int area;

area = a.getArea*b.getArea;

System.out.println(area);

}

}

class Rect

{

private int width=1;

private int height=1;

Rect(){}

Rect( int iwidth, int iheight)

{

width = iwidth;

height =iheihgt;

}

publict void set(int iwidth, int iheight)

{

width = iwidth;

height =iheihgt;

}

publict int getArea() {return width*height;}

publict int getWidth() {return width;}

public int getHeight() {regurn height;}

}

JAVA,写一个名为Rectangle的类表示矩形

这个我做完了 希望您能满意

public class Rectangle {

private double height;

private double width;

private String color;

public double getHeight() {

return height;

}

public void setHeight(double height) {

this.height = height;

}

public double getWidth() {

return width;

}

public void setWidth(double width) {

this.width = width;

}

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public Rectangle(double width,double height,String color){

this.setColor(color);

this.setHeight(height);

this.setWidth(width);

}

public void getArea(){

double area=0;

area=this.height*this.width;

System.out.println("矩形的面积为"+area);

}

public String toString(){

String recStr="矩形的高度:"+this.getHeight()+"宽度:"+this.getWidth()

+"颜色:"+this.getColor();

return recStr;

}

/**

* 测试函数

* @param args

*/

public static void main(String[] args) {

Rectangle rec=new Rectangle(3, 4, "红色");

rec.getArea();

System.out.println(rec.toString());

}

}

编写一个JAVA程序,描写一个矩形类,并输出某个矩形的长,宽,面积。具体描述如下?

// 矩形

public class RectangleDemo {

public static void main(String[] args) {

RectangleDemo demo = new RectangleDemo(12, 32);

System.out.println(demo.getPerimeter());

System.out.println(demo.getArea());

demo = new RectangleDemo();

System.out.println(demo.getArea());

System.out.println(demo.getPerimeter());

demo.setHeight(50);

demo.setWidth(30);

System.out.println(demo.getArea());

System.out.println(demo.getPerimeter());

}

// 求周

public double getPerimeter() {

return (height + width) * 2;

}

// 求面积

public double getArea() {

return height * width;

}

public RectangleDemo(double height, double width) {

this.height = height;

this.width = width;

}

public RectangleDemo() {

this.height = 10;

this.width = 10;

}

private double height;// 高度

private double width;// 宽度

public double getHeight() {

return height;

}

public void setHeight(double height) {

this.height = height;

}

public double getWidth() {

return width;

}

public void setWidth(double width) {

this.width = width;

}

}

编写矩形类RectangleJava程序矩形类两数据员别rLength宽rWidth通getLength()、getWidth()、getArea()别查看矩形、宽面积通setLength()setWidth()重新设置矩形宽

关于java矩形对象和java定义矩形的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。