Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please use the example at the bottom as reference. I need to be as descriptive as possible . For each variable listed below, identify its

Please use the example at the bottom as reference. I need to be as descriptive as possible. For each variable listed below, identify its scope (static, instance, local, or block) and describe the scope using complete sentences. An example description is provided, after the code listing, for the ounces variable in the Beverage2 class.

Please describe the scope for the following variables:

  1. The num variable in the Beverage2 class
  2. The price variable in the Beverage2 class
  3. The shots variable in the Espresso class
  4. The b variable in the TestBeverage2 class
  5. The j variable in the TestBeverage2 class

public abstract class Beverage2 {

private static int num;

private int ounces;

private double price;

protected Beverage2(int ounces, double price) {

this.ounces = ounces;

this.price = price;

num++;

}

public int getOunces() { return ounces; }

public double getPrice() { return price; }

public static int getNum() { return num; }

public abstract String drink();

}

public class Espresso extends Beverage2 {

private int shots;

protected Espresso (int ounces, double price, int shots) {

super(ounces, price);

this.shots = shots;

}

public int getShots() { return shots; }

public String drink() {

return "The espresso is strong.";

}

}

public class TestBeverage2 {

public static void main(String[] args) {

Espresso b;

for (int j = 0; j < 3; j++) {

b = new Espresso(2, 2.49, j + 1);

System.out.print("Drink #" + Beverage2.getNum() + ": ");

System.out.println("Espresso with " + b.getShots() + " shot(s)");

}

System.out.println(" You ordered " + Beverage2.getNum() + " drinks.");

}

}

Example description of scope for the ounces variable in the Beverage2 class: The ounces variable in the Beverage2 class is an instance variable of Beverage2, and therefore has instance variable scope. Whenever a Beverage2 object is instantiated, it has an ounces variable that lives as long as that Beverage2 object exists.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David J. Auer David M. Kroenke

13th Edition

B01366W6DS, 978-0133058352

More Books

Students also viewed these Databases questions

Question

1. Define science and explain four of its major goals.

Answered: 1 week ago

Question

13. You always should try to make a good first impression.

Answered: 1 week ago