Big Java Late Objects
Big Java Late Objects
2nd Edition
ISBN: 9781119330455
Author: Horstmann
Publisher: WILEY
bartleby

Videos

Textbook Question
Book Icon
Chapter 3, Problem 1RE

What is the value of each variable after the if statement?

  1. a. int n = 1; int k = 2; int r = n;

  if (k < n) { r = k; }

  1. b. int n = 1; int k = 2; int r;

  if (n < k) { r = k; }

  else { r = k + n; }

  1. c. int n = 1; int k = 2;

  int r = k; if (r < k) { n = r; }

  else { k = n; }

  1. d. int n = 1; int k = 2; int r = 3;

  if (r < n + k) { r = 2 * n; }

  else { k = 2 * r; }

Expert Solution
Check Mark

Explanation of Solution

a.

//Initialize the variables

int n=1; int k=2; int r=n;

//Check the condition

if (k<n)

{

    //Assign k to r

    r=k

}

Value of each variable afterifstatement:

n=1, k=2, r=1

Explanation:

The value assigned to variable “n” is “1”, the condition inside the “if” statement fails. As the value of “n” is not greater than “k” and so the value of “r” remains unchanged.

Expert Solution
Check Mark

Explanation of Solution

b.

//Initialize the variables

int n=1; int k=2; int r;

//Check the condition

if (n<k)

{

    //Assign k to r

    r=k

}

//Otherwise

else

{

    //Add k+n and store it in r

    r=k+n;

}

Value of each variable afterifstatement:

n=1, k=2, r=2

Explanation:

The value assigned to variable “n” is “1”, the condition inside the “if” statement is true. As the value of “k” is greater than “n” and so the value of “r” is changed to “2”.

Expert Solution
Check Mark

Explanation of Solution

c.

//Initialize the variables

int n=1; int k=2; int r=k;

//Check the condition

if (r<k)

{

    //Assign k to r

    n=r

}

//Otherwise

else

{

    //Assign n to k

    k=n;

}

Value of each variable afterifstatement:

n=1, k=1, r=2

Explanation:

The value assigned to variable “n” is “1”, the condition inside the “if” statement fails. As the value of “r” is not greater than “k” and it moves to the “else” part where the value of “k” is changed to value of “n” that is “1”. In the first statement, the value of “r” is initialized to value of “k” that is “2”.

Expert Solution
Check Mark

Explanation of Solution

d.

//Initialize the variables

int n=1; int k=2; int r=3;

//Check the condition

if (r<n+k)

{

    //Assign k to r

    r=2*n

}

//Otherwise

else

{

    //Assign n to k

    k=2*r;

}

Value of each variable afterifstatement:

n=1, k=6, r=3

Explanation:

The value assigned to variable “n” is “1”, the condition inside the “if” statement fails. As the value of “r” is not greater than “n+k” and it moves to the “else” part where the value of “k” is changed to value of “2*r” that is “6”. In the first statement, the value of “r” is initialized to “3”.

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Fix the mistakes in the following program so it runs as expected. There are 5 mistakes. All literals (such as 3.5) have the correct value and data type.  // Java program by [Student name] [Today's date] public class Mistakes {        public static void main(String[] args) {               / This is a comment               int z = 3.5;                System.out.println("z is + z");                 int s2 = "Michael";               System.out.println("s2 has the value " s2);        } } Please show the mistakes clearly and how the change is made.
In Java, one way to determine how healthy a person is by measuring the body fat of the person. The formulas to determine  the body fat for female and male are as follows: Bodyfat formula for women: A1 = (Body weight * 0.732) + 8.987 A2 = Wrist measurement (at fullest point) / 3.140 A3 = Waist measurement (at navel) * 0.157 A4 = Hip measurement (at fullest point) * 0.249 A5 = Forearm measurement (at fullest point) * 0.434 B = A1 + A2 - A3 - A4 + A5   Body fat = body weight - B Body fat percentage = body fat * 100 / body weight   Body fat formula for men: A1 = (Body wright * 1.082) + 94.42 A2 = Waist measurement * 4.15 B = A1 - A2 Body fat = body weight - B Body fat percentage = body fat * 100 / body weight   Write a program to caluclate the body fat of a person
Java - Programming Develop a letter-guessing game in this assignment.Requirements:1). When the game begins, generate a random letter between A and Z;2). Display a message "I have a secret letter (A to Z), can you guess what it is?";3). Read the user's answer;4). Compare the user's answer and the random secret letter generated;5). If the user answer is before the random secret letter in the alphabet, display "Incorrect. Trysomething later in the alphabet" and go to Step 2;6). If the user answer is after the random secret letter in the alphabet, display "Incorrect. Trysomething earlier in the alphabet." and go to Step 2;7). If the user answer is the same as the random secret letter, display "Well done. Want to playagain (y/n)?";8). Read the user's answer. If the answer is 'y', go to Step 1. If the answer is 'n', go to Step 9;9). Display "Thanks for playing the game. Goodbye!". The program stops.Run your program several times. Take screenshots. Submit your Java file with the screenshots

Chapter 3 Solutions

Big Java Late Objects

Ch. 3.3 - In a game program, the scores of players A and B...Ch. 3.3 - Prob. 12SCCh. 3.3 - Prob. 13SCCh. 3.3 - Beginners sometimes write statements such as the...Ch. 3.3 - Prob. 15SCCh. 3.3 - Suppose we want to have the earthquake program...Ch. 3.4 - Prob. 17SCCh. 3.4 - Would that amount change if the first nested if...Ch. 3.4 - Prob. 19SCCh. 3.4 - Prob. 20SCCh. 3.4 - Prob. 21SCCh. 3.5 - Draw a flowchart for a program that reads a value...Ch. 3.5 - Prob. 23SCCh. 3.5 - Prob. 24SCCh. 3.5 - Draw a flowchart for a program that reads a value...Ch. 3.5 - Draw a flowchart for a program that reads a value...Ch. 3.6 - Prob. 27SCCh. 3.6 - Prob. 28SCCh. 3.6 - Prob. 29SCCh. 3.6 - Suppose you are designing a part of a program for...Ch. 3.7 - Suppose x and y are two integers. How do you test...Ch. 3.7 - How do you test whether at least one of them is...Ch. 3.7 - How do you test whether exactly one of them is...Ch. 3.7 - Prob. 34SCCh. 3.7 - What is the advantage of using the type boolean...Ch. 3.8 - In the ElevatorSimulation2 program, what is the...Ch. 3.8 - Your task is to rewrite lines 1926 of the...Ch. 3.8 - In the Sherlock Holmes story The Adventure of the...Ch. 3.8 - Prob. 39SCCh. 3 - What is the value of each variable after the if...Ch. 3 - Prob. 2RECh. 3 - Find the errors in the following if statements. a....Ch. 3 - What do these code fragments print? a. int n = 1;...Ch. 3 - Suppose x and y are variables of type double....Ch. 3 - Suppose x and y are variables of type double....Ch. 3 - Explain why it is more difficult to compare...Ch. 3 - Given two pixels on a computer screen with integer...Ch. 3 - It is easy to confuse the - and operators. Write...Ch. 3 - Each square on a chess board can be described by a...Ch. 3 - Prob. 11RECh. 3 - In a scheduling program, we want to check whether...Ch. 3 - Draw a flowchart for the algorithm in Exercise...Ch. 3 - Draw a flowchart for the algorithm in Exercise...Ch. 3 - Draw a flowchart for the algorithm in Exercise...Ch. 3 - Prob. 16RECh. 3 - Prob. 17RECh. 3 - Write pseudocode for a program that prompts the...Ch. 3 - Write pseudocode for a program that assigns letter...Ch. 3 - Explain how the lexicographic ordering of strings...Ch. 3 - Of the following pairs of strings, which comes...Ch. 3 - Explain the difference between an if/else if/else...Ch. 3 - Give an example of an if/else if/else sequence...Ch. 3 - Rewrite the condition in Section 3.3 to use ...Ch. 3 - Prob. 25RECh. 3 - Make up a Java code example that shows the...Ch. 3 - Complete the following truth table by finding the...Ch. 3 - True or false? A B is the same as B A for any...Ch. 3 - The advanced search feature of many search engines...Ch. 3 - Suppose the value of b is false and the value of x...Ch. 3 - Simplify the following expressions. Here, b is a...Ch. 3 - Simplify the following statements. Here, b is a...Ch. 3 - What is wrong with the following program?...Ch. 3 - Write a program that reads an integer and prints...Ch. 3 - Write a program that reads a floating-point number...Ch. 3 - Write a program that reads an integer and prints...Ch. 3 - Write a program that reads three numbers and...Ch. 3 - Write a program that reads three numbers and...Ch. 3 - Repeat Exercise E3.5, but before reading the...Ch. 3 - Write a program that reads in three integers and...Ch. 3 - Write a program that reads four integers and...Ch. 3 - A compass needle points a given number of degrees...Ch. 3 - Write a program that reads a temperature value and...Ch. 3 - The boiling point of water drops by about one...Ch. 3 - Add error handling to Exercise E3.11. If the user...Ch. 3 - When two points in time are compared, each given...Ch. 3 - The following algorithm yields the season (Spring,...Ch. 3 - Write a program that reads in two floating-point...Ch. 3 - Unit conversion. Write a unit conversion program...Ch. 3 - Write a program that prompts the user to provide a...Ch. 3 - Write a program that asks the user to enter a...Ch. 3 - Write a program that translates a letter grade...Ch. 3 - Write a program that translates a number between 0...Ch. 3 - Write a program that takes user input describing a...Ch. 3 - Write a program that reads in three floating-point...Ch. 3 - Write a program that reads in three strings and...Ch. 3 - Write a program that prompts for the day and month...Ch. 3 - The original U.S. income tax of 1913 was quite...Ch. 3 - Write a program that computes taxes for the...Ch. 3 - The TaxReturn.java program uses a simplified...Ch. 3 - Write a program that reads in the x- and...Ch. 3 - Write a program that reads in the x- and...Ch. 3 - Write a program that reads in the x- and...Ch. 3 - Roman numbers. Write a program that converts a...Ch. 3 - A year with 366 days is called a leap year. Leap...Ch. 3 - French country names are feminine when they end...Ch. 3 - Write a program to simulate a bank transaction....Ch. 3 - Write a program that reads in the name and salary...Ch. 3 - When you use an automated teller machine (ATM)...Ch. 3 - Calculating the tip when you go to a restaurant is...Ch. 3 - A supermarket awards coupons depending on how much...Ch. 3 - Write a program that prompts the user for a...Ch. 3 - Repeat Exercise P3.21, modifying the program so...Ch. 3 - Repeat Exercise P3.21, modifying the program so...Ch. 3 - A minivan has two sliding doors. Each door can be...Ch. 3 - Sound level L in units of decibel (dB) is...Ch. 3 - The electric circuit shown below is designed to...Ch. 3 - Crop damage due to frost is one of the many risks...Ch. 3 - A mass m = 2 kilograms is attached to the end of a...Ch. 3 - The average person can jump off the ground with a...
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
Memory Management Tutorial in Java | Java Stack vs Heap | Java Training | Edureka; Author: edureka!;https://www.youtube.com/watch?v=fM8yj93X80s;License: Standard YouTube License, CC-BY