Back to the Program-A-Day homepage
Problem
Write a method which will take 3 integers as parameters.
The method should print out the elements in order from smallest to largest, separated by a comma.
For example, if the numbers input by the user are 40, 32, and 11, the program would have this output:
Enter the first number:
40
Enter the second number:
32
Enter the third number:
11
Numbers: 11, 32, 40
Programmed by A. Student
**End of Program**
|
|
Static Methods
|
Solution
You should split this up into three methods which print out the smallest, middle and largest numbers.
public static void printSmallest( int n1, int n2, int n3 );
public static void printMidNum( int n1, int n2, int n3 );
public static void printLargest( int n1, int n2, int n3 );
You will need to use if statements to determine the proper
order to print out the numbers.
if( n1 <= n2 && n1 <= n3 )
{
System.out.print( n1 );
}
else if( n2 <= n3 )
{
System.out.print( n2 );
}
else
{
System.out.print( n3 );
}
and similarly for the middle and largest numbers.
|
Code
Solution Code
import java.util.Scanner;
/************************************************
PrintNumbers
Purpose: This program will take 3 integers as input from the user, and then will
re-order them and print the result
************************************************/
public class PrintNumbers{
public static void main(String []args){
Scanner keyboard = new Scanner(System.in); //create Scanner object
processNums(keyboard) ;
endProgram() ;
}
//Method: processNums
//Purpose: Accepts three integers as input from the user, and passes
// these numbers to reorderAndPrint()
public static void processNums(Scanner keyboard) {
int numberOne ;
int numberTwo ;
int numberThree ;
System.out.println("Enter the first number: ") ;
numberOne = keyboard.nextInt() ;
System.out.println("Enter the second number: ") ;
numberTwo = keyboard.nextInt() ;
System.out.println("Enter the third number: ") ;
numberThree = keyboard.nextInt() ;
reorderAndPrint(numberOne, numberTwo, numberThree) ; //passes the three numbers to reorderAndPrint()
}
//Method: reorderAndPrint
//Purpose: Accepts three numbers, reorders them, and prints the result
public static void reorderAndPrint(int one, int two, int three) {
if(one <= two && two <= three)
System.out.println("Numbers: " + one + ", " + two + ", " + three) ;
else if(two <= one && one <= three)
System.out.println("Numbers: " + two + ", " + one + ", " + three) ;
else if(three <= two && two <= one)
System.out.println("Numbers: " + three + ", " + two + ", " + one) ;
else if(one <= three && three <= two)
System.out.println("Numbers: " + one + ", " + three + ", " + two) ;
else if(two <= three && three <= one)
System.out.println("Numbers: " + two + ", " + three + ", " + one) ;
else
System.out.println("Numbers: " + three + ", " + one + ", " + two) ;
}
public static void endProgram() {
System.out.println() ;
System.out.println("Programmed by A. Student") ;
System.out.println("**End of Program**") ;
}
}
|
Back to the Program-A-Day homepage