Difference between revisions of "Print Numbers"

From CompSciWiki
Jump to: navigation, search
(Changed the introduction to be a little more specific and added an OutputBlock)
(Completely changed the Solution Code. Old version was broken and incomplete. New version is hot, hot, hot)
Line 58: Line 58:
  
 
|SolutionCode=
 
|SolutionCode=
public static void printNumbers( int n1, int n2, int n3 )
+
import java.util.Scanner;
{
+
  printSmallest( n1, n2, n3 );
+
  System.out.print( "," );
+
  printMidNum( n1, n2, n3 );
+
  System.out.print( "," );
+
  printLargest( n1, n2, n3 );
+
}
+
  
public static void printSmallest( int n1, int n2, int n3 )
+
/************************************************
{
+
   PrintNumbers
   if( n1 <= n2 && n1 <= n3 )
+
    
   {
+
   Purpose: This program will take 3 integers as input from the user, and then will
      System.out.print( n1 );
+
   re-order them and print the result
   }
+
    
   else if( n2 <= n3 )
+
  ************************************************/
  {
+
      System.out.print( n2 );
+
  }
+
  else
+
  {
+
      System.out.print( n3 );
+
   }
+
}
+
  
public static void printMidNum( int n1, int n2, int n3 )
+
public class PrintNumbers{
{
+
  public static void main(String []args){
  if( n1 >= n2 && n1 <= n3 || ( n1 >= n3 && n1 <= n2 ) )
+
   
  {
+
    Scanner keyboard = new Scanner(System.in);  //create Scanner object
       System.out.print( n1 );
+
   
  }
+
    processNums(keyboard) ;
  else if( n2 >= n1 && n2 <= n3 )
+
   
  {
+
    endProgram() ;
       System.out.print( n2 );
+
   
  }
+
  }
  else
+
 
  {
+
  //Method: processNums
       System.out.print( n3 );
+
  //Purpose: Accepts three integers as input from the user, and passes
  }
+
  //        these numbers to reorderAndPrint()
}
+
 
 
+
  public static void processNums(Scanner keyboard) {
public static void printLargest( int n1, int n2, int n3 )
+
   
{
+
    int numberOne ;
  if( n1 >= n2 && n1 >= n3 )
+
    int numberTwo ;
  {
+
    int numberThree ;
       System.out.print( n1 );
+
   
  }
+
    System.out.println("Enter the first number: ") ;
  else if( n2 >= n3 )
+
    numberOne = keyboard.nextInt() ;
  {
+
    System.out.println("Enter the second number: ") ;
       System.out.print( n2 );
+
    numberTwo = keyboard.nextInt() ;
  }
+
    System.out.println("Enter the third number: ") ;
  else
+
    numberThree = keyboard.nextInt() ;
  {
+
   
      System.out.print( n3 );
+
    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**") ;
 +
   
 +
  }
 
}
 
}
 
}}
 
}}

Revision as of 14:26, 7 December 2011

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

Wiki method02.jpg

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

Back to the Program-A-Day homepage