Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Lu Liu
- //CSC-112 Intermediate Java Programming
- //Feb 12 2016
- //lliu0001@student.stcc.edu
- //Design a class named MyInteger and then implement the class:
- //create object num1
- //Check methods: isOdd(), isEven(), and isPrime()
- //Check static methods: isOdd(int ), isEven(int ), and isPrime(int )
- //Check static methods: isOdd(MyInteger), isEven(MyInteger), and isPrime(MyInteger)
- //create object num2 and check method: equals (int )
- //create object num3 and check method equals (MyInteger)
- //Check methods parseInt(char []) and parseInt(String )
- package chapter09;
- import java.util.*;
- public class MyInteger {
- public static void main(String[] args) {
- //create object num1
- Scanner input = new Scanner(System.in);
- System.out.println("Enter a postive integer to create a MyInteger object (num1): ");
- int sc = input.nextInt();
- MyInteger num1 = new MyInteger(sc);
- //Check methods: isOdd(), isEven(), and isPrime()
- System.out.println("\nCheck methods: isOdd(), isEven(), and isPrime()");
- System.out.println(num1.getValue() + " is odd? " + num1.isOdd());
- System.out.println(num1.getValue() + " is even? " + num1.isEven());
- System.out.println(num1.getValue() + " is prime? " + num1.isPrime());
- //Check static methods: isOdd(int ), isEven(int ), and isPrime(int )
- System.out.println("\nCheck static methods: isOdd(int ), isEven(int ), and isPrime(int )");
- System.out.println(num1.getValue() + " is odd? " + MyInteger.isOdd(sc));
- System.out.println(num1.getValue() + " is even? " + MyInteger.isEven(sc));
- System.out.println(num1.getValue() + " is prime? " + MyInteger.isPrime(sc));
- //Check static methods: isOdd(MyInteger), isEven(MyInteger), and isPrime(MyInteger)
- System.out.println("\nCheck static methods: isOdd(MyInteger), isEven(MyInteger), and isPrime(MyInteger) ");
- System.out.println(num1.getValue() + " is odd? " + MyInteger.isOdd(num1.getValue()));
- System.out.println(num1.getValue() + " is even? " + MyInteger.isEven(num1.getValue()));
- System.out.println(num1.getValue() + " is prime? " + MyInteger.isPrime(num1.getValue()));
- //create object num2 and check method: equals (int )
- System.out.println("\nCheck methods: equals (int )");
- System.out.println("Enter a positive integers to check equals(int ): ");
- int sc1 = input.nextInt();
- System.out.println("Enter a positive integers to creat a MyInteger object (num2) ");
- MyInteger num2 = new MyInteger(input.nextInt());
- System.out.println(sc1 + " equals " + num2.getValue() + " ? " + num2.equals(sc1));
- //create object num3 and check method: equals (MyInteger)
- System.out.println("\nCheck methods equals (MyInteger)");
- System.out.println("Enter a positive integers to creat a MyInteger object (num3)");
- MyInteger num3 = new MyInteger(input.nextInt());
- System.out.println(num2.getValue() + " equals " + num3.getValue() + " ? " + num2.equals(num3.getValue()));
- //Check methods: parseInt(char []) and parseInt(String )
- System.out.println("\nCheck methods parseInt(char []) and parseInt(String )");
- char[] ch = { '1', '2', '3', '4' };
- System.out.println(MyInteger.parseInt(ch));
- String s = "5678";
- System.out.println(MyInteger.parseInt(s));
- }
- //Design the class
- private int value;
- public MyInteger(int value) {
- this.value = value;
- }
- public int getValue() {
- return this.value;
- }
- public boolean isEven() {
- if (value % 2 == 0) {
- return true;
- }
- return false;
- }
- public boolean isOdd() {
- if (value % 2 != 0) {
- return true;
- }
- return false;
- }
- public boolean isPrime() {
- for (int i = 2; i <= value / 2; i++) {
- if (value % i == 0) {
- return false;
- }
- }
- return true;
- }
- public static boolean isEven(int value) {
- if (value % 2 == 0) {
- return true;
- }
- return false;
- }
- public static boolean isOdd(int value) {
- if (value % 2 != 0) {
- return true;
- }
- return false;
- }
- public static boolean isPrime(int value) {
- for (int i = 2; i <= value / 2; i++) {
- if (value % i == 0) {
- return false;
- }
- }
- return true;
- }
- public static boolean isEven(MyInteger myInt) {
- return MyInteger.isEven(myInt.getValue());
- }
- public static boolean isOdd(MyInteger myInt) {
- return MyInteger.isOdd(myInt.getValue());
- }
- public static boolean isPrime(MyInteger MyInt) {
- return MyInteger.isPrime(MyInt.getValue());
- }
- public boolean equals(int value) {
- return this.value == value;
- }
- public boolean equals(MyInteger myInt) {
- return myInt.equals(myInt.getValue());
- }
- public static int parseInt(String s) {
- return Integer.parseInt(s);
- }
- public static int parseInt(char[] ch) {
- return parseInt(new String(ch));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement