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

熱點內容
測量參數時接單片機的 瀏覽:110
手機音樂添加文件夾 瀏覽:544
百度智能雲b18怎麼刪除app 瀏覽:966
單片機中為什麼顯示找不到頭文件 瀏覽:149
iisweb伺服器如何重啟 瀏覽:836
微信沒有適配方舟編譯器 瀏覽:79
箍筋加密區梁的凈高 瀏覽:887
samp如何加入外國伺服器 瀏覽:893
保鮮膜解壓教學視頻 瀏覽:981
台達plc編程通訊管理軟體 瀏覽:405
優優pdf 瀏覽:799
程序員職業穿搭 瀏覽:255
程序員軟考大綱 瀏覽:17
命令窗口輸入後不滾動 瀏覽:639
C面向切面編程aop例子 瀏覽:369
windowsrar命令 瀏覽:380
單片機編程語言有哪些 瀏覽:442
蘋果安卓系統筆記本怎麼設置密碼 瀏覽:983
只能加密不能解密有什麼用 瀏覽:240
怎麼製造app 瀏覽:122