java:<创建一个MyInteger类>
来源:互联网 发布:怎么访问apache服务器 编辑:程序博客网 时间:2024/06/17 00:28
设计名为MyInteger的类,它包括:
一个名为value的int型数据域,存储这个对象表示的int值。
一个为指定的int值创建的MyInteger对象的构造方法。
一个返回int的get方法。
如果值分别为偶数,奇数或素数,那么isEven()、isOdd()、isPrime()方法都会返回true。
如果指定值分别为偶数,奇数或素数,那么isEven(int)、isOdd(int)、isPrime(int)方法都会返回true。
如果指定值分别为偶数,奇数或素数,那么isEven(MyInteger)、isOdd(MyInteger)、isPrime(MyInteger)方法都会返回true。
如果该对象的值与指定的值相等,那么equals(int)和equals(MyInteger)方法返回return。
静态方法parseInt(char[])将数字字符构成的数组转换为一个int值。
静态方法parseInt(String)将一个字符串转换为一个int值。
编写用户程序测试该类中的所有方法。
public class Exercise10_3 { public static void main(String[] args) { String s = "123"; char[] a ={'1','2'};//特别注意这里。 MyInteger mi = new MyInteger(3); MyInteger ni = new MyInteger(4); System.out.println(mi.isEven()); System.out.println(mi.isOdd()); System.out.println(mi.isPrime()); System.out.println(mi.isPrime(ni)+"l"); System.out.println(mi.equals(5)); System.out.println(mi.equals(ni)); System.out.println(mi.parseInt(s)); System.out.println(mi.parseInt1(a)); } }class MyInteger { private int value; public MyInteger(int value) { this.value = value; } public int getValue() { return value; } public boolean isEven() { return value % 2 == 0; } public boolean isOdd() { return value % 2 != 0; } public boolean isPrime() { if ((num == 1) || (num == 2)) { return true; } for (int i = 1; i <= value / 2; i++) { if (value % i == 0) return false; } return true; } public boolean isEven(int value) { return value % 2 == 0; } public boolean isOdd(int value) { return value % 2 != 0; } public boolean isPrime(int value) { if ((num == 1) || (num == 2)) { return true; } for (int i = 1; i <= value / 2; i++) { if (value % i == 0) return false; } return true; } public boolean isEven(MyInteger value) { return value.isEven(); } public boolean isOdd(MyInteger value) { return value.isOdd(); } public boolean isPrime(MyInteger value) { return value.isPrime(); } public boolean equals(int value){ return this.value==value; } public boolean equals(MyInteger value){ return value.equals(this.value); } public static int parseInt(String s){ return Integer.valueOf(s); } public static int parseInt1(char[] cs){ return Integer.valueOf(new String(cs)); }}
public class Exercise10_3 { public static void main(String[] args) { MyInteger n1 = new MyInteger(5); System.out.println("n1 is even? " + n1.isEven()); System.out.println("n1 is prime? " + n1.isPrime()); System.out.println("15 is prime? " + MyInteger.isPrime(15)); char[] chars = {'3', '5', '3', '9'}; System.out.println(MyInteger.parseInt(chars)); String s = "3539"; System.out.println(MyInteger.parseInt(s)); MyInteger n2 = new MyInteger(24); System.out.println("n2 is odd? " + n2.isOdd()); System.out.println("45 is odd? " + MyInteger.isOdd(45)); System.out.println("n1 is equal to n2? " + n1.equals(n2)); System.out.println("n1 is equal to 5? " + n1.equals(5)); }}class MyInteger { private int value; public int getValue() { return value; } public MyInteger(int value) { this.value = value; } public boolean isPrime() { return isPrime(value); } public static boolean isPrime(int num) { if ((num == 1) || (num == 2)) { return true; } for (int i = 2; i <= num / 2; i++) { if (num % i == 0) { return false; } } return true; } public static boolean isPrime(MyInteger o) { return isPrime(o.getValue()); } public boolean isEven() { return isEven(value); } public boolean isOdd() {return isOdd(value); } public static boolean isEven(int n) { return n % 2 == 0; } public static boolean isOdd(int n) {return n % 2 != 0; } public static boolean isEven(MyInteger n) { return isEven(n.getValue()); } public boolean equals(int anotherNum) { return value == anotherNum; } public boolean equals(MyInteger o) { return value == o.getValue(); } public static int parseInt(char[] numbers) { // numbers consists of digit characters. // For example, if numbers is {'1', '2', '5'}, the return value // should be 125. Please note that // numbers[0] is '1' // numbers[1] is '2' // numbers[2] is '5' int result = 0; for (int i = 0; i < numbers.length; i++) { result = result * 10 + (numbers[i] - '0'); } return result; } // You may mention this when you covered Ch8 public static int parseInt(String s) { // s consists of digit characters. // For example, if s is "125", the return value // should be 125. int result = 0; for (int i = 0; i < s.length(); i++) { result = result * 10 + (s.charAt(i) - '0'); } return result; }}
阅读全文
0 0
- java:<创建一个MyInteger类>
- JAVA bootstrap ClassLoader和JAVA核心API替换 《java -Xbootclasspath/p:F:\myinteger.jar MyIntegerTest》
- 来吧java,创建一个SeqList类
- 在myeclipse创建一个java的类
- java 创建一个进度条
- java 创建一个程序
- java 创建一个进程
- 1809 The MyInteger class (eden)
- 创建一个JAVA安装文件
- eclipse创建一个java工程
- 使用Java 创建一个计算器
- 用java创建一个文件
- Java创建一个临时文件方法
- java:创建一个TCP服务
- Maven创建一个Java工程
- Maven创建一个Java项目
- java创建一个基本窗体
- Java语言 -- 一个简单的类创建过程
- 浅谈UML的概念和模型之UML视图
- java匿名内部类
- HDU
- Python学习——字典和集合
- 循环双向链表
- java:<创建一个MyInteger类>
- JS 本地存储布尔值Bug笔记
- 浅谈UML的概念和模型之UML类图关系
- XML封装通讯接口
- linux服务器上部署多个tomcat
- hust 1607 Triangles
- HBase - 数据写入流程解析
- c语言小游戏---扫雷
- Crossword Answers, ACM/ICPC World Finals 1994, UVa232