當前位置:首頁 » 編程語言 » 用java編寫一個矩形類

用java編寫一個矩形類

發布時間: 2023-01-10 08:50:23

『壹』 編寫一個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的題目

publicclassRectangle
{
privatedoublewidth,height;
publicRectangle()
{
this(0,0);
}
publicRectangle(doublewidth,doubleheight)
{
this.width=width;
this.height=height;
}
publicdoublegetCurve()
{
return2.0f*(width+height);
}
publicdoublegetSurface()
{
returnwidth*height;
}
}
publicclassTest
{
publicstaticvoidmain(String[]args)
{
Rectanglerect1=newRectangle(10,20);
System.out.println("面積:"+rect1.getSurface());
System.out.println("周長:"+rect1.getCurve());
}
}

『叄』 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編寫一個矩形類,至少包含以下方法:

importjava.awt.Point;

publicclassRectangle{
privateintwidthT=0;
privateintheightT=0;
Pointpoint=newPoint();

publicRectangle(intwidth,intheight,intcenterX,intcenterY){
widthT=width;
heightT=height;
point.x=centerX;
point.y=centerY;
}

publicintwidth(){
returnwidthT;
}

publicintheight(){
returnheightT;
}

publicintcenterX(){
returnpoint.x;
}

publicintcenterY(){
returnpoint.y;
}
}

麻煩採納呦~~~親

『伍』 java 編寫一個矩形類 rect 要求如下:


publicclassRect{
privateintlength;
privateintwidth;
privateintstartX;
privateintstartY

publicRect(){}

publicRect(intlength,intwidth){
this.length=length;
this.width=width;
}

publicRect(intlength,intwidth,intstartX,intstartY){
this.length=length;
this.width=width;
this.startX=startX;
this.startY=startY;
}

//不知道你要什麼成員方法,我隨便點....


publicvoidlouzhuhao(){
System.out.println("樓主好....");
}
publicintgetLength(){
returnlength;
}
publicvoidsetLength(intlength){
this.length=length;
}
publicintgetWidth(){
returnwidth;
}
publicvoidsetWidth(intwidth){
this.width=width;
}
publicintgetStartX(){
returnstartX;
}
publicvoidsetStartX(intstartX){
this.startX=startX;
}
publicintgetStartY(){
returnstartY;
}
publicvoidsetStartY(intstartY){
this.startY=startY;
}
}

熱點內容
紅點角標演算法 發布:2025-07-12 12:11:16 瀏覽:843
開心消消樂伺服器繁忙什麼情況 發布:2025-07-12 12:11:14 瀏覽:237
資料庫的封鎖協議 發布:2025-07-12 12:10:35 瀏覽:724
如何配置一台長久耐用的電腦 發布:2025-07-12 11:43:03 瀏覽:601
昆明桃源碼頭 發布:2025-07-12 11:38:45 瀏覽:568
大司馬腳本掛機 發布:2025-07-12 11:38:35 瀏覽:458
資料庫實時監控 發布:2025-07-12 11:31:33 瀏覽:743
vb6反編譯精靈 發布:2025-07-12 11:23:12 瀏覽:997
模擬存儲示波器 發布:2025-07-12 11:10:58 瀏覽:814
怎麼查看安卓真實運行內存 發布:2025-07-12 11:08:39 瀏覽:883