luliu

Design a class named MyInteger and then implement the class

Feb 12th, 2016
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.69 KB | None | 0 0
  1. //Lu Liu
  2. //CSC-112 Intermediate Java Programming
  3. //Feb 12 2016
  4.  
  5. //Design a class named MyInteger and then implement the class:
  6. //create object num1
  7. //Check methods: isOdd(), isEven(),  and isPrime()
  8. //Check static  methods: isOdd(int ), isEven(int ), and isPrime(int )
  9. //Check static  methods: isOdd(MyInteger), isEven(MyInteger), and isPrime(MyInteger)
  10. //create object num2 and check method: equals (int )
  11. //create object num3 and check method equals (MyInteger)
  12. //Check methods parseInt(char []) and  parseInt(String )
  13.  
  14. package chapter09;
  15.  
  16. import java.util.*;
  17.  
  18. public class MyInteger {
  19.     public static void main(String[] args) {
  20.         //create object num1
  21.         Scanner input = new Scanner(System.in);
  22.         System.out.println("Enter a postive integer to create a MyInteger object (num1): ");
  23.         int sc = input.nextInt();
  24.         MyInteger num1 = new MyInteger(sc);
  25.        
  26.         //Check methods: isOdd(), isEven(),  and isPrime()
  27.         System.out.println("\nCheck methods: isOdd(), isEven(),  and isPrime()");
  28.         System.out.println(num1.getValue() + " is odd? " + num1.isOdd());
  29.         System.out.println(num1.getValue() + " is even? " + num1.isEven());
  30.         System.out.println(num1.getValue() + " is prime? " + num1.isPrime());
  31.        
  32.         //Check static  methods: isOdd(int ), isEven(int ), and isPrime(int )
  33.         System.out.println("\nCheck static  methods: isOdd(int ), isEven(int ), and isPrime(int )");
  34.         System.out.println(num1.getValue() + " is odd? " + MyInteger.isOdd(sc));
  35.         System.out.println(num1.getValue() + " is even? " + MyInteger.isEven(sc));
  36.         System.out.println(num1.getValue() + " is prime? " + MyInteger.isPrime(sc));
  37.        
  38.         //Check static  methods: isOdd(MyInteger), isEven(MyInteger), and isPrime(MyInteger)
  39.         System.out.println("\nCheck static  methods: isOdd(MyInteger), isEven(MyInteger), and isPrime(MyInteger) ");
  40.         System.out.println(num1.getValue() + " is odd? " + MyInteger.isOdd(num1.getValue()));
  41.         System.out.println(num1.getValue() + " is even? " + MyInteger.isEven(num1.getValue()));
  42.         System.out.println(num1.getValue() + " is prime? " + MyInteger.isPrime(num1.getValue()));
  43.  
  44.         //create object num2 and check method: equals (int )
  45.         System.out.println("\nCheck methods: equals (int )");
  46.         System.out.println("Enter a positive integers to check equals(int ): ");
  47.         int sc1 = input.nextInt();
  48.         System.out.println("Enter a positive integers  to creat a MyInteger object (num2) ");
  49.         MyInteger num2 = new MyInteger(input.nextInt());
  50.         System.out.println(sc1 + " equals " + num2.getValue() + " ? " + num2.equals(sc1));
  51.        
  52.         //create object num3 and check method: equals (MyInteger)
  53.         System.out.println("\nCheck methods equals (MyInteger)");
  54.         System.out.println("Enter a positive integers  to creat a MyInteger object (num3)");
  55.         MyInteger num3 = new MyInteger(input.nextInt());
  56.         System.out.println(num2.getValue() + " equals " + num3.getValue() + " ? " + num2.equals(num3.getValue()));
  57.        
  58.         //Check methods: parseInt(char []) and  parseInt(String )
  59.         System.out.println("\nCheck methods parseInt(char []) and  parseInt(String )");
  60.         char[] ch = { '1', '2', '3', '4' };
  61.         System.out.println(MyInteger.parseInt(ch));
  62.         String s = "5678";
  63.         System.out.println(MyInteger.parseInt(s));
  64.     }
  65.  
  66.    
  67.      //Design the class
  68.     private int value;
  69.  
  70.     public MyInteger(int value) {
  71.         this.value = value;
  72.     }
  73.  
  74.     public int getValue() {
  75.         return this.value;
  76.     }
  77.  
  78.     public boolean isEven() {
  79.         if (value % 2 == 0) {
  80.             return true;
  81.         }
  82.         return false;
  83.     }
  84.  
  85.     public boolean isOdd() {
  86.         if (value % 2 != 0) {
  87.             return true;
  88.         }
  89.         return false;
  90.     }
  91.  
  92.     public boolean isPrime() {
  93.         for (int i = 2; i <= value / 2; i++) {
  94.             if (value % i == 0) {
  95.                 return false;
  96.             }
  97.         }
  98.         return true;
  99.     }
  100.  
  101.     public static boolean isEven(int value) {
  102.         if (value % 2 == 0) {
  103.             return true;
  104.         }
  105.         return false;
  106.     }
  107.  
  108.     public static boolean isOdd(int value) {
  109.         if (value % 2 != 0) {
  110.             return true;
  111.         }
  112.         return false;
  113.     }
  114.  
  115.     public static boolean isPrime(int value) {
  116.         for (int i = 2; i <= value / 2; i++) {
  117.             if (value % i == 0) {
  118.                 return false;
  119.             }
  120.         }
  121.         return true;
  122.     }
  123.  
  124.     public static boolean isEven(MyInteger myInt) {
  125.         return MyInteger.isEven(myInt.getValue());
  126.     }
  127.  
  128.     public static boolean isOdd(MyInteger myInt) {
  129.         return MyInteger.isOdd(myInt.getValue());
  130.     }
  131.  
  132.    
  133.     public static boolean isPrime(MyInteger MyInt) {
  134.         return MyInteger.isPrime(MyInt.getValue());
  135.     }
  136.    
  137.  
  138.     public boolean equals(int value) {
  139.         return this.value == value;
  140.     }
  141.  
  142.     public boolean equals(MyInteger myInt) {
  143.         return myInt.equals(myInt.getValue());
  144.     }
  145.  
  146.     public static int parseInt(String s) {
  147.         return Integer.parseInt(s);
  148.     }
  149.  
  150.     public static int parseInt(char[] ch) {
  151.         return parseInt(new String(ch));
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment