QuestionQuestion

Transcribed TextTranscribed Text

Introduction to Computer Programming Programming Assignment In this assignment you’ll be creating a single class called MealPrep . The idea is to discover what kind of meal you can make with the ingredients you have on hand. Its main() method will ask a series of questions about what ingredients you have available and will then suggest a possible meal. Practice writing Boolean expressions and conditions Practice selection statements by writing multiple forms of if statements or switch statements Reinforce your ability to read and interpret data from the user Explore basic concepts of machine learning (i.e., decision trees) There is a supplemental document for this assignment that contains a diagram of a decision tree. Examining it, you can see that it starts off broadly by asking the user if they have meat on hand and then, if they have meat, if they have beef, chicken, or other meat available. From there, based on answers to questions, the tree narrows the possibilities with each step until it comes to a conclusion at the end. Decision trees are one of the basic building blocks of machine learning algorithms used in modern computer systems. In this assignment, the decision tree is specified ahead of time, but in machine learning the objective is for the computer to construct such a tree by itself based on the data it has observed so far. It does this either with or without the aid of a human (i.e., supervised or unsupervised learning). Please note that some branches have three options, while others are yes/no questions. If there are only two branches, the question should be asked as "Do you have XYZ?" with a "yes" response being the upward branch and a "no" response being the downward branch. If there are three branches available, the question should be of the format "Do you have A, B, or C?" Program 4 Overview Goals Decision Trees Your program should implement the decision tree as seen in the supplemental document. You will need to write a large series of nested if statements that complete the program. Your program should: 1. Prompt the user with a message that you will be assisting them in making their dinner decision. 2. Ask the user questions to follow the logic of the decision tree. You’ll need to use many nested if-else/ and if-else-if statements or switch statements to do this. 3. Answers to yes/no questions should allow case-insensitive responses (i.e., YES , yEs , and yeS are identical for response purposes) and and also allow single-character responses such as y or Y for yes or n or N for no. 4. Once your program comes to a conclusion (i.e., reaches the end of a branch in the tree) about the user’s item, it should provide a suggestion to the user in the form of You should make MNO , where MNO is the name of the dish as specified in the decision tree document. The program should then end. See the example runs at the end of this document. SPECIAL NOTE: This program will get somewhat long and the nested selection statements will make it challenging to keep track of what is going on as you write the code. Use proper indentation and add good comments about the purpose of each block to help you keep your place. Name your class MealPrep and source file MealPrep.java . All questions should be printed on a line by themselves. All String values read in should use nextLine() to prevent potential problems. If the user enters an invalid input (anywhere) the program should print an error message with the word "invalid" and end. There should only be a single main() method in your program. Writing additional methods may work with local testing in jGRASP, but will potentially break Web-CAT testing. The main() method for this assignment will be large (potentially over 300 lines of code), but most of it is repetitious and can be copy/paste/modified once you have one branch of your program working. What Your Program Will Do Class and File Naming Points to Think About Performing your own testing will require that you make sure all paths through the tree are handled correctly. Printing off the tree and manually checking off each one as you test it in your program may prove useful. Web-CAT will attempt to traverse all paths of the decision tree. Additionally, most paths will be tested with bad input to see if your program handles these correctly. For a detailed program like this, it might be useful to pick one branch of the tree, such as to get to steak, baked potato and salad or frittata and write just enough code to get that far in your decision tree implementation. Once you know that that path works, you can begin to cover the rest of the decision tree. The liberal use of comments throughout this program will aid greatly in keeping track of what's going on. Testing Your Program Developing Your Program Welcome to the Meal Prep Program. Let's see what you can make. Do you have any meat? yes Do you have beef, chicken, or other meat? beef Do you have steak, ground, or roast? ground Do you have buns? no Do you have rice? yes You should make a casserole Sample Runs This example shows answering yes to meat , which is the first question, the three options for beef , chicken , or other meat (the second question if yes to meat is answered), and steak , ground , or roast if beef is answered. Welcome to the Meal Prep Program. Let's see what you can make. Do you have any meat? yes Do you have beef, chicken, or other meat? chicken Do you have whole or pieces? pieces Do you have vegetables? no Do you have pasta? yes You should make chicken piccata Welcome to the Meal Prep Program. Let's see what you can make. Do you have any meat? no Do you have any vegetables? yes Do you have any eggplant? yes Do you have any tomato? yes You should make eggplant parmesan This example shows entering yes , chicken , pieces , no , and yes as input. This example shows entering no , yes , yes , and yes as input to reach eggplant parmesan . Notice that the invalid response can come at any point in the questioning process. Welcome to the Meal Prep Program. Let's see what you can make. Do you have any meat? asdf Invalid response. Notice that the invalid response can come at any point in the questioning process. Welcome to the Meal Prep Program. Let's see what you can make. Do you have any meat? yes Do you have beef, chicken, or other meat? moose Invalid response. Notice that the invalid response can come at any point in the questioning process. Welcome to the Meal Prep Program. Let's see what you can make. Do you have any meat? yes Do you have beef, chicken, or other meat? chicken Do you have whole or pieces? whole Do you have pasta? no Do you have rice? kllkjhasdfkjh; Invalid response.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.Scanner;

/**
* This program implements the given
* decision tree to arrive on the meal
* decision.
*/
public class MealPrep {

    public static void main(String[] args) {
       // Single scanner object
       Scanner scanner = new Scanner(System.in);
       // Printing welcome message.
       System.out.println("Welcome to the Meal Prep Program. Let's see what you can make.");

       // Asking first question.
       System.out.println("Do you have any meat?");
       String response = scanner.nextLine();

       // Checking if response is "yes", if user has meat.
       if (response.equalsIgnoreCase("yes")
                || response.equalsIgnoreCase("y")) {

            System.out.println("Do you have beef, chicken, or other meat?");
            response = scanner.nextLine();
            response = response.toLowerCase();

            // Checking cases if user have beef, chicken, or other meat.
            switch (response) {
                case "beef":

                   System.out.println("Do you have steak, ground, or roast?");
                   response = scanner.nextLine();
                   response = response.toLowerCase();

                   switch (response) {
                        case "steak":

                            System.out.println("Do you have any Potatoes?");
                            response = scanner.nextLine();
                            // Checking if response is "yes", if user has Potatoes.
                            if (response.equalsIgnoreCase("yes")
                                    || response.equalsIgnoreCase("y")) {

                               System.out.println("Do you have any Salad?");
                               response = scanner.nextLine();
                               // Checking if response is "yes", if user has Salad.
                               if (response.equalsIgnoreCase("yes")
                                        || response.equalsIgnoreCase("y")) {
                                    System.out.println("You should make Steak, Baked Potato and Salad");
                               } else if (response.equalsIgnoreCase("no")
                                        || response.equalsIgnoreCase("n")) {

                                    System.out.println("Do you have any Fruit?");
                                    response = scanner.nextLine();
                                    // Checking if response is "yes", if user has Fruit.
                                    if (response.equalsIgnoreCase("yes")
                                           || response.equalsIgnoreCase("y")) {
                                        System.out.println("You should make Steak, Baked Potato and Fruit");
                                    } else if (response.equalsIgnoreCase("no")
                                           || response.equalsIgnoreCase("n")) {
                                        System.out.println("You should make Steak and Potato Hash");
                                    } else {
                                        // If user's response is invalid.
                                        System.out.println("Invalid response.");
                                    }

                               } else {
                                    // If user's response is invalid.
                                    System.out.println("Invalid response.");
                               }

                            } else if (response.equalsIgnoreCase("no")
                                    || response.equalsIgnoreCase("n")) {
                               System.out.println("Do you have any Salad?");
                               response = scanner.nextLine();
                               // Checking if response is "yes", if user has Salad.
                               if (response.equalsIgnoreCase("yes")
                                        || response.equalsIgnoreCase("y")) {
                                    System.out.println("You should make Steak Salad");
                               } else if (response.equalsIgnoreCase("no")
                                        || response.equalsIgnoreCase("n")) {
                                    System.out.println("You should make Steak Sandwich");
                               } else {
                                    // If user's response is invalid.
                                    System.out.println("Invalid response.");
                               }
                            } else {
                               // If user's response is invalid.
                               System.out.println("Invalid response.");
                            }


                            break;
                        case "ground":

                            System.out.println("Do you have any Buns?");
                            response = scanner.nextLine();
                            // Checking if response is "yes", if user has Buns.
                            if (response.equalsIgnoreCase("yes")
                                    || response.equalsIgnoreCase("y")) {
                               System.out.println("You should make Hamburgers");
                            } else if (response.equalsIgnoreCase("no")
                                    || response.equalsIgnoreCase("n")) {

                               System.out.println("Do you have any Rice?");
                               response = scanner.nextLine();
                               // Checking if response is "yes", if user has Rice.
                               if (response.equalsIgnoreCase("yes")
                                        || response.equalsIgnoreCase("y")) {
                                    System.out.println("You should make Casserole");
                               } else if (response.equalsIgnoreCase("no")
                                        || response.equalsIgnoreCase("n")) {
                                    System.out.println("You should make Chili");
                               } else {
                                    // If user's response is invalid.
                                    System.out.println("Invalid response.");
                               }
                            } else {
                               // If user's response is invalid.
                               System.out.println("Invalid response.");
                            }...

By purchasing this solution you'll be able to access the following files:
MealPrep.java.

$25.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats