導航:首頁 > 編程語言 > Java矩形代碼

Java矩形代碼

發布時間:2023-06-04 17:56:47

㈠ 編寫一個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中做一個按鈕,點擊按鈕後畫一個矩形的代碼怎麼寫

兄弟幫你寫了一個:

import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;

public class Print {
public static void main(String[] args) {
new Te();
}
}

class Te extends Frame implements ActionListener {

Color cc = Color.red;
int x = -20, y = -50;
Random r = new Random();

public Te() {

this.setLayout(null);
Button b = new Button("畫圓");
this.add(b);
b.setBounds(30,30,50,50);
b.addActionListener(this);
this.addWindowListener(new WindowAdapter () {

@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}

});
this.setBounds(200,200,500,400);
this.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
this.cc = Color.red;
this.x = r.nextInt(400);
do {
int x1 = r.nextInt(300);
this.y = x1;
} while (this.y < 50);

this.repaint();
}

@Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(cc);
g.drawRect(x,y,50,50);
g.setColor(c);
}
}

閱讀全文

與Java矩形代碼相關的資料

熱點內容
ipad解壓專家怎麼解壓qq郵箱文件 瀏覽:251
php712安裝 瀏覽:448
python遠程桌面控制 瀏覽:215
操作系統scan演算法 瀏覽:11
伺服器板塊有什麼龍頭 瀏覽:74
我的世界伺服器成員怎麼開創造 瀏覽:660
程序員鄭州買房哪個區好 瀏覽:203
程序員發怒 瀏覽:823
安卓機看視頻怎麼沒有小窗口 瀏覽:456
minecraft伺服器怎麼布置 瀏覽:306
怎麼把安卓的東西轉到已激活蘋果 瀏覽:852
停止服務doss命令 瀏覽:877
u盤占內存但該文件夾為空 瀏覽:612
伺服器怎麼更換重生點 瀏覽:34
收費api調用平台源碼 瀏覽:647
安卓怎麼自檢病毒 瀏覽:560
布卡雲伺服器 瀏覽:770
程序員是怎麼做系統的 瀏覽:745
燕窩溯源碼最大加工廠 瀏覽:939
黑馬程序員第28集 瀏覽:487