Advertisement
luliu

Design a class named MyInteger and then implement the class

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