Write a program NumberList that computes the largest and sma

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)

Solution

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

*/

 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 get
 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 get
 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 get

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site