Question

In: Computer Science

/*Design and code and test a class MaxMin that has the following properties * two integer...

/*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.

Solutions

Expert Solution

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

  }
}

Related Solutions

I. Design and code a class Rectangle that has the following properties: Two private members double...
I. Design and code a class Rectangle that has the following properties: Two private members double length and width A constructor that accepts two parameters for the private members Default constructor that sets both members to 0 Setters/getters for each private member Method area() that returns the area of the rectangle Method Perim() that returns the perimeter of the rectangle Method toString that return the param as a String Method Add that accepts a Rectangle and returns a rectangle with...
/*Design and code a class Calculator that has the following * tow integer member variables, num1...
/*Design and code a class Calculator that has the following * tow integer member variables, num1 and num2. * - a method to display the sum of the two integers * - a method to display the product * - a method to display the difference * - a method to display the quotient * - a method to display the modulo (num1%num2) * - a method toString() to return num1 and num2 in a string * - Test your...
Java program Test Scores? Design a TestScore class that has three integer fields, each holding a...
Java program Test Scores? Design a TestScore class that has three integer fields, each holding a test score. The class should have accessor and mutator methods for the test score fields and a method that returns the average of the test scores as a double. Test the TestScore class by writing a separate program that creates an instance of the class. The program should ask the user to enter three test scores, which should be stored in the TestScore object....
Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.”
Programming Problem 2 - Cycle[A] Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.” Create a constructor with two parameters, using the same variable names in the parameter list. Assign each variable to numberOfWheels” and “weight” respectively. Write a separate application to test the class and display its properties. Note: Do not change the names of the instance variables or the variables listed in the constructor’s parameter list.[B] Edit your class Cycle by...
*Please give the answers in pseudo code 1) Design an algorithm that will receive two integer...
*Please give the answers in pseudo code 1) Design an algorithm that will receive two integer items from a terminal operator, and display to the screen their sum, difference, product and quotient. Note that the quotient calculation (first integer divided by second integer) is only to be performed if the second integer does not equal zero. 2) Design an algorithm that will read two numbers and an integer code from the screen. The value of the integer code should be...
Write the class declarations for the following objects' descriptions. NO CODE. That means only properties and...
Write the class declarations for the following objects' descriptions. NO CODE. That means only properties and method declarations. Like... class Lamp { int mBulbs; void Explode(); }; 1) A Laptop has a color, brand, and processor speed. You can open, close, turn on, and turn off a laptop. 2) A monitor has a screen size and a brightness. 3) Oh wait, a laptop has a monitor itself. To do this, would you add a line to answer #1 or answer...
Program Specification Design an inventory class that stores the following members: serialNum: An integer that holds...
Program Specification Design an inventory class that stores the following members: serialNum: An integer that holds a part's serial number. manufactDate: A member that holds the date the part was manufactured. lotNum: An integer that holds the part's lot number. The class should have appropriate setter and getter functions. Next, design a stack class that can hold objects of the class described above. If you wish, you may use the linked list from program 5 as a basis for designing...
The Account class Create a class named Account, which has the following private properties:
in java The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber (), getBalance (), setBalan newBalance). There is no setNumber () once an account is created, its account number cannot change. Now implement these methods: void deposit (double amount) and void withdraw (double amount). For both these methods, if the amount is less than...
The Account class Create a class named Account , which has the following private properties:
 The Account class Create a class named Account , which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNunber(), getBalance(), setBalance (double newBalance) . There is no setNunber() - once an account is created, its account number cannot change. Now implement these methods: void deposit (double anount) and void withdraw(double anount). For both these methods, if the amount is less than zero,...
Complete the following C++ tasks: a. Design a class named BaseballGame that has fields for two...
Complete the following C++ tasks: a. Design a class named BaseballGame that has fields for two team names and a final score for each team. Include methods to set and get the values for each data field. Create the class diagram and write the pseudocode that defines the class. b. Design an application that declares three BaseballGame objects and sets and displays their values. c. Design an application that declares an array of 12 BaseballGame objects. Prompt the user for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT