導航:首頁 > 編程語言 > 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矩形代碼相關的資料

熱點內容
php社區程序 瀏覽:976
python現在就業怎麼樣 瀏覽:304
php上傳目錄許可權設置 瀏覽:736
php合同檔案管理系統 瀏覽:366
tshock伺服器地址 瀏覽:877
php解析文件流 瀏覽:780
伺服器機箱銅片有什麼用 瀏覽:969
樂視手機如何使用原生態安卓 瀏覽:215
dns伺服器怎麼備份啊 瀏覽:865
蘋果退款app怎麼重新購買 瀏覽:655
U盤cmd檢查文件夾成文件 瀏覽:66
java路徑是否存在 瀏覽:338
新氧app上怎麼測臉型 瀏覽:787
用指令獲得命令方塊 瀏覽:427
流星蝴蝶劍怎麼輸入命令 瀏覽:681
keil有紅叉但是能編譯通過 瀏覽:498
賽拉圖的空調壓縮機多少錢 瀏覽:330
linux54 瀏覽:986
shutdown命令linux 瀏覽:250
編譯的輸入 瀏覽:511