导航:首页 > 编程语言 > 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矩形代码相关的资料

热点内容
lcd单片机驱动 浏览:643
通达信主力拉升出货指标公式源码 浏览:635
廉洁pdf 浏览:16
批量用修改多个文件夹及子目录 浏览:315
王者每个服务器爆满怎么办 浏览:947
安卓手机如何清除所有的数据 浏览:978
激光打标机加密狗驱动无法启动 浏览:908
硅谷程序员题库 浏览:558
安卓系统怎么开车模式 浏览:940
网剧程序员那么可爱15集完整版 浏览:172
pdf怎么生成 浏览:934
pythondict树 浏览:582
ilo2命令 浏览:292
会饮pdf 浏览:88
软件加密到哪里去了 浏览:604
nas可以做云服务器吗 浏览:623
app利率怎么算 浏览:8
广电运通程序员 浏览:568
程序员放弃后会怎样 浏览:189
河北模具编程 浏览:192