Print Numbers

From CompSciWiki
Revision as of 14:54, 7 December 2011 by AdamW (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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

To create this program, you will use four distinct methods. First, you will have the main method. All this method will do is create an instance of Scanner and call other methods. No actual processing is done in the main method.

 public static void main(String []args){
    
  Scanner keyboard = new Scanner(System.in);  //create Scanner object
    
  processNums(keyboard) ;
    
  endProgram() ; 

Now, you will notice that the first method to be called by the main method is processNums(keyboard). By putting keyboard in parentheses, we are passing the Scanner instance to the method so it can use it. Alternatively, you could just declare keyboard inside processNums, but this way, if another method needs to accept input, you can just pass keyboard to it rather than declaring keyboard inside each method. Now we must write the method processNums().

 public static void processNums(Scanner kb) {
    
    int numberOne ;
    int numberTwo ;
    int numberThree ;
    
    System.out.println("Enter the first number: ") ;
    numberOne = kb.nextInt() ;
    System.out.println("Enter the second number: ") ;
    numberTwo = kb.nextInt() ;
    System.out.println("Enter the third number: ") ;
    numberThree = kb.nextInt() ;
    
    reorderAndPrint(numberOne, numberTwo, numberThree) ; //passes the three numbers to reorderAndPrint()
    
} 

You will notice that processNums explicitly states that it accepts a Scanner object (kb) as a parameter. The object has been named kb here, to illustrate that you do not have to match the names of the parameters when calling a method, only the type. ProcessNums then declares a variable for each integer, prompts the user to enter an integer, then accepts an integer. When processNums() has all three integers, it calls reorderAndPrint() to complete the processing.

 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) ;
  
  } 

This method takes the three numbers passed to it, re-orders them, and prints the result. It uses an if-else block to determine every possible ordering of three integers. Obviously this is not the most practical way to do this, and if there were many more integers, the code would be quite long. But this is good enough for now. Now that the method is finished re-ordering, it will jump back to the method that called it (processNums). ProcessNums is also finished so the program jumps back to the main method. The main method then processes the last method, endProgram().

 public static void endProgram() {
    
    System.out.println() ;
    System.out.println("Programmed by A. Student") ;
    System.out.println("**End of Program**") ;
    
  } 

This is a simple method that you can call to indicate the end of the program, rather than just typing it all in at the end of the main method. This is not necessary but helps to make your code look cleaner.
Congratulations, you have written a program to re-order integers so you no longer have to do it yourself.

Code

Solution Code

Back to the Program-A-Day homepage