日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java黑皮书课后题第10章:10.3(MyInteger类)设计一个名为MyInteger的类

發(fā)布時間:2024/8/1 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java黑皮书课后题第10章:10.3(MyInteger类)设计一个名为MyInteger的类 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

10.3(MyInteger類)設(shè)計一個名為MyInteger的類

  • 題目
    • 程序說明
  • 代碼
    • Test3.java
    • Test3_MyInteger.java
  • 運行實例
  • UML

題目

程序說明

測試程序:Test3.java
構(gòu)造程序:Test3_MyInteger.java

代碼

Test3.java

public class Test3 {public static void main(String[] args) {Test3_MyInteger mi = new Test3_MyInteger(1);System.out.println(mi.getValue());System.out.println(mi.isEven() + " " + mi.isOdd() + " " + mi.isPrime());System.out.println(Test3_MyInteger.isEven(2) + " " + Test3_MyInteger.isOdd(2) +" " + Test3_MyInteger.isPrime(2));Test3_MyInteger mi_pro = new Test3_MyInteger(2);System.out.println(Test3_MyInteger.isEven(mi_pro) + " " + Test3_MyInteger.isOdd(mi_pro) +" " + Test3_MyInteger.isPrime(mi_pro));char[] ch = {'1', '2', '3'};System.out.println(Test3_MyInteger.parseInt(ch));String str = "123";System.out.print(Test3_MyInteger.parseInt(str));} }

Test3_MyInteger.java

public class Test3_MyInteger {// int型數(shù)據(jù)域static int value;// 有參構(gòu)造方法public Test3_MyInteger(int num){value = num;}// 獲取valuepublic int getValue() {return value;}// 三個方法,判斷對象中的值public boolean isEven(){return value % 2 == 0;}public boolean isOdd(){return value % 2 == 1;}public boolean isPrime(){return havePrimeNumber(value);}// 判斷指定值(int型)public static boolean isEven(int num){return num % 2 == 0;}public static boolean isOdd(int num){return num % 2 == 1;}public static boolean isPrime(int num){return havePrimeNumber(value);}// 判斷指定值(MyInteger型)public static boolean isEven(Test3_MyInteger mi){return mi.isEven(mi.value);}public static boolean isOdd(Test3_MyInteger mi){return mi.isOdd(mi.value);}public static boolean isPrime(Test3_MyInteger mi){return havePrimeNumber(mi.value);}// +: 判斷一個數(shù)是否是素數(shù)public static boolean havePrimeNumber(int num){boolean bool = true;for (int i = 2 ; i <= num / 2 ; i++){if (num % i != 0){bool = false;}}return bool;}// 判斷值是否相等public boolean equals(int num){return value == num;}public boolean equals(Test3_MyInteger mi){return this.value == mi.value;}// 數(shù)組字符構(gòu)成的數(shù)組轉(zhuǎn)為int值public static int parseInt(char[] arr){int length = arr.length;String str = "";for (int i = 0 ; i < length ; i++){str += arr[i];}return parseInt(str);}// 字符串轉(zhuǎn)intpublic static int parseInt(String str){int length = str.length(), result = 0;char temp;for (int i = 0 ; i < length ; i++){temp = str.charAt(i);result = result * 10 + temp - 48;}return result;} }

運行實例

1 false true true true false true true false true 123 123

UML

與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的Java黑皮书课后题第10章:10.3(MyInteger类)设计一个名为MyInteger的类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。