Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.wit.comp1050.chunga.ica2.part2;
- public class MyInteger {
- // Private utility class constraints
- private final static int DEFAULT_VALUE = 0;
- private final static String DEFAULT_FORMAT = "%,d";
- // Instance variable
- private static int value;
- // Constructor to set default value
- public MyInteger() {
- this(DEFAULT_VALUE);
- value = DEFAULT_VALUE;
- }
- // Constructor to set specified value
- public MyInteger(int initialValue) {
- value = initialValue;
- }
- // Getter method
- public static int valueOf() {
- return value;
- }
- // Setter method
- public static int setValue(int initialValue) {
- return value = initialValue;
- }
- // Boolean check method for value
- public boolean equals(int otherValue) {
- return value == otherValue;
- }
- // String method
- @Override
- public String toString() {
- return toString(value);
- }
- // String format method
- private static String toString(int valueToFormat) {
- return String.format(DEFAULT_FORMAT, valueToFormat);
- }
- // Boolean check for if value is zero
- public boolean isZero() {
- return isZero(value);
- }
- // Boolean check for if testing value is zero
- public static boolean isZero(int valueToTest) {
- return valueToTest == 0;
- }
- // Boolean check for if value is even
- public boolean isEven() {
- return isEven(value);
- }
- // Boolean check for if testing value is even
- public static boolean isEven(int valueToTest) {
- return (valueToTest % 2) == 0;
- }
- // Boolean check for if value is odd
- public boolean isOdd() {
- return isOdd(value);
- }
- // Boolean check for if testing value is odd
- public static boolean isOdd(int valueToTest) {
- return (valueToTest % 2) != 0;
- }
- // Increases value by 1
- public int increment() {
- return ++value;
- }
- // Decreases value by 1
- public int decrement() {
- return --value;
- }
- // Method using + operator for value
- public int add(int adder) {
- value = value + adder;
- return value;
- }
- // Method using - operator for value
- public int subtract(int subtractor) {
- value = value - subtractor;
- return value;
- }
- // Method using * operator for value
- public int multiplyBy(int multiplier) {
- value = value * multiplier;
- return value;
- }
- // Method using / operator for value
- public int divideBy(int divisor) {
- value = value / divisor;
- return value;
- }
- // Method using % operator for value
- public int remainder(int remainder) {
- value = value % remainder;
- return value;
- }
- @Override
- public boolean equals(Object otherObject) {
- MyInteger otherMyInteger;
- if (otherObject instanceof MyInteger){
- otherMyInteger = (MyInteger) otherObject;
- } else {
- }
- return false;
- }
- public int add(MyInteger adder) {
- return 0;
- }
- public int subtract(MyInteger subtractor) {
- return 0;
- }
- public int multiplyBy(MyInteger multiplier) {
- return 0;
- }
- public int divideBy(MyInteger divisor) {
- return 0;
- }
- public boolean isPrime() {
- return isPrime(value);
- }
- public static boolean isPrime(MyInteger valueToTest) {
- if (valueToTest == 1 || valueToTest == 2) {
- return true;
- } else {
- for (int i = 2; i< valueToTest; i++) {
- if (i % valueToTest == 0) {
- return false;
- }
- }
- }
- return true;
- }
- public static int parseInt(char[] parser) {
- return 0;
- }
- // Main method to test all methods
- public static void main(String[] args) {
- MyInteger value = new MyInteger();
- System.out.println("The default value is: " + value);
- System.out.println("Is value equal to 0? " + value.isZero());
- System.out.println("Is value even? " + value.isEven());
- System.out.println("Is value odd? " + value.isOdd());
- System.out.println("Is value equal to 5? " + value.equals(5));
- System.out.println("");
- MyInteger integer = new MyInteger(5);
- System.out.println("The specified integer is: " + integer);
- System.out.println("With specified integer, value has been changed to: " + valueOf());
- System.out.println("Value has been changed to " + setValue(1000));
- System.out.println("Value has incremented, the new value is: " + value.increment());
- System.out.println("Value has decremented, the new value is: " + value.decrement());
- System.out.println("Value in string format with grouping separators is: " + value.toString());
- System.out.println("");
- System.out.println("5000 in string form with grouping seperators: " + MyInteger.toString(5000));
- System.out.println("");
- System.out.println("Is 400 equal to 0? " + isZero(400));
- System.out.println("Is 400 even? " + isEven(400));
- System.out.println("Is 400 odd? " + isOdd(400));
- System.out.println("");
- System.out.println("Value is currently: " + value);
- System.out.println("Value added by 5 equals: " + value.add(5));
- System.out.println("Value subtracted by 4 equals: " + value.subtract(4));
- System.out.println("Value multipled by 2 equals: " + value.multiplyBy(2));
- System.out.println("Value divided by 2 equals: " + value.divideBy(2));
- System.out.println("The remainder of " + value + " divided by 2 equals: " + value.remainder(2));
- System.out.println("The new value is now: " + value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement