An int data field/instance variable named value that stores the int value repres
ID: 3847728 • Letter: A
Question
An int data field/instance variable named value that stores the int value represented by this object.
A constructor that creates a MyInteger object for a default int value.
A constructor that creates a MyInteger object for a specified int value.
A getter method, valueOf(), that returns value.
A setter method, setValue(int), that replaces the current value with the specified one.
The method equals(int) that return true if the value in this object is equal to the specified value. This method will not override Object’s equals() method.
The method toString() that returns a text (String) version of value. toString() must include thousands grouping separators when value is greater than 999. This method will override Object’s toString() method.
The methods isZero(), isEven() and isOdd() that return true if the value in this object is zero, even or odd, respectively.
The static methods isZero(int), isEven(int) and isOdd(int) that return true if the specified value is zero, even or odd, respectively.
The methods increment() and decrement() that increase or decrease value by 1, respectively. These methods should both modify value and return its new value.
The methods add(int), subtract(int), multiplyBy(int), divideBy(int), and remainder(int), corresponding to the five arithmetic operators (+, -, *, /, %). These methods should both modify value and return its new value.
Explanation / Answer
class MyInteger
{
private int value;
public MyInteger()//default constructor
{
value = 0;
}
public MyInteger(int value)//argument constructor
{
this.value = value;
}
//set and get methods
public int valueOf()
{
return value;
}
public void setValue(int value)
{
this.value = value;
}
public boolean equals(int x)
{
if(this.value == x)
return true;
else
return false;
}
public String toString()
{
int x;
if(value <=999)
return " "+value;
else
x = value/1000;
return " "+x+","+(value-x*1000);
}
//methods to check if My Integer object is 0,even or odd
public boolean isZero()
{
if(value == 0)
return true;
else
return false;
}
public boolean isEven()
{
if(value % 2 == 0)
return true;
else
return false;
}
public boolean isOdd()
{
if(value % 2 != 0)
return true;
else
return false;
}
//static methods
public static boolean isZero(int value)
{
if(value == 0)
return true;
else
return false;
}
public static boolean isEven(int value)
{
if(value % 2 == 0)
return true;
else
return false;
}
public static boolean isOdd(int value)
{
if(value % 2 != 0)
return true;
else
return false;
}
//increment and decrement methods
public int increment()
{
return ++value;
}
public int decrement()
{
return --value;
}
//arithmetic methods
public int add(int x)
{
return (value + x);
}
public int subtract(int x)
{
return(value -x);
}
public int multiplyBy(int x)
{
return (value*x);
}
public int divideBy(int x)
{
return (value/x);
}
public int remainder(int x)
{
return (value%x);
}
}
class TestInteger
{
public static void main (String[] args)
{
MyInteger myint = new MyInteger(4567);
System.out.println("myint = "+myint);
System.out.println("myint is even :"+myint.isEven());
System.out.println("increment myint :"+myint.increment());
System.out.println("myint divided by 5 :"+myint.divideBy(5));
}
}
Output:
myint = 4,567
myint is even :false
increment myint :4568
myint divided by 5 :913
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.