16.26 (Cooking with Healthier Ingredients) Obesity in America is increasing at a
ID: 3530938 • Letter: 1
Question
16.26 (Cooking with Healthier Ingredients) Obesity in America is increasing at an alarming rate. Check the map from the Centers for Disease Control and Prevention (CDC) at www.cdc.gov/ nccdphp/dnpa/Obesity/trend/maps/index.htm, which shows obesity trends in the United States over the last 20 years. As obesity increases, so do occurrences of related problems (e.g., heart disease, high blood pressure, high cholesterol, type 2 diabetes). Write a program that helps users choose healthier ingredients when cooking, and helps those allergic to certain foods (e.g., nuts, gluten) find substitutes. The program should read a recipe from a JTextArea and suggest healthier replacements for some of the ingredients. For simplicity, your program should assume the recipe has no abbreviations for measures such as teaspoons, cups, and tablespoons, and uses numerical digits for quantities (e.g., 1 egg, 2 cups) rather than spelling them out (one egg, two cups). Some common substitutions are shown in Fig. 16.27. Your program should display a warning such as,Explanation / Answer
import java.awt.Color; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.JCheckBox;public class Healthier extends JFrame { /** * All constants and instance variables */ private static final int MWA_WIDTH = 450; private static final int MWA_HEIGHT = 675; private static final Color BG_COLOR = Color.PINK;
private JLabel copyLbl; private JTextArea oldRecipeTA; private JTextArea newRecipeTA; private JCheckBox heartHealthCB; private JCheckBox loseWeightCB; private JCheckBox sugarCB; private Ingredients ing;
/** * Healthier constructor -- the primary "work horse" of this application */ public Healthier() { setTitle("Healthier Ingredient Substitutes"); setupGUI(); } // end Healthier() constructor
/** * setupGUI lays out the GUI */ private void setupGUI() { /** * Set background color and other JFrame attributes */ setLayout(null); getContentPane().setBackground(BG_COLOR); setSize(MWA_WIDTH, MWA_HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setResizable(false); ing = new Ingredients(); String oldRecipe = "1 egg 2 cup milk 3 cup sugar"; String newRecipe = ing.action(oldRecipe);
// Display both old and new strings System.out.println("oldRecipe: <" + oldRecipe + ">"); System.out.println("newRecipe: <" + newRecipe + ">"); System.out.println();
/** * Set the title of the application (in the GUI) */ JLabel titleLbl = new JLabel("Healthier Ingredients - Simple ingredient substitutions"); titleLbl.setBounds(40, 25, 300, 20); add(titleLbl);
/** * Insert a JTextField, its JLabel, and the copy JLabel */ JLabel nameLbl = new JLabel("Type your ingredients here:"); nameLbl.setBounds(40, 70, 300, 20); add(nameLbl);
oldRecipeTA = new JTextArea(50, 40); oldRecipeTA.setBounds(40, 115, 300, 60); add(oldRecipeTA);
newRecipeTA = new JTextArea(50, 40); newRecipeTA.setBounds(40, 280, 300, 60); add(newRecipeTA);
copyLbl = new JLabel("These are your ingredient substitutions:"); copyLbl.setBounds(40, 245, 300, 20); add(copyLbl);
copyLbl = new JLabel("Please consult your Physician before making changes to your diet."); copyLbl.setBounds(30, 600, 300, 20); add(copyLbl);
bloodPressureCB = new JCheckBox("High Blood Pressure?"); bloodPressureCB.setBounds(70, 500, 200, 20); add(bloodPressureCB); bloodPressureCB.setBackground(BG_COLOR);
diabetesCB = new JCheckBox("Diabetic?"); diabetesCB.setBounds(70, 530, 200, 20); add(diabetesCB); diabetesCB.setBackground(BG_COLOR);
allergiesCB = new JCheckBox("Allergies?"); allergiesCB.setBounds(70, 560, 200, 20); add(allergiesCB); allergiesCB.setBackground(BG_COLOR);
/** * Create and insert the Copy and Quit buttons * Hook up their handlers */ JButton newBtn = new JButton("Click for ingredient substitutions!"); newBtn.setBounds(75, 190, 190, 22); newBtn.addActionListener(new Healthy.NewListener()); add(newBtn);
JButton exitBtn = new JButton("Quit"); exitBtn.setBounds(235, 390, 65, 22); exitBtn.addActionListener(new Healthier.QuitListener()); add(exitBtn);
copyLbl = new JLabel("Please enter your ingredients like this...."); copyLbl.setBounds(30, 425, 300, 20); add(copyLbl); JButton clearBtn = new JButton("Clear"); clearBtn.setBounds(155, 390, 65, 22); clearBtn.addActionListener(new Healthier.ClearListener()); add(clearBtn); } // end setupGUI()
/** * The application begins execution here; make the application "thread-safe" */ public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new Healthier().setVisible(true); new Healthy(); } }); } // end main()
/** * All inner classes are placed below this point */
/** * Listener/handler for the Copy button */ private class NewListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { String data = oldRecipeTA.getText().trim(); System.out.println("<" + data + ">"); System.out.println("State of High Blood Pressure: " + bloodPressureCB.isSelected()); System.out.println("State of Diabetes: " + diabetesCB.isSelected()); System.out.println("State of Allergies: " + allergiesCB.isSelected());
String newData = ing.action(data); // Process the old recipe data newRecipeTA.setText(newData); // Load the new recipe into the JTextArea
oldRecipeTA.requestFocus(); } } // end class CopyListener
/** * Listener/handler for the Quit button */ private class QuitListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } } // end class QuitListener
private class ClearListener implements ActionListener {
@Override public void actionPerformed(ActionEvent e) { oldRecipeTA.setText(" "); newRecipeTA.setText(" "); } } // end class ClearListener
class Ingredients {
// Parallel arrays for the table of food substitutions private int[] quantity = {1, 1, 1, 2}; private String[] measure = {"cup", "cup", "teaspoon", "cup"}; private String[] food = {"sour_cream", "milk", "lemon_juice", "sugar"}; private boolean[] loseWeight = {true, false, false, true}; private boolean[] sugar = {true, true, false, true}; private boolean[] heartHealth = {true, false, true, true}; Ingredients() { System.out.println("Ingredients Constructor"); } public String action(String oldRecipe) { System.out.println(" Original data (recipe) " + oldRecipe + " *****"); String newData; String newRecipe = oldRecipe; // Need to load it up with something good String aFood = "cider"; // This food would come from oldRecipe
int index = -1; // -1 indicates aFood was NOT found in the food array for (int i = 0; i < food.length; i++) { if (aFood.equals(food[i])) { // This is how you do string compare index = i; break; } } int index = -1; for (int i = 0; i < quantity.length; i++){ if (aQuantity.equals(quantity[i])){ index = i; break; } } int index = -1; for (int i = 0; i < measure.length; i++) { if (aMeasure.equals(measure[i])) { index = i; break; } } // index >= 0 means that index contins the row of the table that // contains the substituion information. I'll not do any actual // substitution -- that work is left for you to do if (index >= 0) { newRecipe = aFood + " should be substituted in: " + oldRecipe; }
// Return newly updated recipe to the caller return newRecipe; } // end convert() class SubString{
} // newData = "Processed: " + data + " ******"; // return newData; }
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.