Write a program that reads in three floating-point numbers and prints the three
ID: 3624888 • Letter: W
Question
Write a program that reads in three floating-point numbers and prints the three inputs in sorted order. For example:Please enter three numbers:
4
9
2.5
The inputs in sorted order are:
2.5
4
9
Here is a sample program run:
Please enter three numbers:
4
9
2.5
The inputs in sorted order are:
2.5
4.0
9.0
Use the following class as your main class:
import java.util.Scanner;
/**
This is program sorts three numbers.
*/
public class DataSorter
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Please enter three numbers:");
double num1 = in.nextDouble();
double num2 = in.nextDouble();
double num3 = in.nextDouble();
DataSet s = new DataSet(num1, num2, num3);
System.out.println("The inputs in sorted order are: "
+ s.getSmallest() + " "
+ s.getMiddle() + " "
+ s.getLargest());
}
}
Complete the following class in your solution:
/**
This class finds the smallest, middle, and largest of
three numbers.
*/
public class DataSet
{
. . .
/**
Constructs a data set that processes three numbers.
@param num1 the first number to sort
@param num2 the second number to sort
@param num3 the third number to sort
*/
public DataSet(double num1, double num2, double num3)
{
. . .
}
/**
Gets the smallest number in the data set.
@return smallest the smallest of three numbers
*/
public double getSmallest()
{
. . .
}
/**
Gets the largest number in the data set.
@return largest the largest of three numbers
*/
public double getLargest()
{
. . .
}
/**
Gets the middle number in the data set.
@return middle the middle number of three numbers
*/
public double getMiddle()
{
. . .
}
}
Explanation / Answer
Dear,
import java.io.*;
import java.util.Scanner;
public class DataSorted
{
public static void main(String[] arg)
{
Scanner in=new Scanner(System.in);
System.out.println("enter the numbers:");
float n1=in.nextFloat();
float n2=in.nextFloat();
float n3=in.nextFloat();
Order o=new Order(n1,n2,n3);
System.out.println("The input sorted order is: "+ o.smallest() + " "+ o.middle() + " "+ o.largest());
}
}
class Order
{
public float num1;
public float num2;
public float num3;
Order(float a,float b,float c)
{
num1=a;
num2=b;
num3=c;
}
public float smallest()
{
if(num1 return(num1);
else
if(num2 return(num2);
else
return(num3);
}
public float middle()
{
if(num1num3)
return(num1);
else
if(num2>num3)
return(num3);
else
return(num2);
}
public float largest()
{
if(num1>num2 && num1>num3)
return(num1);
else
if(num2>num1 && num2>num3)
return(num2);
else
return(num3);
}
}
Output:
enter the number:
4
9
2.5
The input Sorted order is :
2.5
4
9
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.