Java
1. Implement a method that meets the following
requirements:
(a) Try to write this method with as few lines of code as you
can
(b) Sorts a group of three integers, x,y and z, into increasing
order (they do not have to be in a
sequence).
(c) Assume the value in x is less than the value in z. You can also
assume there are no duplicates
among x, y and z (none of them contain the same value)
(d) Prints a message each time the order of two elements are
changed.
(e) Prints the list before and after sorting
Hello,
Explanation
In the below screenshot, I showed the output and I displayed the message(Whenever the values are interchanged)
and each and every line in the code explained through comment lines.Hope you understand..!!
source code
import java.util.*;
import java.io.*;
public class HelloWorld{
public static void main(String []args){
int x;
int y;
int z;
Scanner sc=new Scanner(System.in);
//Reading the x and y and z values.
x=sc.nextInt();
y=sc.nextInt();
z=sc.nextInt();
System.out.println("--------------");
System.out.println("The values of x and y and z are displaying
before sorting in increasing order");
System.out.println(x);
System.out.println(y);
System.out.println(z);
System.out.println("--------------");
//comparing x and y
if(x>y)
{
int k=y;
int k1=x;
//Here the message shown,When the values are interchanged
System.out.println("Here,The two values of"+x+"and"+y+"are
interchanged");
x=k;
y=k1;
}
//comparing y and z
if(y>z)
{
int k=z;
int k1=y;
//Here the message shown,When the values are interchanged
System.out.println("Here,The two values of"+y+"and"+z+"are
interchanged");
y=k;
z=k1;
}
System.out.println("--------------");
System.out.println("The values of x and y and z are displaying
after sorting in increasing order");
System.out.println(x);
System.out.println(y);
System.out.println(z);
System.out.println("--------------");
}
}If you are satisfied with my
Explanation. Please hit a like button.
Have a good Day...!!!!
Get Answers For Free
Most questions answered within 1 hours.