Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a MeasurableSet class that stores and analyzes Measurable objects. The Me

ID: 664234 • Letter: C

Question

Create a MeasurableSet class that stores and analyzes Measurable objects. The MeasurableSet class should have three instance variables:

ArrayList<Measurable> that holds all Measurable objects that have been added to the MeasurableSet

Measurable min that holds the object with the smallest measure that has been added to the MeasurableSet

Measurable max that holds the object with the largest measure that has been added to the MeasurableSet .


The MeasurableSet class has the following constructor
MeasurableSet ()

The MeasurableSet class has the following methods

// mutator to add a new Measurable object to the MeasurableSet . Set min if the object is smaller than any previously seen object, max if the object is larger than any previously seen object.
void add(Measurable m)

// accessor to return the min object
Measurable getMin()

// accessor to return the max object
Measurable getMax()

// accessor to return the Measurable objects in the MeasurableSet whose Measurable value is greater than or equal to floor, and less than or equal to ceiling
ArrayList<Measurable> getMiddle(int floor, int ceiling)

5: Write a class with a main method. The main method should create a MeasurableSet object, and four objects from the Wall/Field/Word classes that you created for Steps 2 and 3. Add these object to the MeasurableSet, and print the smallest and largest values returned from the MeasurableSet. Test the getMiddle method. (Does it make any sense to mix Wall, Field and Word objects in the same MeasurableSet? Does the MeasurableSet object care? By implementing Measurable, applications using Wall, Field or Word objects can use the exact same MeasurableSet to analyze data.)

Explanation / Answer

Measurable.java

package chegg_samples;

import java.util.ArrayList;
import java.util.Collections;

/**
*
* @author prmsh
*/
class Measurable {
int measurable;
ArrayList<Measurable> MeasurableElements =new ArrayList<Measurable>();
Measurable(int measurable){ //constructor
this.measurable=measurable;   
}
  

Measurable() {//default construct
this.measurable=0;
}
  
public void add(Measurable m) {
MeasurableElements.add(m);
}
Measurable getMin(){ //minimum function
Measurable min = new Measurable(Integer.MAX_VALUE);
for(int i=0; i<MeasurableElements.size(); i++){
if(MeasurableElements.get(i).measurable < min.measurable){
min = MeasurableElements.get(i);
}
}
return min;
}
Measurable getMax(){ //maximum function
Measurable max = new Measurable(Integer.MIN_VALUE);
for(int i=0; i<MeasurableElements.size(); i++){
if(MeasurableElements.get(i).measurable < max.measurable){
max = MeasurableElements.get(i);
}
}
return max;
}
ArrayList<Measurable> getMiddle(int floor, int ceiling){//to get elements between range
ArrayList<Measurable> middleelements =new ArrayList<Measurable>();
for(int i=0; i<MeasurableElements.size(); i++){
if(MeasurableElements.get(i).measurable > floor && MeasurableElements.get(i).measurable < ceiling){
middleelements.add(MeasurableElements.get(i));
}
}
return middleelements;
}
}

MeasurableSet.java

package chegg_samples;

import java.util.ArrayList;

/**
*
* @author prmsh
*/
public class MeasurableSet {
  
public static void main(String args[]){
Measurable wall=new Measurable(3);//creating measurable objects
Measurable field=new Measurable(2);
Measurable word=new Measurable(4);
Measurable four=new Measurable(1);
wall.add(wall);//adding to arralylist
word.add(word);
field.add(field);
four.add(four);
Measurable min = new Measurable();
min.getMin();//calling function
System.out.println("Min element value is"+min.measurable);
Measurable max = new Measurable();
max.getMax();//calling max function
System.out.println("Min element value is"+max.measurable);
Measurable dummy = new Measurable();
ArrayList<Measurable> middleElements = dummy.getMiddle(1,4);
System.out.println("Here are middle elements");
for(int i=0; i<middleElements.size(); i++){//printing middle elements
System.out.println(middleElements.get(i).measurable);
}
}
}