Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Swapping Values Summary In this lab, you will complete a Java program that swaps values stored in three int variables and determines maximum and minimum

Swapping Values

Summary

In this lab, you will complete a Java program that swaps values stored in three int variables and determines maximum and minimum values. The Java file provided contains the necessary variable declarations, as well as the input and output statements. You want to end up with the smallest value stored in the variable named first and the largest value stored in the variable named third. You need to write the statements that compare the values and swap them if appropriate. Comments included in the code tell you where to write your statements.

Instructions

Write the statements that test the first two integers, and swap them if necessary.

Write the statements that test the second and third integer, and swap them if necessary.

Write the statements that test the first and second integers again, and swap them if necessary.

Execute the program using the following sets of input values.

10122-23

63015009

2122

__________________________________

// Swap.java - This program determines the minimum and maximum of three values input by

// the user and performs necessary swaps.

// Input: Three int values.

// Output: The numbers in numerical order.

import java.util.Scanner;

public class Swap

{

public static void main(String args[]) throws Exception

{

// Declare variables.

String firstNumber; // String version of first number.

String secondNumber; // String version of second number.

String thirdNumber; // String version of third number.

int first = 0; // First number.

int second = 0; // Second number.

int third = 0; // Third number.

int temp; // Used to swap numbers.

Scanner input = new Scanner(System.in);

// Get user input.

System.out.print("Enter first number: ");

if((firstNumber = input.nextLine())) != null)

{

System.out.print("Enter second number: ");

secondNumber = input.nextLine();

System.out.print("Enter third number: ");

thirdNumber = input.nextLine();

// Convert Strings to int.

first = Integer.parseInt(firstNumber);

second = Integer.parseInt(secondNumber);

third = Integer.parseInt(thirdNumber);

}

// Test to see if the first number is greater than the second number.

// Test to see if the second number is greater than the third number.

// Test to see if the first number is greater than the second number again.

// Print values in numerical order.

System.out.println("Smallest: " + first);

System.out.println("Next largest: " + second);

System.out.println("Largest: " + third);

// Exit the program.

System.exit(0);

} // End of main() method.

} // End of Swap class.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Database Programming With Visual Basic.NET

Authors: Ying Bai

1st Edition

0521712351, 978-0521712354

More Books

Students also viewed these Databases questions

Question

4. What actions should Bouleau & Huntley take now?

Answered: 1 week ago