计算长方形的周长和面积(类和对象)
来源:互联网 发布:python .shift[1] 编辑:程序博客网 时间:2024/05/16 18:45
计算长方形的周长和面积(类和对象)
Time Limit: 1000MS Memory Limit: 65536KB
SubmitStatistic
Problem Description
设计一个长方形类Rect,计算长方形的周长与面积。
成员变量:整型、私有的数据成员length(长)、width(宽);
构造方法如下:
(1)Rect(int length) —— 1个整数表示正方形的边长
(2)Rect(int length, int width)——2个整数分别表示长方形长和宽
成员方法:包含求面积和周长。(可适当添加其他方法)
要求:编写主函数,对Rect类进行测试,输出每个长方形的长、宽、周长和面积。
Input
输入多组数据;
一行中若有1个整数,表示正方形的边长;
一行中若有2个整数(中间用空格间隔),表示长方形的长度、宽度。
若输入数据中有负数,则不表示任何图形,长、宽均为0。
Output
每行测试数据对应一行输出,格式为:(数据之间有1个空格)
长度 宽度 周长 面积
Example Input
12 34 52-2-2 -3
Example Output
1 1 4 12 3 10 64 5 18 202 2 8 40 0 0 00 0 0 0
Hint
Author
zhouxq
import java.util.*;class rect{int length,width;rect(int length){this.length = length;System.out.println(this.length+" "+this.length+" "+4*this.length+" "+this.length*this.length);}rect(int length,int width){this.length = length;this.width = width;System.out.println(this.length+" "+this.width+" "+2*(this.length+this.width)+" "+this.width*this.length);}}public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); while(input.hasNext()) { String s = input.nextLine(); char[] a = s.toCharArray(); int t = 0; for(int i=0;i<a.length;i++) { if(a[i]==' ') { t = 1; } } if(t==0) { int c = Integer.parseInt(s); if(c<=0) System.out.println("0 0 0 0"); else { rect r1 = new rect(c); } } else { String s2[] = s.split(" "); int a1 = Integer.parseInt(s2[0]); int b1 = Integer.parseInt(s2[1]); if(a1<0||b1<0) { System.out.println("0 0 0 0"); } else { rect r2 = new rect(a1,b1); } } } }}
0 0
- 计算长方形的周长和面积(类和对象)
- 计算长方形的周长和面积(类和对象)
- 计算长方形的周长和面积(类和对象)
- 计算长方形的周长和面积(类和对象)
- java_sdut_计算长方形的周长和面积(类和对象)
- java计算长方形的周长和面积(类和对象)
- 计算长方形的周长和面积(类和对象)java sdut ACM 3339
- c++ 计算长方形的周长和面积
- 计算一个长方形的面积和周长
- 4-计算长方形的周长和面积
- 2673-计算长方形的周长和面积
- 计算长方形的面积和周长
- 计算长方形的面积和周长
- 计算长方形的面积和周长
- 计算圆、长方形的面积和周长
- 长方形的面积和周长
- 3-4 计算长方形的周长和面积
- 3-4 计算长方形的周长和面积
- React Native实战之flexbox布局(RN基础)
- 01:整型数据类型存储空间大小
- Tomcat的使用笔记
- java设计模式之观察者模式
- css3:border-image边框图像详解
- 计算长方形的周长和面积(类和对象)
- 解决样式错乱
- php连接mysql与时间戳和时间的转换
- 02:浮点型数据类型存储空间大小
- Unity 第三人称镜头跟随
- zoj3160Couples
- Lock的用法
- 集合框架
- 优化SQL语句的一般步骤