Difference between revisions of "Print Numbers"

From CompSciWiki
Jump to: navigation, search
Line 13: Line 13:
  
 
|SideSection=
 
|SideSection=
 +
<BR>
  
  
 
|Solution=
 
|Solution=
You should split this up into three methods which print out the smallest, middle and largest images.
+
You should split this up into three methods which print out the smallest, middle and largest numbers.
 
<pre>
 
<pre>
 
public static void printSmallest( int n1, int n2, int n3 );
 
public static void printSmallest( int n1, int n2, int n3 );
public static void printMiddle( 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 );
 
public static void printLargest( int n1, int n2, int n3 );
 
</pre>
 
</pre>
Line 39: Line 40:
 
}
 
}
 
</pre>
 
</pre>
 +
and similarly for the middle and largest numbers.
  
  
 
|SolutionCode=
 
|SolutionCode=
 
<pre>
 
<pre>
public static void printArray( int n1, int n2, int n3 )
+
public static void printNumbers( int n1, int n2, int n3 )
 
{
 
{
   for( int i = 0; i < n.length; i++ )
+
   printSmallest();
 +
  System.out.print( "," );
 +
  printMidNum();
 +
  System.out.print( "," );
 +
  printLArgest();
 +
}
 +
 
 +
public static void printSmallest( int n1, int n2, int n3 )
 +
{
 +
  if( n1 <= n2 && n1 <= n3 )
 
   {
 
   {
      System.out.print( n[i] + "," );
+
      System.out.print( n1 );
 +
  }
 +
  else if( n2 <= n3 )
 +
  {
 +
      System.out.print( n1 );
 +
  }
 +
  else
 +
  {
 +
      System.out.print( n1 );
 
   }
 
   }
 
}
 
}
 +
 +
public static void printMidNum( int n1, int n2, int n3 )
 +
{
 +
  if( n1 >= n2 && n1 <= n3 )
 +
  {
 +
      System.out.print( n1 );
 +
  }
 +
  else if( n2 >= n1 && n2 <= n3 )
 +
  {
 +
      System.out.print( n1 );
 +
  }
 +
  else
 +
  {
 +
      System.out.print( n1 );
 +
  }
 +
}
 +
 +
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( n1 );
 +
  }
 +
  else
 +
  {
 +
      System.out.print( n1 );
 +
  }
 +
}
 +
 
</pre>
 
</pre>
  
  
 
}}
 
}}

Revision as of 12:48, 6 April 2010

Back to the Program-A-Day homepage

Problem

Write a method which will take 3 ints as parameters. The method should print the elements of the array in order, separated by a comma.

Example:
printNumbers(5, 7, 4);
Output:4,5,7

 

...by students


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( n1 );
}
else
{
   System.out.print( n1 );
}

and similarly for the middle and largest numbers.

Code

Solution Code

Back to the Program-A-Day homepage