j2se之方法的封装
来源:互联网 发布:java实现聊天室功能 编辑:程序博客网 时间:2024/05/18 01:50
public class Lesson18 {
/**
* author荀小贱
* 要求:建立矩形类,功能,完成矩形的面积和周长,要求矩形的长和宽必须大于0
* 目的:有效的将方法封装起来
*/
private int length;
private int width;
private int area;
private int circle;
public Lesson18(int length, int width){
this.length = length;
this.width = width;
}
public int area(){
this.area = length * width;
return area;
}
public int circle(){
this.circle = (length + width) * 2;
return circle;
}
public boolean is(){
boolean is = false;
if(this.width > 0 && this.length > 0){
is = true;
}
return is;
}
}
package myTest;
import java.util.Scanner;
public class TestLesson18 {
/**
* author荀小贱
* 主测试类
*/
public static void main(String[] args) {
String str = "y";
while (str.equals("y")) {
int[] list = input();
Lesson18 test = new Lesson18(list[0], list[1]);
if (test.is()) {
System.out.println("矩形的面积为:" + test.area());
System.out.println("矩形的周长为:" + test.circle());
} else {
System.out.println("您输入的有误");
}
inputN("是否继续?如果继续请按Y");
}
}
// 将输入的长与宽存入数组当中
public static int[] input() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入矩形的长:");
int num1 = sc.nextInt();
System.out.println("请输入矩形的宽:");
int num2 = sc.nextInt();
int[] list = {num1,num2};
return list;
}
// 是否继续
public static String inputN(String str) {
Scanner sc = new Scanner(System.in);
System.out.println(str);
String s = sc.next();
return s;
}
}
- j2se之方法的封装
- J2SE笔记之方法
- J2SE之Join方法
- J2SE基础夯实系列之String类的split方法
- j2se之数组的运用
- j2se之数组的运用
- j2se笔记 封装
- j2se方法
- J2SE基础夯实系列之File的相关方法代码实例
- J2SE基础夯实系列之使用Arrays.sort()方法,以及涉及到的Comparable和Comparator
- j2se线程使用的两种方法
- J2SE基础之Random类的使用
- J2SE基础之Scanner的使用
- j2se之对象的上转型对象
- j2se之对象的创建过程
- J2SE的知识点之(监听适配器)
- j2se之任意随机数的阶乘
- J2SE基础夯实系列之hashcode和equals方法详细解析, hashmap对于hashcode方法的使用
- unix基础篇
- Cocos2dx: 弹出对话框的设计与实现
- 设计模式总结
- 迭代器的删除问题
- jsp 按照数据规则输出类似合并单元格类型的数据
- j2se之方法的封装
- Android Studio 中文编码问题
- repast HPC初探(运行其自带例子过程中出现的问题)
- Maven pom.xml 配置详解
- WINDOWS下的ANSI字符串和UTF8字符串之间的相互转换
- 排序算法之插入排序
- map/reduce二次排序
- ACM->dijkstra + heap + stl 一个人的旅行 hdu 2066
- MySQL 5.1参考手册学习笔记 第1章:一般信息