the second Objects lab.

Heater.java 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Write a description of class Heater here.
  3. *
  4. * @author (your name)
  5. * @version (a version number or a date)
  6. */
  7. public class Heater
  8. {
  9. // instance variables - replace the example below with your own
  10. private int temperature;
  11. private int min;
  12. private int max;
  13. private int increment;
  14. /**
  15. * Constructor for objects of class Heater
  16. */
  17. public Heater(int minTemp, int maxTemp)
  18. {
  19. // initialise instance variables
  20. temperature = 15;
  21. min = minTemp;
  22. max = maxTemp;
  23. increment = 5;
  24. }
  25. /**
  26. * increase temperature by increment
  27. */
  28. public void warmer()
  29. {
  30. // put your code here
  31. if(temperature + increment <= max){
  32. temperature += increment;
  33. } else {
  34. System.out.print("cannot set temperature above max");
  35. }
  36. }
  37. /**
  38. * decrease temperature by increment
  39. */
  40. public void cooler()
  41. {
  42. // put your code here
  43. if(temperature - increment >= min){
  44. temperature -= increment;
  45. } else {
  46. System.out.print("cannot set temperature below min");
  47. }
  48. }
  49. /**
  50. * return temperature
  51. */
  52. public int getTemperature()
  53. {
  54. // put your code here
  55. return temperature;
  56. }
  57. /**
  58. * increase temperature by increment
  59. */
  60. public void setIncrement(int newIncrement)
  61. {
  62. // put your code here
  63. increment = newIncrement;
  64. }
  65. }