当前位置:首页 » 编程语言 » 矩形java

矩形java

发布时间: 2023-06-07 16:07:24

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 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 编写一个矩形类Rect

public class Rect {
private double length;//矩形的长
private double width;//矩形的宽

public Rect() {}//默认构造器

public Rect(double length,double width) {
this.length = length;
this.width = width;
}

/**
* 取得矩形的面积
* */
public double getArea(){
return this.getLength() * this.getWidth();
}

/**
* 取得矩形的周长
* */
public double getPerimeter(){
return (this.getLength() + this.getWidth()) * 2;
}

/**
* 取得矩形的面积,需传入矩形长与宽
* */
public double getArea(double length,double width){
return length * width;
}

/**
* 取得矩形的周长,需传入矩形长与宽
* */
public double getPerimeter(double length,double width){
return (length + width) * 2;
}

public double getLength() {
return length;
}

public void setLength(double length) {
this.length = length;
}

public double getWidth() {
return width;
}

public void setWidth(double width) {
this.width = width;
}
}

❹ 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());
}
}

热点内容
少儿编程机构十强排名 发布:2025-02-14 04:15:33 浏览:898
关闭androidstudio 发布:2025-02-14 03:53:42 浏览:18
安卓脚本一键打板 发布:2025-02-14 03:50:16 浏览:866
我的世界租赁服务器会被封ip吗 发布:2025-02-14 03:46:05 浏览:982
数据库附 发布:2025-02-14 03:44:43 浏览:837
安卓系统解不了锁怎么办 发布:2025-02-14 03:44:35 浏览:552
怎么查询信用卡密码 发布:2025-02-14 03:42:04 浏览:318
studio外贸服装直播脚本 发布:2025-02-14 03:34:16 浏览:453
python集合运算符 发布:2025-02-14 03:06:18 浏览:205
pic编译软件 发布:2025-02-14 03:01:04 浏览:984