矩形類的創(chuàng)建java代碼 java設計一個矩形類rectangle

Java編寫一個矩形類,并計算面積和周長?

class Rectangle{

目前成都創(chuàng)新互聯(lián)公司已為1000+的企業(yè)提供了網(wǎng)站建設、域名、網(wǎng)頁空間、網(wǎng)站托管、服務器托管、企業(yè)網(wǎng)站設計、增城網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

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

import?java.awt.Point;

public?class?Rectangle?{

private?int?widthT?=?0;

private?int?heightT?=?0;

Point?point?=?new?Point();

public?Rectangle(int?width,?int?height,?int?centerX,?int?centerY)?{

widthT?=?width;

heightT?=?height;

point.x?=?centerX;

point.y?=?centerY;

}

public?int?width()?{

return?widthT;

}

public?int?height()?{

return?heightT;

}

public?int?centerX()?{

return?point.x;

}

public?int?centerY()?{

return?point.y;

}

}

麻煩采納呦~~~親

編寫一個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程序矩形類兩數(shù)據(jù)員別rLength寬rWidth通getLength()、getWidth()、getArea()別查看矩形、寬面積通setLength()setWidth()重新設置矩形寬

分享名稱:矩形類的創(chuàng)建java代碼 java設計一個矩形類rectangle
本文來源:http://muchs.cn/article24/dospdce.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設虛擬主機、云服務器、網(wǎng)站收錄、定制開發(fā)、

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)