一道简单的java编程题,各位大神拜托了!
WriteaclassnamedRectangletorepresentrectangles.ithasmemberattributeswidth:intandheigh...
Write a class named Rectangle to represent rectangles.it has member attributes width:int and height:int.please provide the setter and getter of those members, and design a method named area to calculate its area.
编写一个类名为矩形代表rectangles.it的成员属性width:int和高度:int.please提供setter和getter那些成员,设计名为面积的方法来计算其面积。
新手,所以请大神们用简单点的方式解答,多谢! 展开
编写一个类名为矩形代表rectangles.it的成员属性width:int和高度:int.please提供setter和getter那些成员,设计名为面积的方法来计算其面积。
新手,所以请大神们用简单点的方式解答,多谢! 展开
3个回答
展开全部
等楼下的给你解答吧
居然没人回答你,看下面
public class Rectangles{
private int width; // 宽度
private int height; // 长度
public void setWidth(int width){
this.width = width;
}
public int getWidth(){
return width;
}
public void setHeight(int height){
this.height = height;
}
public int getHeight(){
return height;
}
public int getSquare(){
return width * height;
}
}
展开全部
package t90;
public class Rectangle {
static int hight;
static int width;
public int getHight() {
return hight;
}
public void setHight(int hight) {
this.hight = hight;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getArea (){
if(hight <= 0 || width <= 0){
System.out.println("你设置的长宽不符合条件");
return 0;
}else{
return hight*width;
}
}
}
测试类
package t90;
public class Test {
public static void main(String[] args) {
Rectangle r = new Rectangle();
r.setHight(-1);
r.setWidth(10);
System.out.println(r.getArea());
}
}
public class Rectangle {
static int hight;
static int width;
public int getHight() {
return hight;
}
public void setHight(int hight) {
this.hight = hight;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getArea (){
if(hight <= 0 || width <= 0){
System.out.println("你设置的长宽不符合条件");
return 0;
}else{
return hight*width;
}
}
}
测试类
package t90;
public class Test {
public static void main(String[] args) {
Rectangle r = new Rectangle();
r.setHight(-1);
r.setWidth(10);
System.out.println(r.getArea());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Rectangle {
int width;
int height;
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String area(){
return "area is "+ width*height;
}
public static void main(String[] args) {
Rectangle r=new Rectangle();
r.setWidth(5);
r.setHeight(4);
System.out.println(r.area());
}
}
int width;
int height;
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String area(){
return "area is "+ width*height;
}
public static void main(String[] args) {
Rectangle r=new Rectangle();
r.setWidth(5);
r.setHeight(4);
System.out.println(r.area());
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询