「java求矩形的长」用java编写矩形的面积和周长

博主:adminadmin 2022-12-01 14:10:06 72

本篇文章给大家谈谈java求矩形的长,以及用java编写矩形的面积和周长对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

编写一个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编写一个矩形类,并计算面积和周长?

class Rectangle{

private int width = 2;

private int length = 1;

public int getWidth(){

return this.width;

}

public void setWidth(int w){

this.width = w;

}

public int getLength(){

return this.length;

}

public void setLength(int l){

this.length = l;

}

public int getArea(){

return this.length * this.width;

}

public int getCircumference(){

return (this.length + this.width) * 2;

}

public Rectangle(){}

public Rectangle(int l, int w){

this.length = l;

this.width = w;

}

}

public class demo{

public static void main(String[] args) {

Rectangle rect = new Rectangle(30, 8);

System.out.print("长方形的面积是:");

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

System.out.printf("长方形的周长是:%d\n", rect.getCircumference());

}

}

JAVA求助:由键盘输入一个矩形的长和宽,求出这个矩形的周长和面积 该怎么写

public static void main(String[] args) {

       System.out.println("输入长度");

       Double c=new Scanner(System.in).nextDouble();

       System.out.println("输入宽度");

       Double k=new Scanner(System.in).nextDouble();

       System.out.println("面积:"+c*k);

       System.out.println("周长:"+(c+k)*2);

    }

Java编程 求矩形周长和面积

import java.util.*;

public class Rectangle {

private float length; //定义长变量

private float width; // 宽变量

public Rectangle(float length,float width){

this.length=length;

this.width=width;

}

public float getGirth(){

return (length+width)*2;

} //求周长方法

public float getArea(){

return length*width;

} //求面积方法

public static void main (String[] args) {

Scanner in=new Scanner(System.in);//调用输入方法

System.out.println ("请输入矩形的长:");

float a=in.nextFloat();

System.out.println ("请输入矩形的宽:");

float b=in.nextFloat();

System.out.println ("矩形周长为:"+new Rectangle(a,b).getGirth());

System.out.println ("矩形面积为:"+new Rectangle(a,b).getArea());

}

}

//Jcreator4.0编译通过,写的比较简单 只有简单的功能 刚刚写的求周长时忘乘2了...

java求矩形的长的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于用java编写矩形的面积和周长、java求矩形的长的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-01,除非注明,否则均为首码项目网原创文章,转载请注明出处。