Difference between revisions of "Print Numbers"

From CompSciWiki
Jump to: navigation, search
(Re-wrote the step-by-step solution to reflect the new Solution Code)
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{1010PrAD|ProblemName=Print Numbers
 
{{1010PrAD|ProblemName=Print Numbers
  
|Problem= Write a method which will take 3 ints as parameters.
+
|Problem= Write a method which will take 3 integers as parameters.
The method should print the elements in order, separated by a comma.
+
The method should print out the elements in order from smallest to largest, separated by a comma.
 
<BR><BR>
 
<BR><BR>
Example:
+
For example, if the numbers input by the user are 40, 32, and 11, the program would have this output:
 
<BR>
 
<BR>
printNumbers(5, 7, 4);<BR>
+
{{OutputBlock
Output:4,5,7
+
|Code=
 +
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**
 +
}}
 +
 
  
  
Line 13: Line 25:
  
 
|SideSection=
 
|SideSection=
[[Image:Wiki_method02.jpg|center]]
+
[[Image:Wiki_method02.jpg|center]]<BR>
<BR>
+
  
  
 
|Solution=
 
|Solution=
You should split this up into three methods which print out the smallest, middle and largest numbers.
+
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.
<pre>
+
{{CodeBlock
public static void printSmallest( int n1, int n2, int n3 );
+
|Code=
public static void printMidNum( int n1, int n2, int n3 );
+
public static void main(String []args){
public static void printLargest( int n1, int n2, int n3 );
+
   
</pre>
+
  Scanner keyboard = new Scanner(System.in); //create Scanner object
 
+
   
You will need to use <b>if statements</b> to determine the proper
+
  processNums(keyboard) ;
order to print out the numbers.
+
   
<pre>
+
  endProgram() ;
if( n1 <= n2 && n1 <= n3 )
+
   
{
+
}}
  System.out.print( n1 );
+
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().
 +
{{CodeBlock
 +
|Code=
 +
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()
 +
   
 
}
 
}
else if( n2 <= n3 )
+
}}
{
+
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.
  System.out.print( n2 );
+
{{CodeBlock
}
+
|Code=
else
+
public static void reorderAndPrint(int one, int two, int three) {
{
+
   
  System.out.print( n3 );
+
    if(one <= two && two <= three)
}
+
      System.out.println("Numbers: " + one + ", " + two + ", " + three) ;
</pre>
+
   
and similarly for the middle and largest numbers.
+
    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().
 +
{{CodeBlock
 +
|Code=
 +
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.
 +
<BR>
 +
Congratulations, you have written a program to re-order integers so you no longer have to do it yourself.
  
 
|SolutionCode=
 
|SolutionCode=
<pre>
+
import java.util.Scanner;
public static void printNumbers( int n1, int n2, int n3 )
+
{
+
  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) {
 +
   
 +
    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**") ;
 +
   
 +
  }
 
}
 
}
 
public static void printLargest( int n1, int n2, int n3 )
 
{
 
  if( n1 >= n2 && n1 >= n3 )
 
  {
 
      System.out.print( n1 );
 
  }
 
  else if( n2 >= n3 )
 
  {
 
      System.out.print( n2 );
 
  }
 
  else
 
  {
 
      System.out.print( n3 );
 
  }
 
}
 
 
</pre>
 
 
 
 
}}
 
}}

Latest revision as of 14:54, 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

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