In: Computer Science
/*Design and code and test a class MaxMin that has the
following properties * two integer member variables, min and max
where min is smaller or equal than max at all times * A default
constructor that sets both min and max to 0 * A constructor that
accepts one integer and sets both min and max to that integer * A
constructor that accepts two integers and sets min the smallest and
max the largest * * Setters and getters. * Setter for min: if you
try to set min to more than max then min is set to the old max and
max * is set to the new value: example min = 2 and max =10, if you
invode .setMin(12), then * min is set to 10 and max is set to 12. *
Same for setMax, if you try to set max to a value smaller than min,
then max is set to old min * and min is set to the new value. * *
toString() that returns min max as couple (2, 10) (min, max) * *
method MinMax add(int a) that returns a MinMax object with min and
max augmented by a * method MinMax add(MinMax b) that returns a
MinMax object that is the sum of this and b * method MinMax add(int
a, int b) that returns a MinMax object where min(a, b) is added to
min and * max(a, b) is added to max of this. Example. M = new
MinMax(2, 5); * T = M.add(4, 3), then T will be (5, 9) (2+3, 5+4).
* -equals method boolean equals(MaxMin M) that returns true if M is
equal to this false if not. * Test all methods. * */ public class
MaxMin { private int min, max; // A default constructor that sets
both min and max to 0 public MaxMin() { min = max = 0; } // A
constructor that accepts one integer and sets both min and max to
that integer public MaxMin(int a) { min = max = a; } // A
constructor that accepts two integers and sets min the smallest and
max the largest public MaxMin(int a, int b) { this.setMinMax(a, b);
} //setter for both minmax public void setMinMax(int a, int b) {
if(a < b) { min = a; max = b; }else { min = b; max = a; } }
//getter public int getMin() { return min; } public int getMax() {
return max; } //Setter for min: if you try to set min to more than
max then min is set to the old max and max // * is set to the new
value: example min = 2 and max =10, if you invode .setMin(12), then
// * min is set to 10 and max is set to 12. public void setMin(int
a) { //if a is larger than max, mnin has to be equal max and max to
a /*Eitehr uyou di it this way * if(a > this.max)
setMinMax(this.max, a); else setMinMax(a, this.max); */ //OR this
way if(a > this.max) { this.min = this.max; this.max = a; }else
this.min = a; } //* Same for setMax, if you try to set max to a
value smaller than min, then max is set to old min // * and min is
set to the new value. // * public void setMax(int a) { //if a is
smaller than min, then set max to min and min to a /*Either you do
it this way if(a < this.min) setMinMax(a, this.min); else
setMinMax(this.min, a); */ //OR this way if(a < this.min) {
this.max = this.min; this.min = a; }else this.max = a; } //toString
public String toString() { return "(" + min + ", " + max + ")"; }
//method MaxMin add(int a) that returns a MaxMin object with min
and max augmented by a public MaxMin add(int a) { MaxMin res = new
MaxMin(); res.setMinMax(this.min+a, this.max+a); return res; /*You
can do it in one line. return new MaxMin(this.min+a, this.max+a);
*/ } //* method MaxMin add(MaxMin b) that returns a MinMax object
that is the sum of this and b public MaxMin add(MaxMin b) { //this
(2, 5) b (3, 7) =>> return object should be (5, 12) //how
many objects am I going to be dealing with here? MaxMin res = new
MaxMin(this.min+b.min, this.max + b.max); return res; } //* method
MaxMin add(int a, int b) that returns a MinMax object where min(a,
b) is added to min and public MaxMin add(int a, int b) { if (a >
b) { MaxMin res = new MaxMin(this.min+b, this.max + a); return res;
}else { MaxMin res = new MaxMin(this.min+a, this.max + b); return
res; } } public boolean equals(MaxMin m) { if(this.min == m.min
&& this.max == m.max) return true; return false; } }
language Java and set to even and change accordingly. Thank you.
I have added the below correct formatted code with the required changes. I have added the main method as well and tested the properties and functions in it. I have added the comments as well in the code explaining what each line of code is doing and what result is expected.
public class MaxMin {
private int min,
max;
public void MaxMin() {
min = 0;
max = 0;
}
public void MaxMin(int input1) {
min = input1;
max = input1;
}
public void MaxMin(int input1, int input2) {
if (input1 > input2) {
min = input2;
max = input1;
}
else {
min = input1;
max = input2;
}
}
public void setMin(int x) {
if (x > max) {
min = max;
max = x;
}
else {
min = x;
}
}
public int getMin() {
return min;
}
public void setMax(int x) {
if (x < min) {
max = min;
min = x;
}
else {
max = x;
}
}
public int getMax() {
return max;
}
public String toString() {
return "(" + min + ", " + max + ")";
}
public MaxMin add(int a) {
MaxMin res = new MaxMin();
res.MaxMin(this.min + a, this.max + a);
return res;
}
public MaxMin add(MaxMin b) {
MaxMin res = new MaxMin();
res.MaxMin(this.min + b.min, this.max + b.max);
return res;
}
public MaxMin add(int a, int b) {
MaxMin res = new MaxMin();
if (a > b) {
res.MaxMin(this.min + b, this.max + a);
return res;
} else {
res.MaxMin(this.min + a, this.max + b);
return res;
}
}
public boolean equals(MaxMin m) {
if (this.min == m.min && this.max == m.max) return true;
else return false;
}
public static void main(String[] args) {
MaxMin test1 = new MaxMin(); // MaxMin Class first Instance
MaxMin new1 = new MaxMin(); // MaxMin Class second Instance
MaxMin new2 = new MaxMin(); // MaxMin Class third Instance
new1.MaxMin(5, 20); // Initializing second instance with min=5 & max=20
new2.MaxMin(20, 10); // Initializing third instance with min=20 & max=5
test1.MaxMin(5); // Initializing first instance with setting min & max to 5
System.out.println(test1.getMin()); // It should return 5
System.out.println(test1.getMax()); // It should return 5
System.out.println();
test1.MaxMin(10, 15); // Setting min=10 & max=15
System.out.println(test1.getMin()); // It should return 10
System.out.println(test1.getMax()); // It should return 15
System.out.println();
test1.setMin(20); // Setting min=20. But, it is greater than max, hence min=15 and max=20
System.out.println(test1.getMin()); // It should return 15
System.out.println(test1.getMax()); // It should return 20
System.out.println();
test1.setMin(10); // Setting min=10
System.out.println(test1.getMin()); // It should return 10
System.out.println(test1.getMax()); // It should return 20
System.out.println();
test1.setMax(5); // Setting max=5. But, it is smaller than min, hence min=5 and max=10
System.out.println(test1.getMin()); // It should return 5
System.out.println(test1.getMax()); // It should return 10
System.out.println();
test1.setMax(20); // Setting max=20
System.out.println(test1.getMin()); // It should return 5
System.out.println(test1.getMax()); // It should return 20
System.out.println();
System.out.println(test1.toString()); // It should return (5,20)
System.out.println();
System.out.println(test1.add(4)); // It should return (9,24) as we are adding 4 to the min & max
System.out.println();
/* It should return (5+5=10,20+20=40) as we are adding new1(5,20) to the test1(5,20) */
System.out.println(test1.add(new1)); // It should return (45,70)
System.out.println();
/* It should return (5+10=15,20+20=40) as we are adding (10,20) to the test1(5,20)*/
System.out.println(test1.add(10, 20)); // It should return (15,40)
System.out.println();
// If new1(5,20) is equal to test1(5,20), it will return true
System.out.println(test1.equals(new1)); // It should return true
System.out.println();
// If new2(10,20) is equal to test1(5,20), it will return true; else false
System.out.println(test1.equals(new2)); // It should return false
}
}