Question

Need help extending and constructing this problem in JAVA using the given guidelines. public class gatorade...

Need help extending and constructing this problem in JAVA using the given guidelines.

public class gatorade {

public void nutrition() {
System.out.println("nutrition facts method");
System.out.println("90 Calories");
System.out.println("0 Grams Fat");
System.out.println("20 MG Sodium");
System.out.println("18 Grams Sugar");
System.out.println("0 Grams Protein");
}
}
//inheritance
public class blueberry extends gatorade{
System.out.println("Color is blue");

public class orange extends gatorade{
System.out.println("Color is orange");

public class lemonlime extends gatorade{
System.out.println("Color is yellow");

public class fruitpunch extends gatorade{
System.out.println("Color is red");

________________________________________________________

Write one application program by using the following requirements:

1) Exception handling

2) Inheritance

a) At least one superclass or abstract superclass: this class needs to have data members, accessor, mutator, and toString methods

b) At least one subclass: this class also needs to have data members, accessor, mutator, and toString methods

c) At least one interface: this interface needs to have at least two abstract methods

d) At least one method overloading

e) At least one method overriding

f) At least one static member and one static method

g) Polymorphism: you need to have at least one superclass reference to reference objects of a subclass

3. Within your application, please include comments at the places where you use the above items.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

interface Nutrition{//interface with two methods
   public void getNutrition();
   public void showColor();
}

class Gatorade implements Nutrition{
   protected int calories, fat, protein;
   protected static String color = "Orange";//static variable

   public Gatorade(int calories, int fat, int protein) {
       this.calories = calories;
       this.fat = fat;
       this.protein = protein;
   }
  
   //acessor mutator
   public int getCalories() {
       return calories;
   }

   public void setCalories(int calories) {
       this.calories = calories;
   }

   public int getFat() {
       return fat;
   }

   public void setFat(int fat) {
       this.fat = fat;
   }

   public int getProtein() {
       return protein;
   }

   public void setProtein(int protein) {
       this.protein = protein;
   }

   public static String getColor() {//static method
       return color;
   }

   public static void setColor(String color) {
       Gatorade.color = color;
   }
  
   @Override
   public String toString() {//toString method
       return "gatorade [calories=" + calories + ", fat=" + fat + ", protein=" + protein + "]";
   }

   @Override//implementing the interface
   public void getNutrition() {
       System.out.println("nutrition facts method");
       System.out.println(calories+" Calories");
       System.out.println(fat+" Grams Fat");
       System.out.println(protein+" Grams Protein");
   }

   @Override
   public void showColor() {
       System.out.println("Default Color is "+color);
   }

}

//inheritance
class Blueberry extends Gatorade implements Nutrition{
   private int sodium, sugar;

   public Blueberry(int calories, int fat, int protein, int sodium, int sugar) {
       super(calories, fat, protein);
       this.sodium = sodium;
       this.sugar = sugar;
   }
   //overloading method/constructor
   public Blueberry(int calories, int fat, int protein, int sodium, int sugar, String color) {
       super(calories, fat, protein);
       this.sodium = sodium;
       this.sugar = sugar;
       Blueberry.color = color;
   }

   public int getSodium() {
       return sodium;
   }

   public void setSodium(int sodium) {
       this.sodium = sodium;
   }

   public int getSugar() {
       return sugar;
   }

   public void setSugar(int sugar) {
       this.sugar = sugar;
   }
  
   @Override
   public void getNutrition() {
       System.out.println("nutrition facts method");
       System.out.println(calories+" Calories");
       System.out.println(fat+" Grams Fat");
       System.out.println(protein+" Grams Protein");
       System.out.println(sodium+" MG Sodium");
       System.out.println(sugar+" Grams Sugar");
   }
  
   @Override
   public void showColor() {
       System.out.println("The Color is "+color);
      
   }

   @Override
   public String toString() {
       return "Blueberry [sodium=" + sodium + ", sugar=" + sugar + ", calories=" + calories + ", fat=" + fat
               + ", protein=" + protein + "]";
   }
}

public class Test {// driver class
   public static void main(String[] args) {
       try {//exception handling
           Gatorade obj = new Gatorade(52, 12, 16);
           System.out.println(obj);
           System.out.println("\nBlackBerry");
           //polymorphism
           obj = new Blueberry(32, 2, 12, 14, 30, "Blue");
           System.out.println();
           obj.getNutrition();
           System.out.println();
           obj.showColor();
       }
       catch(NumberFormatException e) {
           e.printStackTrace();
       }

   }
}

Output

Add a comment
Know the answer?
Add Answer to:
Need help extending and constructing this problem in JAVA using the given guidelines. public class gatorade...
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
  • Can someone please help with this in JAVA? Write one application program by using the following...

    Can someone please help with this in JAVA? Write one application program by using the following requirements: 1) Exception handling 2) Inheritance a) At least one superclass or abstract superclass: this class needs to have data members, accessor, mutator, and toString methods b) At least one subclass: this class also needs to have data members, accessor, mutator, and toString methods c) At least one interface: this interface needs to have at least two abstract methods d) At least one method...

  • Write ONE application program by using the following requirements: Using JAVA File input and outp...

    Write ONE application program by using the following requirements: Using JAVA File input and output Exception handling Inheritance At least one superclass or abstract superclass: this class needs to have data members, accessor, mutator, and toString methods At least one subclass: this class also needs to have data members, accessor, mutator, and toString methods At least one interface: this interface needs to have at least two abstract methods At least one method overloading At least one method overriding At least...

  • In Java Which of the following statements declares Salaried as a subclass of payType? Public class...

    In Java Which of the following statements declares Salaried as a subclass of payType? Public class Salaried implements PayType Public class Salaried derivedFrom(payType) Public class PayType derives Salaried Public class Salaried extends PayType If a method in a subclass has the same signature as a method in the superclass, the subclass method overrides the superclass method. False True When a subclass overloads a superclass method………. Only the subclass method may be called with a subclass object Only the superclass method...

  • In JAVA Algorithm Workbench 1. Write the first line of the definition for a Poodle class....

    In JAVA Algorithm Workbench 1. Write the first line of the definition for a Poodle class. The class should extend the Dog class. 2. Look at the following code, which is the first line of a class definition: public class Tiger extends Felis In what order will the class constructors execute? 3. Write the statement that calls a superclass constructor and passes the arguments x, y, and z. 4. A superclass has the following method: public void setValue(int v) value...

  • What is wrong with the following Java Code. public class TestEdible { abstract class Animal {...

    What is wrong with the following Java Code. public class TestEdible { abstract class Animal { /** Return animal sound */ public abstract String sound(); } class Chicken extends Animal implements Edible { @Override public String howToEat() { return "Chicken: Fry it"; } @Override public String sound() { return "Chicken: cock-a-doodle-doo"; } } class Tiger extends Animal { @Override public String sound() { return "Tiger: RROOAARR"; } } abstract class Fruit implements Edible { // Data fields, constructors, and methods...

  • please this is Java and i need help Question 5 public class Food { public void...

    please this is Java and i need help Question 5 public class Food { public void Foodmethod_1 (int i) { public void Foodmethod_2 (int i) { } public static void Foodmethod_3(int i) { public class Bankudade extends Food { public static void Foodmethod_1 (int i) { public void Foodmethod_2 (int i) { public void Foodmethod_3 (int i) { > 7 a) Determine which method in the subclass overrides a method in the super class? (6 marks) EV b) Determine which...

  • What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that...

    What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that derives from a superclas:s ■ Demon "Declare a variable of the superclass type and assign it an instance of the subclass type strate polymorphic behavior Access the public members of the superclass type Notice how the overridden versions of the subclass type are called Notice how the subclass specific members are inaccessible "Create an array of superclass type and use a foreach loop to...

  • (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */...

    (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */ import java.awt.Graphics; public abstract class Animal { private int x; // x position private int y; // y position private String ID; // animal ID /** default constructor * Sets ID to empty String */ public Animal( ) { ID = ""; } /** Constructor * @param rID Animal ID * @param rX x position * @param rY y position */ public Animal( String...

  • PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is...

    PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is the output of the program as it is written? (Program begins on p. 2) 2. Why would a programmer choose to define a method in an abstract class (such as the Animal constructor method or the getName()method in the code example) vs. defining a method as abstract (such as the makeSound()method in the example)? /********************************************************************** *           Program:          PRG/421 Week 1 Analyze Assignment *           Purpose:         Analyze the coding for...

  • IN JAVA For the following questions, “define a class” means the following: • Create an appropriately-named...

    IN JAVA For the following questions, “define a class” means the following: • Create an appropriately-named file containing the Java class definition, including the following: – A block comment describing the file (and hence the class), as always – Any instance variables, as required by the question – Accessor (getter) and mutator (setter) methods for any instance variables – Constructors as appropriate or as required by the question – A toString method that prints instances of the class informatively –...

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