Java 编写一个矩形类Rect
1、编写一个矩形类Rect.java包含:两个私有属性:矩形的宽width;矩形的长length。一个构造器法:带有两个参数的构造器方法,用于将width和length属...
1、编写一个矩形类Rect.java
包含:
两个私有属性:矩形的宽width;矩形的长length。
一个构造器法:带有两个参数的构造器方法,用于将width和length属性初化。
length和width属性的set和get方法。
两个公有成员方法:分别用于计算并返回矩形的面积和周长。 展开
包含:
两个私有属性:矩形的宽width;矩形的长length。
一个构造器法:带有两个参数的构造器方法,用于将width和length属性初化。
length和width属性的set和get方法。
两个公有成员方法:分别用于计算并返回矩形的面积和周长。 展开
4个回答
展开全部
public class Rect {
private double length;//矩形的长
private double width;//矩形的宽
public Rect() {}//默认构造器
public Rect(double length,double width) {
this.length = length;
this.width = width;
}
/**
* 取得矩形的面积
* */
public double getArea(){
return this.getLength() * this.getWidth();
}
/**
* 取得矩形的周长
* */
public double getPerimeter(){
return (this.getLength() + this.getWidth()) * 2;
}
/**
* 取得矩形的面积,需传入矩形长与宽
* */
public double getArea(double length,double width){
return length * width;
}
/**
* 取得矩形的周长,需传入矩形长与宽
* */
public double getPerimeter(double length,double width){
return (length + width) * 2;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
}
private double length;//矩形的长
private double width;//矩形的宽
public Rect() {}//默认构造器
public Rect(double length,double width) {
this.length = length;
this.width = width;
}
/**
* 取得矩形的面积
* */
public double getArea(){
return this.getLength() * this.getWidth();
}
/**
* 取得矩形的周长
* */
public double getPerimeter(){
return (this.getLength() + this.getWidth()) * 2;
}
/**
* 取得矩形的面积,需传入矩形长与宽
* */
public double getArea(double length,double width){
return length * width;
}
/**
* 取得矩形的周长,需传入矩形长与宽
* */
public double getPerimeter(double length,double width){
return (length + width) * 2;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
}
展开全部
public class Rect {
private double width;
private double height;
public Rect(double width, double height) {
this.width = width;
this.height = height;
}
public Rect() {
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea() {
return this.width * this.height;
}
public double getAround() {
return 2 * (this.width + this.height);
}
}
private double width;
private double height;
public Rect(double width, double height) {
this.width = width;
this.height = height;
}
public Rect() {
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea() {
return this.width * this.height;
}
public double getAround() {
return 2 * (this.width + this.height);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Rect {
/**
* @param args
*/
private Double width;
private Double length;
public Rect(Double width,Double length){
this.width = width;
this.length = length;
}
public Double getWidth() {
return width;
}
public void setWidth(Double width) {
this.width = width;
}
public Double getLength() {
return length;
}
public void setLength(Double length) {
this.length = length;
}
//计算矩形面积
public Double Rect_Area(){
return this.width*this.length;
}
//计算矩形周长
public Double Rect_Around(){
return 2*(this.width+this.length);
}
}
//测试类
public test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Double w = 20.0;
Double l = 30.0;
Rect rect = new Rect(w,l);
System.out.println("宽度:"+w+" 长度:"+l+"面积:"+rect.Rect_Area());
System.out.println("宽度:"+w+" 长度:"+l+"周长:"+rect.Rect_Around());
}
}
/**
* @param args
*/
private Double width;
private Double length;
public Rect(Double width,Double length){
this.width = width;
this.length = length;
}
public Double getWidth() {
return width;
}
public void setWidth(Double width) {
this.width = width;
}
public Double getLength() {
return length;
}
public void setLength(Double length) {
this.length = length;
}
//计算矩形面积
public Double Rect_Area(){
return this.width*this.length;
}
//计算矩形周长
public Double Rect_Around(){
return 2*(this.width+this.length);
}
}
//测试类
public test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Double w = 20.0;
Double l = 30.0;
Rect rect = new Rect(w,l);
System.out.println("宽度:"+w+" 长度:"+l+"面积:"+rect.Rect_Area());
System.out.println("宽度:"+w+" 长度:"+l+"周长:"+rect.Rect_Around());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你是不是宁波教育学院的,我是*林君收到请留言。谢谢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询