THINK FIRST·CODE LATER

← All labs

Three numbers in order, with if and swap

Problem

SortThree reads three whole numbers and prints them in increasing order, then the smallest, the middle and the largest, and finally whether all three are equal, two are equal, or all are different.

Use only if statements and the swap of two variables (no arrays, no library method): compare-and-swap a and b, then b and c, then a and b again. For the input

42 7 19

the program prints

In order: 7 19 42
Smallest: 7, middle: 19, largest: 42
All three are different

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l6-7-data-1.txt

42 7 19
What your program must print for l6-7-data-1.txt
In order: 7 19 42
Smallest: 7, middle: 19, largest: 42
All three are different

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l6-7-data-1.txt

Windows PowerShell does not understand <:

Get-Content l6-7-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.