Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA PROBLEM Create a small class hierarchy representing different beverages, as follows. That is, there should be a base class called Beverage; classes HotBeverage and

JAVA PROBLEM

Create a small class hierarchy representing different beverages, as follows.

That is, there should be

  • a base class called Beverage;
  • classes HotBeverage and ColdBeverage, both subclasses of Beverage;
  • Tea and Coffee, both subclasses of HotBeverage.
  • IceTea and Beer, both subclasses of ColdBeverage.
  • a placeholder class NoBeverage, a subclass of Beverage.

Beverages (and thus all derived classes) should have a protected integer attribute price and an appropriate getter getPrice(). Every leaf class in this hierarchy should override the method toString() to return a two-character label (a String). Use the following labels and prices.

Class Label Price

NoBeverage -- 0

Tea HT 2

Coffee HC 2

IceTea CT 1

Beer CB 3

For example, if t is a Tea, and n is a NoBeverage, then t.toString() returns "HT", n.toString() returns "--", t.getPrice() returns 22 and n.getPrice() returns 00.

Hint: Notice that the prices of different instances of each class are the same. You could therefore set the price in a Constructor method that takes no parameters.

  1. Add a class called VendingMachine. This class should have

    • a private integer attribute cassette, to keep track of how much money has been inserted.

    • an attribute representing a rack of beverages. This should contain four rows (one for Tea,Coffee,IceTea and Beer, respectively, in that order) and 5 items per row.

      Hint: use a 2-dimensional array of Beverage (a variable of type Beverage[][]) and initialize it in a constructor method..

    • a method insertCoin, that takes an integer and adds it to the internal cassette.

    • a method getBeverage. This should take two integers, row and column, and return the respective Beveragefrom your internal rack. Of course, this should only succeed if the cassette contains enough money, namely at least than that beverage's price. If successful, you also need to update the cassette and rack of beverages (replace the sold item by a new placeholder NoBeverage). If unsuccessful (not enough money was inserted), just return a placeholder NoBeverage.

    • a method toString() that takes no arguments and returns a String representation of the machine, where

      • the first line contains the string "Cassette: $" followed by the current value of the attribute cassette;
      • each row in your rack occupies one line of text;
      • each column in the row represents one beverage, as the string "[r,c]:XX:$p", where r and c, are row and column number, XX the label and p the price of the item at position (r,c).

      For example, if set up correctly and before any other methods are called, this should return exactly

      Cassette: $0 [0,0]:HT:$2 [0,1]:HT:$2 [0,2]:HT:$2 [0,3]:HT:$2 [0,4]:HT:$2 [1,0]:HC:$2 [1,1]:HC:$2 [1,2]:HC:$2 [1,3]:HC:$2 [1,4]:HC:$2 [2,0]:CT:$1 [2,1]:CT:$1 [2,2]:CT:$1 [2,3]:CT:$1 [2,4]:CT:$1 [3,0]:CB:$3 [3,1]:CB:$3 [3,2]:CB:$3 [3,3]:CB:$3 [3,4]:CB:$3

      and after calling insertCoin(5) once, followed by calls to getBeverage(2,2) and getBeverage(0,1), we expect the toString() to return the following.

      Cassette: $2 [0,0]:HT:$2 [0,1]:--:$0 [0,2]:HT:$2 [0,3]:HT:$2 [0,4]:HT:$2 [1,0]:HC:$2 [1,1]:HC:$2 [1,2]:HC:$2 [1,3]:HC:$2 [1,4]:HC:$2 [2,0]:CT:$1 [2,1]:CT:$1 [2,2]:--:$0 [2,3]:CT:$1 [2,4]:CT:$1 [3,0]:CB:$3 [3,1]:CB:$3 [3,2]:CB:$3 [3,3]:CB:$3 [3,4]:CB:$3

Notice that the VendingMachine class need not be an application: you don't have to add a main method.

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

Step: 3

blur-text-image

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

More Books

Students also viewed these Databases questions

Question

1. Discuss the four components of language.

Answered: 1 week ago

Question

f. What stereotypes were reinforced in the commercials?

Answered: 1 week ago