Guest User

Untitled

a guest
Feb 18th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. /**
  2. Computes information about a set of data values.
  3. */
  4. public class DataSet
  5. {
  6. private double sum;
  7. private double maximum;
  8. private int count;
  9.  
  10. /**
  11. Constructs an empty data set.
  12. */
  13. public DataSet()
  14. {
  15. sum = 0;
  16. count = 0;
  17. maximum = 0;
  18. }
  19.  
  20. /**
  21. Adds a data value to the data set
  22. @param x a data value
  23. */
  24. public void add(double x)
  25. {
  26. sum = sum + x;
  27. if (count == 0 || maximum < x) maximum = x;
  28. count++;
  29. }
  30.  
  31. /**
  32. Gets the average of the added data.
  33. @return the average or 0 if no data has been added
  34. */
  35. public double getAverage()
  36. {
  37. if (count == 0) return 0;
  38. else return sum / count;
  39. }
  40.  
  41. /**
  42. Gets the largest of the added data.
  43. @return the maximum or 0 if no data has been added
  44. */
  45. public double getMaximum()
  46. {
  47. return maximum;
  48. }
  49. }
Add Comment
Please, Sign In to add comment