For this lab you will create a simple abstract data type (ADT) called IntTrackMi
ID: 3753930 • Letter: F
Question
For this lab you will create a simple abstract data type (ADT) called IntTrackMinMax that takes integers one at a time and keeps track of the minimum and maximum values. Here is the API for this ADT Description Function constructor IntTrackMinMax() constructor check getMinint getMaxint getMax() toString String tostring() returns the string min,max] Signature compares i to the current minimum and maximum values and updates them accordingly returns the minimum value provided to check() so far returns the maximum value provided to check () so far void check(int i) getMin(o Your getMax() and getMin() functions may assume that check() has been called at least once. If getMin() or getMax() is called before the first call to check(), the results are undefined. constructor The constructor needs to initialize the current minimum and maximum to suitable initial values. In fact, if you set the class up correctly, you really don't even need an explicit constructor.Explanation / Answer
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Vamsi
*/
public class IntTrackMinMax {
int min,max;//variable declaration
int c;
IntTrackMinMax()
{
//setting initial values to 0
c=0;//if c=0 zero means min & max are not initialized
}
void check(int i)
{
//method to update min and max}
if(c==0)
{
//then intitializing min and max with i
min = max = i;
c=1;
}
else
{
//updating min and max
if(i<min)min=i;
if(max<i)max=i;
}
}
//method to return min
int getMin(){
return min;
}
//method to return max
int getMax()
{
return max;
}
//method to return both min and max in string format
@Override
public String toString()
{
String s = "["+min+","+max+"]" ;
return s;
}
//testing method
public static void main(String argv[])
{
IntTrackMinMax tmm = new IntTrackMinMax();
tmm.check(0);
tmm.check(5);
tmm.check(-5);
tmm.check(3);
tmm.check(-8);
System.out.println("Min :"+tmm.getMin());
System.out.println("Min :"+tmm.getMax());
System.out.println(tmm.toString());
}
}
//if you find this helpful, pls give a thumbsup, it helps me alot, thanks
//if you have any doubts, pls feel free to ask in comments
output:
run:
Min :-8
Min :5
[-8,5]
BUILD SUCCESSFUL (total time: 0 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.