欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java:,

来源: javaer 分享于  点击 31210 次 点评:67

java:,


设计名为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;
  }
}


相关文章

    暂无相关文章
相关栏目:

用户点评