Exercise 11-1. Create a class called Integer that has a single, private data mem
ID: 3716821 • Letter: E
Question
Exercise 11-1. Create a class called Integer that has a single, private data member of type
int . Provide a class constructor that outputs a message when an object is created. Define
function members to get and set the data member, and to output its value. Write a test program
to create and manipulate at least three Integer objects, and verify that you can’t assign a value
directly to the data member. Exercise all the class function members by getting, setting, and
outputting the value of the data member of each object.
Exercise 11-2. Modify the constructor for the Integer class in the previous exercise so that
the data member is initialized to zero in the constructor initialization list and implement a
copy constructor. Add a function member that compares the current object with an Integer
object passed as an argument. The function should return –1 if the current object is less than
the argument, 0 if they = objects are equal, and +1 if the current object is greater than the
argument. Try two versions of the Integer class, one where the compare() function argument is
passed by value and the other where it is passed by reference. What do you see output from the
constructors when the function is called? Make sure that you understand why this is so.
You can’t have both functions present in the class as overloaded functions. Why not?
Exercise 11-3. Implement function members add() , subtract() , and multiply() for the
Integer class that will add, subtract, and multiply the current object by the value represented
by the argument of type Integer . Demonstrate the operation of these functions in your class
with a version of main() that creates Integer objects encapsulating values 4, 5, 6, 7, and 8, and
then uses these to calculate the value of 4*5^3 +6* 5^2 +7*5+8. Implement the functions so that the
calculation and the output of the result can be performed in a single statement.
Explanation / Answer
11.1
/**
*
*/
/**
* @author yourname
*
*/
public class Integer {
//Data member
private int value;
//constructor
public Integer() {
System.out.println("... A new Integer Object is created ...");
}
//getters and setters
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
IntegerTester.java
/**
*
*/
/**
* @author name
*
*/
public class IntegerTester {
/**
* @param args
*/
public static void main(String[] args) {
//Create a Integer Object
Integer a=new Integer();
//can not assign value dirctely
//call setter method to set the values
a.setValue(10);
System.out.println(" Integer value is "+a.getValue());
}
}
output:
... A new Integer Object is created ...
Integer value is 10
11.2
/**
*
*/
/**
* @author yourname
*
*/
public class Integer {
//Data member
private int value;
//constructor
public Integer() {
this.value=0; //initialize value to 0
System.out.println("... A new Integer Object is created ...");
}
//copy constructor
public Integer(Integer obj)
{
this.value=obj.value;
compare(obj);
//compare(obj.value); // error The method compare(Integer) in the type Integer is not applicable for the arguments (int)
}
//getters and setters
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
//compare method
/**
*
*/
public int compare(Integer obj) {
int diff=this.getValue()-obj.getValue();
if(diff<0)
{
diff=-1;
}
else if (diff>0)
{
diff=1;
}
else
{
diff=0;
}
return diff;
}
}
/**
*
*/
/**
* @author name
*
*/
public class IntegerTester {
/**
* @param args
*/
public static void main(String[] args) {
//Create a Integer Object
Integer a=new Integer();
//can not assign value dirctely
//call setter method to set the values
a.setValue(10);
System.out.println(" Integer value is "+a.getValue());
//Create one more object using copy constructor
Integer b=new Integer(a);
//Call compare method by value
//a.compare(b.getValue());
//The method compare(Integer) in the type Integer is not applicable for the arguments (int) this error will occur
//Call compare method by Reference
System.out.println("Object Comparision "+a.compare(b));
}
}
Output
... A new Integer Object is created ...
Integer value is 10
Object Comparision 0
11.3
/**
*
*/
/**
* @author yourname
*
*/
public class Integer {
//Data member
private int value;
//constructor
public Integer() {
this.value=0; //initialize value to 0
System.out.println("... A new Integer Object is created ...");
}
//copy constructor
public Integer(Integer obj)
{
this.value=obj.value;
compare(obj);
//compare(obj.value); // error The method compare(Integer) in the type Integer is not applicable for the arguments (int)
}
//getters and setters
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
//compare method
/**
*
*/
public int compare(Integer obj) {
int diff=this.getValue()-obj.getValue();
if(diff<0)
{
diff=-1;
}
else if (diff>0)
{
diff=1;
}
else
{
diff=0;
}
return diff;
}
/**
* method to add
*/
public Integer add(Integer obj) {
this.value+=obj.getValue();
return this;
}
/**
* method to subtract
*/
public Integer sub(Integer obj) {
this.value-=obj.getValue();
return this;
}
/**
* method to multiply
*/
public Integer multiply(Integer obj) {
this.value*=obj.getValue();
return this;
}
@Override
public String toString() {
return ""+value;
}
}
/**
*
*/
/**
* @author name
*
*/
public class IntegerTester {
/**
* @param args
*/
public static void main(String[] args) {
//Create a Integer Objects
Integer a=new Integer();
a.setValue(4);
Integer b=new Integer();
b.setValue(5);
Integer c=new Integer();
c.setValue(6);
Integer d=new Integer();
d.setValue(7);
Integer e=new Integer();
e.setValue(8);
// /4*5^3 +6* 5^2 +7*5+8
System.out.println("Expression value in Single Statement is ");
System.out.println(a.multiply(b).multiply(b).multiply(b).add(c.multiply(b).multiply(b)).add(d.multiply(b)).add(e));
}
}
Output
... A new Integer Object is created ...
... A new Integer Object is created ...
... A new Integer Object is created ...
... A new Integer Object is created ...
... A new Integer Object is created ...
Expression value in Single Statement is
693
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.