tjb1

Untitled

Nov 28th, 2012
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. public class Chap4prob7_fatGRAM {
  2.  
  3. //data members
  4. // are the basic characteristics of the object
  5. private double calories;
  6. private double fatGrams;
  7.  
  8.  
  9.  
  10. //method members
  11. //are the functionalities of the object
  12.  
  13. //constructors These methods create (instantiate) an instance of the object
  14.  
  15. /**
  16. * Default constructor sets calories and fatGrams to 0
  17. */
  18. public Chap4prob7_fatGRAM(){
  19. double calories = 0.0;
  20. double fatGrams = 0.0;
  21.  
  22.  
  23. }
  24.  
  25. /**
  26. * full constructor sets the calories and fatGrams according to the parameters
  27. * @param calories is the calories
  28. * @param fatGrams is the fatGrams
  29. *
  30. */
  31. public Chap4prob7_fatGRAM(double calories, double fatGrams){
  32. this.calories = calories;
  33. this.fatGrams = fatGrams;
  34.  
  35. }
  36.  
  37. //getters
  38.  
  39. /**
  40. * this method returns calories
  41. * @return calories
  42. */
  43. public double getcalories(){
  44. return calories;
  45. }
  46. /**
  47. * this method returns fatGrams
  48. * @return fatGrams
  49. */
  50. public double getfatGrams(){
  51. return fatGrams;
  52. }
  53.  
  54.  
  55. //setters
  56.  
  57. /**
  58. * This method resets calories according to the parameter
  59. * @param value will be set as the new value
  60. */
  61. public void setcalories( double value ) {
  62. this.calories = value;
  63. }
  64. /**
  65. * This method resets fatGrams according to the parameter
  66. * @param value will be set as the new value
  67. */
  68. public void setfatGrams( double value ) {
  69. this.fatGrams = value;
  70. }
  71.  
  72.  
  73. //toString method
  74.  
  75. public String toString(){
  76. String str;
  77.  
  78. str = ("You entered " + calories + " calories and " + fatGrams + " fat grams for your food item.");
  79. return str;
  80. }
  81.  
  82. //all other object specific methods
  83. double fatCalories = 0.0;
  84.  
  85.  
  86. public double fatCalories() {
  87. fatCalories = fatGrams*9.0;
  88. return fatCalories;
  89. }
  90.  
  91. public String percentCal() {
  92. double percent = 0;
  93. String percentCal = "";
  94. percent = fatCalories/calories;
  95. if(percent < .3) {
  96. percentCal = ("Calories from fat are " + fatCalories + " and this is a low fat food.");
  97. }
  98. else {
  99. percentCal = ("Calories from fat are " + fatCalories + ".");
  100. }
  101.  
  102. return percentCal;
  103.  
  104. }
  105.  
  106.  
  107. }//end class
Advertisement
Add Comment
Please, Sign In to add comment