Write a program NumberList that computes the largest and smallest values in a se
ID: 3924267 • Letter: W
Question
Write a program NumberList that computes the largest and smallest values in a sequence of numbers. Supply methods void addValue(int x) int getLargest() int getSmallest() Keep track of the smallest and largest values that you've seen so far. Then use the Math.min and Math.max methods to update them in the addValue method. Write a test program NumberListTester that asking the user to insert four integers number, then for reach number you should call addValue. Lastly, you should print the maximum and the minimum numbers. Write a program HousePicture that draws a picture of a house using Rectangle object. N.B. It is very important that in this assignment you use correctly the following: Constructors Private variables Public methods this keyword (if needed)Explanation / Answer
Hi, Please find my implementtion for problem 1:
Please let me know in case of any problem:
############ NumberList.java #################
public class NumberList {
// instant variables of class numberList
private int max;
private int min;
// constructor
public NumberList(){
this.min = Integer.MAX_VALUE; // initializing min with maximum value
this.max = Integer.MIN_VALUE;// initializing max with minimum value
}
/**
* @param x
*/
public void addValue(int x){
// evaluating max with x
this.max = Math.max(max, x);
// evaluating min
this.min = Math.min(min, x);
}
/**
* @return max
*/
public int getLargest(){
return max;
}
/**
* @return min
*/
public int getSmallest(){
return min;
}
}
############ NumberListTester.java ####################
public class NumberListTester {
public static void main(String[] args) {
// creating NumberList object
NumberList list = new NumberList();
list.addValue(5);
list.addValue(-1);
System.out.println("Largest: "+list.getLargest());
System.out.println("Smallest: "+list.getSmallest());
list.addValue(4);
list.addValue(-8);
System.out.println("Largest: "+list.getLargest());
System.out.println("Smallest: "+list.getSmallest());
}
}
/*
Sample Run:
Largest: 5
Smallest: -1
Largest: 5
Smallest: -8
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.