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

Write a program NumberList that computes the largest and smallest values in a se

ID: 3924273 • 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 solution for Question 1:

############ NumberList.java ##################

public class NumberList {

  

// instant variables of class numberList

private int max;

private int min;

  

// constructor

   public NumberList(){

       min = Integer.MAX_VALUE; // initializing min with maximum value

       max = Integer.MIN_VALUE;// initializing max with minimum value

   }

  

  

/**

   * @param x

   */

   public void addValue(int x){

      

       // evaluating max with x

       max = Math.max(max, x);

      

       // evaluating min

       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

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote