Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Chap4prob7_fatGRAM {
- //data members
- // are the basic characteristics of the object
- private double calories;
- private double fatGrams;
- //method members
- //are the functionalities of the object
- //constructors These methods create (instantiate) an instance of the object
- /**
- * Default constructor sets calories and fatGrams to 0
- */
- public Chap4prob7_fatGRAM(){
- double calories = 0.0;
- double fatGrams = 0.0;
- }
- /**
- * full constructor sets the calories and fatGrams according to the parameters
- * @param calories is the calories
- * @param fatGrams is the fatGrams
- *
- */
- public Chap4prob7_fatGRAM(double calories, double fatGrams){
- this.calories = calories;
- this.fatGrams = fatGrams;
- }
- //getters
- /**
- * this method returns calories
- * @return calories
- */
- public double getcalories(){
- return calories;
- }
- /**
- * this method returns fatGrams
- * @return fatGrams
- */
- public double getfatGrams(){
- return fatGrams;
- }
- //setters
- /**
- * This method resets calories according to the parameter
- * @param value will be set as the new value
- */
- public void setcalories( double value ) {
- this.calories = value;
- }
- /**
- * This method resets fatGrams according to the parameter
- * @param value will be set as the new value
- */
- public void setfatGrams( double value ) {
- this.fatGrams = value;
- }
- //toString method
- public String toString(){
- String str;
- str = ("You entered " + calories + " calories and " + fatGrams + " fat grams for your food item.");
- return str;
- }
- //all other object specific methods
- double fatCalories = 0.0;
- public double fatCalories() {
- fatCalories = fatGrams*9.0;
- return fatCalories;
- }
- public String percentCal() {
- double percent = 0;
- String percentCal = "";
- percent = fatCalories/calories;
- if(percent < .3) {
- percentCal = ("Calories from fat are " + fatCalories + " and this is a low fat food.");
- }
- else {
- percentCal = ("Calories from fat are " + fatCalories + ".");
- }
- return percentCal;
- }
- }//end class
Advertisement
Add Comment
Please, Sign In to add comment