JAVA,写一个名为Rectangle的类表示矩形这个我做完了希望您能满意
public class Rectangle {
private double height;
private double width;
private String color;
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;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Rectangle(double width,double height,String color){
this.setColor(color);
this.setHeight(height);
this.setWidth(width);
}
public void getArea(){
double area=0;
area=this.height*this.width;
System.out.println("矩形的面积为" area);
}
【Java运行矩形代码 java设计矩形类】 public String toString(){
String recStr="矩形的高度:" this.getHeight() "宽度:" this.getWidth()
"颜色:" this.getColor();
return recStr;
}
/**
* 测试函数
* @param args
*/
public static void main(String[] args) {
Rectangle rec=new Rectangle(3, 4, "红色");
rec.getArea();
System.out.println(rec.toString());
}
}
java中做一个按钮,点击按钮后画一个矩形的代码怎么写?兄弟帮Java运行矩形代码你写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.y50);
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编写一个矩形类,并计算面积和周长?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.lengththis.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("长方形Java运行矩形代码的面积是Java运行矩形代码:");
System.out.println(rect.getArea());
System.out.printf("长方形Java运行矩形代码的周长是Java运行矩形代码:%d\n", rect.getCircumference());
}
}
关于Java运行矩形代码和java设计矩形类的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。
推荐阅读
- 怎么把图片变pdf表格,怎么把图片搞成pdf格式
- 重庆丽晶酒店营销环境如何,重庆丽晶酒店有多少间客房
- 阿里波特分服务器吗,阿里波特系列顺序
- 快手直播能收到的礼物,快手直播能收到的礼物有哪些
- mysql日期条件怎么写 mysql 日期格式
- flutter_engine,FlutterEngineGroup
- 华硕路由器没有声音怎么办,华硕路由器没有信号
- 苹果手机怎么录像截屏,苹果手机怎么录像截屏的
- php数据库结果集总条数 php数据库结果集总条数是什么