Question

Test 3 Your Name Spring 2018 // Demonstrates indirect access to isherited private members 1 publie elass FoodAnalyser( //Instantiates a Pizza objeet and printa ita caloriee per II perving 2 publie statie vold main (String11 args) 3 PiEza special-new PizEa (300) 4 System.out.print in (Cajories per eervings special.calorieaPerServing)) /I Representa an item of food. Dsed aa the parent of a subelass /I to demonstrate indirect referencing 5 publie elass FoodItee t 6 tinal private int CALORIES FER GRAN9 7 private int fatGrans Sets up this tood item with the specified nusber of fat grame /I and nunber of servinga. ings-nServinga I- / Computes and returns the number of calories in thia food item /due to fat 12 private int calories) 13 return fatGrana CALORIES PER GRAM 14 15 // Computes and zeturna the nunber of fat calories per serving publie int caloriesperServing 0 return (calories)servinga) // Represents a pizza, which in a food item. Used to demonatrate / indirect referencing through inheritance 16 publie elasa Pise extenda FoodItem ( / seta up a pizza with the specitied anount of fat (aseumes // eight servings) 17 publie Pizza cint fatGrana) super (Eatarans, 10)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

FoodAnalyzer.java


public class FoodAnalyzer {

   public static void main(String[] args) {
       Pizza special = new Pizza(300);
       System.out.println("Caloires per serving: " + special.caloriesPerServing());
   }

}


Pizza.java


public class Pizza extends FoodItem {

   public Pizza (int fatGrams)
   {
       //FoodItem(int numFatGrams, int numServings)
       super(fatGrams, 10);
   }
}

FoodItem.java

public class FoodItem {

   final private int CALORIES_PER_GRAM = 9;
   private int fatGrams;
   protected int servings;
  
   public FoodItem(int numFatGrams, int numServings)
   {
       fatGrams = numFatGrams;
       servings = numServings;
   }
   private int calories()
   {
       return fatGrams * CALORIES_PER_GRAM ;
   }
  
   public int caloriesPerServing()
   {
       return (calories()/servings);
   }
}

Problems @ Javadoce. Declaration Console X!-Coverage <terminated> FoodAnalyzer [Java Application] C:Program FilesJava\jdk1.8.

Add a comment
Know the answer?
Add Answer to:
Test 3 Your Name Spring 2018 // Demonstrates indirect access to isherited private members 1 publie...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT