/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Problem 24 (Largest and Smallest Integers) ... [FREE SOLUTION] | 91Ó°ÊÓ

91Ó°ÊÓ

(Largest and Smallest Integers) Write an application that reads five integers and determines and prints the largest and smallest integers in the group. Use only the programming techniques you learned in this chapter.

Short Answer

Expert verified
Initialize 'largest' with the smallest possible integer, and 'smallest' with the largest possible integer. Read five integers, updating 'largest' and 'smallest' as necessary. Then print the final values of 'largest' and 'smallest'.

Step by step solution

01

Initialize Variables

Create two variables, say 'largest' and 'smallest'. Initialize 'largest' to a very small value (e.g., the smallest integer value) and 'smallest' to a very large value (e.g., the largest integer value). This way, any number compared to these will replace them appropriately.
02

Read Integers

Prompt the user to enter five integers. This can be done using a loop that iterates five times, or by reading the input line and splitting the input into separate integers.
03

Compare Integers

During each iteration, compare the current integer with the 'largest' and 'smallest' variables. If the current integer is greater than 'largest', update 'largest' to this integer. If the current integer is smaller than 'smallest', update 'smallest' to this integer.
04

Print Results

After all five integers have been read and compared, print the values of 'largest' and 'smallest'.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with 91Ó°ÊÓ!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Java Conditional Statements
In Java, conditional statements are the backbone of decision making. Imagine you're at an ice cream shop trying to decide between chocolate and vanilla. Depending on your preference, you choose one flavor over the other. In programming, this is not so different. You're often faced with situations where a program needs to decide between two or more options based on certain conditions.

These decisions are handled by statements like 'if', 'else if', and 'else'. In our exercise, to find the largest and smallest numbers, you use an 'if' statement to check if the current integer is greater than the 'largest' so far or smaller than the 'smallest' so far. For example, the syntax looks something like this:
if(currentNumber > largest) {    largest = currentNumber;}if(currentNumber < smallest) {    smallest = currentNumber;}
Java Loops
Loops in Java are like running laps around a track—you keep going until you've reached the number of laps (or iterations) you set out to do. They are especially handy when you need to repeat an action multiple times, like asking a user for input several times.

In our example with integers, a loop is used to iterate over the user's input five times to capture all five integers. You might use a 'for' loop because you know exactly how many times you want to run the block of code:
for(int i = 0; i < 5; i++) {    // Prompt user and get input    // Compare integers}
  • The loop initializes a counter to zero.
  • It runs until the counter hits five.
  • After each iteration, the counter is increased by one.
Every time through the loop, you're prompting for input and using conditional statements to update 'largest' and 'smallest'.
Variable Initialization in Java
In Java, before you can use a variable, you need to declare it and, ideally, you should initialize it—like setting the time on a stopwatch before starting it. Variable initialization gives your variable an initial value. Without initialization, your variables could contain junk data—which can lead to unpredictable results or run-time errors.

In the textbook exercise, 'largest' and 'smallest' are initialized to extreme values. This lays the groundwork for the comparison in the conditional statements:
  • The 'largest' could be initialized to Integer.MIN_VALUE.
  • The 'smallest' could be initialized to Integer.MAX_VALUE.
This is a clever trick because any number the user inputs will be larger than the smallest possible integer and smaller than the largest possible integer. Thus, your comparison is off to a solid start. Here is what the initialization might look like:
int largest = Integer.MIN_VALUE;int smallest = Integer.MAX_VALUE;
Good initialization ensures that the first user input will properly set both 'largest' and 'smallest' to values that are in the range of user input.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

\((\text { Multiples })\) Write an application that reads two integers, determines whether the first is a multiple of the second and prints the result. [Hint: Use the remainder operator.]

Separating the Digits in an Integer) Write an application that inputs one number consisting of five digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 42339 the program should print $$4 \quad 2 \quad 3 \quad 3 \quad 9$$ Assume that the user enters the correct number of digits. What happens when you execute the program and type a number with more than five digits? What happens when you execute the program and type a number with fewer than five digits? [Hint: It's possible to do this exercise with the techniques you learned in this chapter. You'll need to use both division and remainder operations to "pick off" each digit.]

Write Java statements that accomplish each of the following tasks: a) Display the message "Enter an integer: ", leaving the cursor on the same line. b) Assign the product of variables b and c to variable a. c) Use a comment to state that a program performs a sample payroll calculation.

(Integer Value of a Character) Here's another peek ahead. In this chapter, you learned about integers and the type int. Java can also represent uppercase letters, lowercase letters and a considerable variety of special symbols. Every character has a corresponding integer representation. The set of characters a computer uses together with the corresponding integer representations for those characters is called that computer's character set. You can indicate a character value in a program simply by enclosing that character in single quotes, as in 'A".You can determine a character's integer equivalent by preceding that character with (int), as in (int) 'A' An operator of this form is called a cast operator. (You'll learn about cast operators in Chapter \(4 .\) ) The following statement outputs a character and its integer equivalent: System .out.printf "The character \(\left.\$ c \text { has the value } \gamma d \backslash n^{\prime \prime},^{\prime} A^{\prime},\left((\text { int })^{\prime} A^{\prime}\right)\right)\) When the preceding statement executes, it displays the character A and the value 65 (from the Uni\(\left.\operatorname{cod} e^{@} \text { character set }\right)\) as part of the string. The format specificr \(\Varangle c\) is a placeholder for a character (in this case, the character 'A'). Using statements similar to the one shown earlier in this exercise, write an application that displays the integer equivalents of some uppercase letters, lowercase letters, digits and special symbols. Display the integer equivalents of the following: \(A B C\) a \(b\) c 012 \$ is \(+/\) and the blank character.

(Negative, Positive and Zero Values) Write a program that inputs five numbers and determines and prints the number of negative numbers input, the number of positive numbers input and the number of zeros input.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.