Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class MyInteger { private int value; public MyInteger(int val) { value = val; } public int getValue() { return value; } public int setValue(int

image text in transcribed

public class MyInteger {

private int value;

public MyInteger(int val) {

value = val;

}

public int getValue() {

return value;

}

public int setValue(int set) {

value = set;

return value;

}

public boolean equals(int eq) {

if(eq == value) {

return true;

}

else

return false;

}

public String toString() {

String str = Integer.toString(value);

return str;

}

public boolean isZero() {

if (value == 0) {

return true;

}

return false;

}

public boolean isEven() {

if ((value%2) == 0) {

return true;

}

return false;

}

public boolean isOdd(){

if((value%2) == 1) {

return true;

}

return false;

}

public static boolean isZero(int myInt) {

if (myInt == 0) {

return true;

}

return false;

}

public static boolean isEven(int myInt) {

return (myInt % 2) == 0;

}

public static boolean isOdd(int myInt) {

return (myInt % 2) == 1;

}

public static boolean isEven(MyInteger myInt) {

return myInt.isEven();

}

public static boolean isOdd(MyInteger myInt) {

return myInt.isOdd();

}

public int intIncrement() {

return setValue(value + 1);

}

public int intDecrement() {

return setValue(value - 1);

}

public int add(int myInt) {

value = value + myInt;

return value;

}

public int subtract(int myInt) {

value = value - myInt;

return value;

}

public int multiplyBy(int myInt) {

value = value * myInt;

return value;

}

public int divideBy(int myInt) {

value = value / myInt;

return value;

}

public int remainder(int myInt) {

value = value % myInt;

return value;

}

public static void main(String[] args) {

MyInteger int1 = new MyInteger(0);

MyInteger int2 = new MyInteger(-56);

MyInteger int3 = new MyInteger(2);

MyInteger int4 = new MyInteger(23);

MyInteger int5 = new MyInteger(2300);

System.out.printf("%d is zero? %s%n", int1.getValue(), int1.isZero());

System.out.printf("%d is even? %s%n", int2.getValue(), int2.isEven());

System.out.printf("%d is odd? %s%n", int3.getValue(), int3.isOdd());

System.out.println();

System.out.println("Incrementing the value by one, Old value : " + int3.value + " new value " + int3.intIncrement());

System.out.println("Decrementing the value by one, Old value : " + int3.value + " new value " + int3.intDecrement());

System.out.println();

System.out.println("Adding 5 to " + int4.value + " = " + int4.add(5));

System.out.println("Subtracting 5 from " + int4.value + " = " + int4.subtract(5));

System.out.println("Multiplying " + int4.value + " by 5" + " = " + int4.multiplyBy(5));

System.out.println("Dividing " + int4.value + " by 5" + " = " + int4.divideBy(5));

System.out.println("Remainder from / " + int4.value + " by 5" + " = " + int4.remainder(5));

System.out.println();

System.out.printf(" Static 93 is odd? %s%n", MyInteger.isOdd(93));

System.out.printf(" Static 93 is even? %s%n", MyInteger.isEven(93));

System.out.printf(" Static 93 is zero? %s%n", MyInteger.isZero(93));

System.out.printf(" %d Equals %d? %s%n", int1.getValue(), int4.getValue(), int1.equals(int4));

System.out.println("Output in String " + int5.toString());

}

}

Add the following to class Mylnteger: The method equals (Object) that return true if the value in this object is equal to the value of the specified instance (the parameter instance of Mylnteger). This method will override object's equals() method The methods add (Mylnteger), subtract Mylnteger), multiplyBy(Mylnteger), divideBy(Mylnteger), and remainder (Mylnteger), corresponding to the five arithmetic operators These methods should both modify value in this instance and return its new value. The specified instance (parameter) is unchanged The methods is Prime() and is Prime Mylnteger) that return true if the value in this object or the specified instance (parameter), respectively, is a prime number. Prime() is an instance method and is Prime (Mylnteger) is a static method The static methods parselnt(charl) that converts an array of numeric characters to an int value and parselnt(String) that converts a String of numeric characters to an int value Javad oc comments (see the documentation on BB and the Time class for some examples)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Object Databases The Essentials

Authors: Mary E. S. Loomis

1st Edition

020156341X, 978-0201563412

More Books

Students also viewed these Databases questions