I need help completing the classes below, I don't think the code I have so far is correct because I keep getting errors. Please help me complete and understand the classes.

1. Create an abstract class named CoffeeOption. This class should have the
following components:
a. A protected double instance variable named cost.
b. A protected String instance variable named description.
c. A public method named price. This method does not take any arguments, and
it returns a double value.
d. A public method named toString. This method does not take any arguments.
It returns the String “add”, followed by name, followed by several spaces,
followed by cost.
2. Create a subclass of CoffeeOption named Sugar, with the following method:
a. A no-argument constructor that sets name to “sugar” and cost to 0.05.
3. Create a subclass of CoffeeOption named Cream, with the following method:
a. A no-argument constructor that sets name to “cream” and cost to 0.10.
4. Create a subclass of CoffeeOption named Flavoring, with the following
variables and methods:
a. A private ArrayList that can hold String objects, called flavors.
b. A private String named selectedFlavor.
c. A private method named loadFlavors. This method does not take any
arguments, and it does not return any value. The loadFlavors() method
attempts to open a file on disk named “flavors.txt”. It reads in the complete file line
by line, storing each line in the flavors ArrayList. You may assume that this file
always exists, and always contains at least one line (HINT: use Scannerʼs
hasNextLine() method in your loop condition).
d. A no-argument constructor. The constructor should perform the following steps, in
order:
i. Set name to “flavor shot” and cost to 0.25.
ii. Call loadFlavors() to fill the ArrayList.
iii.Create a Scanner to read from the keyboard.
iv.Use a loop to display the contents of flavors as a numbered menu, as in:
Please select a flavor:
1) blueberry
2) raspberry
3) french vanilla
4) cinnamon
5) mocha
v. Prompt the user to enter a number.
vi.Read an integer from the user (you may assume that the user always chooses
a valid menu option).
vii.Set selectedFlavor to the selected element of the ArrayList.
e. An overridden version of toString(). This version of toString() should call
super.toString(), but it should include the value of selectedFlavor on the
following line, as in:
add flavor shot 0.25
blueberry

what i have is below:

public abstract class CoffeeOption
{

protected double cost;
protected String name;


public double price()
{

}

public String toString ()
{
return(add + name + "      " + cost);
}

public class Sugar extends CoffeeOption
{
   name = "cream";
cost= 0.10;
}

public class Cream extends CoffeeOption
{
 name = "cream";
cost= 0.10;
}

public class Flavoring extends CoffeeOption
{
private ArrayList<String> flavors= new ArrayList<String>();
private String selectedFlavor;
}


private void loadFlavors()
{
  BufferedReader in = null;
  flavors = new ArrayList<String>();

    try {
         in = new BufferedReader(new FileReader("flavors.txt"));
         String flavor;

         while((flavor = in.readLine()) != null) {
            flavors.add(flavor);
                  }
                  
                      } 
         catch(IOException ex) {
                         ex.printStackTrace();
                                }
         finally {
                       try {
                               in.close();
                               in = null;

                            } 
                             catch (IOException ex) {}
                             
}
}

Recommended Answers

All 3 Replies

I keep getting errors

Please copy and paste here the full text of the messages

i get an "<identifier> expected" error for lines 20-21, 26-27.

Please copy and paste the FULL text of the error messages. When I compile, I get the more info than your post shows:

TestSorts.java:138: cannot find symbol
symbol  : variable var
location: class TestSorts
         var = 2;
         ^

The statements at the lines you reference are outside of a method at the class level. They probably need to be in a constructor.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.