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

热点内容
离线版mc如何开服务器 浏览:884
结对程序员 浏览:762
使用过的苹果手机怎么同步app 浏览:344
phpcookie无效 浏览:954
python可以搜数学答案 浏览:706
均线衍生指标源码设置 浏览:496
做精一张图pdf 浏览:851
编程培训小朋友 浏览:787
巴克球制作解压 浏览:851
测量参数时接单片机的 浏览:116
手机音乐添加文件夹 浏览:546
百度智能云b18怎么删除app 浏览:968
单片机中为什么显示找不到头文件 浏览:151
iisweb服务器如何重启 浏览:838
微信没有适配方舟编译器 浏览:81
箍筋加密区梁的净高 浏览:889
samp如何加入外国服务器 浏览:895
保鲜膜解压教学视频 浏览:983
台达plc编程通讯管理软件 浏览:407
优优pdf 浏览:801