Lab 13a – Exam 3

Part I

Write a class named MenuItem that models a restaurant menu item. Include the following:

    • Fields for the menu item’s name (String), number of calories (int), and price (double).
    • A constructor that sets all 3 fields using data passed into the constructor.
        • Do not include data validation in the constructor.
    • Getters and setters for each of the fields.
        • Do not include data validation in the setters.
    • Override the toString() method and return a string containing the name, number of calories, and price, all separated by commas.
    • Override the equals() method. Two menu items are considered equal if they have the same name.
Part II

Create a file named menu.txt that contains the data shown below.  The first line of the file contains an integer that indicates how many menu items are described in the file. Each subsequent line contains the name, number of calories, and price of a menu item – separated by commas.

3
Cheese Cake,560,6.95
16 oz. Prime Rib,900,21.95
Lobster Bisque,600,5.75
Part III

Write a class named Exam3 that satisfies the following.

    • The class has a method named printMenu that takes an array of MenuItem elements as an argument and prints to the screen the name, number of calories, and price of each item in the array.
    • The class has a main method that when run:
      • Reads the data in menu.txt, and for each menu item in the file creates an instance of the MenuItem class and stores the reference to the instance in an array named menuItems.
      • Prints the menu items stored in the array named menuItems to the screen by calling printMenu.
      • Iterates, using a for-loop, over the array named menuItems and prints to the screen the name, a dash (-), and the price of each menu items in the array that has fewer than 700 calories, with each items on a separate line.

Compile, Test, and when correct, push to GitHub.

© 2024, Eric. All rights reserved.