Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
11th Edition
ISBN: 9780134743356
Author: Paul J. Deitel, Harvey Deitel
Publisher: PEARSON
bartleby

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 4, Problem 18.1E

(Dangling-else Problem) The Java compiler always associates an else with the immediately preceding if unless told to do otherwise by the placement of braces ({ and }). This behavior can lead to what is referred to as the dangling-else problem. The indentation of the nested statement

  1. 1 if (x > 5)
  2. 2 if (y > 5)
  3. 3 System.out.println("x and y are > 5");
  4. 4 else
  5. 5 System.out.println("x is <= 5");

appears to indicate that if x is greater than 5, the nested if statement determines whether y is also greater than 5. If so, the statement outputs the string "x and y are > 5". Otherwise, it appears that if x is not greater than 5, the else part of the if ...else outputs the string "x is <= 5". Beware! This nested if ...else statement does not execute as it appears. The compiler actually interprets the statement as

  1. 1 if (x > 5)
  2. 2 if (y > 5)
  3. 3 System.out.println("x and y are > 5");
  4. 4 else
  5. 5 System.out.println("x is <= 5");

in which the body of the first if is a nested if ...else. The outer if statement tests whether x is greater than 5. If so, execution continues by testing whether y is also greater than 5. If the second condition is true, the proper string—"x and y are > 5"—is displayed. However, if the second condition is false, the string "x is <= 5" is displayed, even though we know that x is greater than 5. Equally bad, if the outer if statement’s condition is false, the inner if ...else is skipped and nothing is displayed. For this exercise, add braces to the preceding code snippet to force the nested if ...else statement to execute as it was originally intended.

Blurred answer
Students have asked these similar questions
===========Answer question below:=========== - Write program in Java. (Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses. For example, the expression ( 11 + 12 ) * 13 would be written as 11  12  +  13 * Assume that ALWAYS there is a space between operands and operators in the input expression. Use two stacks, one to store the operands and one to store the operators. Your program only accpets following operators :  ( )  +  -  /  *  Write a method to converts an infix expression into a postfix expression using the following method: String infixToPostfix(String expression) For example, the method should convert the infix expression ( 13 + 25 ) * 34 to 13  25  +  34 * and 20 * ( 10 + 30 ) to 20 10 30 + *. - solution.java: import java.util.*; import java.lang.*; import java.io.*;   class InfixToPostfix { public String infixToPostfix(String expression) { } } class DriverMain { public static void main(String args[]) { Scanner input…
(Emirp) An emirp (prime spelled backward) is a nonpalindromic prime number whose reversal is also a prime. For example, 17 is a prime and 71 is a prime, so 17 and 71 are both emirps. Write a Java program that displays the first 100 emirps. Display 10 numbers per line with a spacing of 5; all numbers should be left-aligned.
C++Jesse the Body-Building Burglar Jesse has broken into a sporting goods store to steal some training equipment. He can only carry out a certain amount of stuff before it gets too heavy. In addition, Jesse can only make one trip in and out of the store before the cops arrive, so he wants to be sure to get the most value out of the stuff he takes. Input for the program will be as below. The first line will contain how much Jesse can carry. This line is followed by a single item per line. Each of these lines will contain the name of the item, the number of items available, each individual item’s weight and its value. There will be up to 10 different items. Output the number of each item type he should take to maximize his value as well as the max value.   Example File: 100Barbell 1 25 10Plate 4 40 12Ball 5 30 15Rope 10 5 10Bench 1 50 20 Output:10 Rope, 1 Bench 120Attached file is the JBB dynamic programming

Chapter 4 Solutions

Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)

Ch. 4 - State whether each of the following is true or...Ch. 4 - State whether each of the following is true or...Ch. 4 - State whether each of the following is true or...Ch. 4 - Prob. 2.6SRECh. 4 - State whether each of the following is true or...Ch. 4 - Prob. 2.8SRECh. 4 - State whether each of the following is true or...Ch. 4 - State whether each of the following is true or...Ch. 4 - Write four different Java statements that each add...Ch. 4 - Write Java statements to accomplish each of the...Ch. 4 - Write Java statements to accomplish each of the...Ch. 4 - Write Java statements to accomplish each of the...Ch. 4 - Write Java statements to accomplish each of the...Ch. 4 - Write a Java statement to accomplish each of the...Ch. 4 - Write a Java statement to accomplish each of the...Ch. 4 - Write a Java statement to accomplish each of the...Ch. 4 - Write a Java statement to accomplish each of the...Ch. 4 - Combine the statements that you wrote in Exercise...Ch. 4 - Determine the value of the variables in the...Ch. 4 - Identify and correct the errors in each of the...Ch. 4 - What is wrong with the following while statement?...Ch. 4 - Compare and contrast the if single-selection...Ch. 4 - Explain what happens when a Java program attempts...Ch. 4 - Describe the two ways in which control statements...Ch. 4 - What type of iteration would be appropriate for...Ch. 4 - What is the difference between preincrementing and...Ch. 4 - Prob. 6.1ECh. 4 - What does the following program print? 1. //...Ch. 4 - 1. Read the problem statement. 2. Formulate the...Ch. 4 - 1. Read the problem statement. 2. Formulate the...Ch. 4 - 1. Read the problem statement. 2. Formulate the...Ch. 4 - 1. Read the problem statement. 2. Formulate the...Ch. 4 - (Find the Largest Number) The process of finding...Ch. 4 - Prob. 13.1ECh. 4 - (Find the Two Largest Numbers) Using an approach...Ch. 4 - Prob. 15.1ECh. 4 - What does the following program print? 1. //...Ch. 4 - What does the following program print? 1. //...Ch. 4 - (Dangling-else Problem) The Java compiler always...Ch. 4 - (Another Dangling-else Problem) Based on the...Ch. 4 - (Another Dangling-else Problem) Based on the...Ch. 4 - Prob. 21.1ECh. 4 - (Palindromes) A palindrome is a sequence of...Ch. 4 - (Printing the Decimal Equivalent of a Binary...Ch. 4 - (Checkerboard Pattern of Asterisks) Write an...Ch. 4 - (Multiples of 2 with an Infinite Loop) Write an...Ch. 4 - (Whats Wrong with This Code?) What is wrong with...Ch. 4 - Prob. 27.1ECh. 4 - (Sides of a Right Triangle) Write an application...Ch. 4 - Prob. 29.1ECh. 4 - Write an application that estimates the value of...Ch. 4 - Write an application that computes the value of e...Ch. 4 - (Enforcing Privacy with Cryptography) The...Ch. 4 - (World Population Growth) World population has...
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Literals in Java Programming; Author: Sudhakar Atchala;https://www.youtube.com/watch?v=PuEU4S4B7JQ;License: Standard YouTube License, CC-BY
Type of literals in Python | Python Tutorial -6; Author: Lovejot Bhardwaj;https://www.youtube.com/watch?v=bwer3E9hj8Q;License: Standard Youtube License