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

  1. Write the statements that test the first two integers, and swap them if necessary.
  2. Write the statements that test the second and third integer, and swap them if necessary.
  3. Write the statements that test the first and second integers again, and swap them if necessary.
  4. Execute the program using the following sets of input values.

    10122-23 63015009 2122

I'm trying to complete this assignment, I'm going over everything in the book and web, what am I doing wrong my part is the (Test to see)?

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.

if (first > second) {

secont = first;

}

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

else if (second > third) {

third = second;

}

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

else

temp = first;

// 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

Database Marketing The Ultimate Marketing Tool

Authors: Edward L. Nash

1st Edition

0070460639, 978-0070460638

More Books

Students also viewed these Databases questions