Difference between revisions of "Return Array"

From CompSciWiki
Jump to: navigation, search
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{1010PrAD|ProblemName=Return Array
 
{{1010PrAD|ProblemName=Return Array
  
|Problem= Write a method which will take an array of phone numbers and add the 204 area code to each one.  Print this array in main.
+
|Problem= Write a method which will take a three digit area code and a phone number and combine these into one value.  Print this value in main.
 
<BR><BR>
 
<BR><BR>
 
Example:
 
Example:
int[] array = {4889123, 4889124, 4889125};<BR>
+
<BR>
returnArray(array);<BR>
+
combineNumber(204, 4881234);
 +
<BR><BR>
 
Output:
 
Output:
2044889123
+
<BR>
2044889124
+
2044881234
2044889125
+
 
  
  
Line 20: Line 21:
  
 
|Solution=The solution...
 
|Solution=The solution...
To solve this problem we are going to create a skeleton structure.  We know that we will have two methods: main & returnArray.  We know that returnArray will accept an integer array and return that array:
+
To solve this problem we are going to create a skeleton structure.  We know that we will have two methods: main & combineNumber.  We know that combineNumber will accept two integers and return an integer:
 
<BR>
 
<BR>
 
<pre>
 
<pre>
Line 26: Line 27:
 
{
 
{
 
}
 
}
public static int[] returnArray( int[] array )
+
public static int returnArray( int areaCode, int phoneNumber )
 
{
 
{
 
}
 
}
 
</pre>
 
</pre>
 
<BR>
 
<BR>
Now that we have the skeleton we can start by creating an array in main, we will pass that array to our returnArray method and return it into a temporary array and then print it:
+
Now that we have the skeleton we can start filling in main.  We know that main will pass an area code and a number to combineNumber.  combineNumber will return a integer which we will store in a temporary place holder and print:
 
<pre>
 
<pre>
 
public static void main()
 
public static void main()
 
{
 
{
   int[] array = {4889123, 4889124, 4889125};
+
   int temp;
  int[] tempArray;
+
  
   tempArray = returnArray(array);
+
   temp = combineNumber(204, 4881234); // instead of storing in temp you could also just print this directly
  
   for( int i = 0; i < tempArray.length; i++ ) // This will loop through every item in the array
+
   System.out.print(temp);
  {     
+
      System.out.print( tempArray[i] + "/" ); // Print the changed value
+
  }
+
}
+
 
+
public static int[] returnArray( int[] n )
+
{
+
 
}
 
}
 
</pre>
 
</pre>
  
 
<BR>
 
<BR>
Now we will fill in returnArray.  We know that it will be passed an array and we need add 204 to the beginning of each one. We will then need to return the array:  
+
Now we will fill in combineNumber.  We know that it will be passed two numbers and we will need to combine them into one number. We can't just add them together though.  We will need to change the area code to be area code + 0000000.  This will cause it to be added onto the start of the phone number:  
  
 
<pre>
 
<pre>
public static int[] returnArray( int[] array )
+
public static int combineNumber( int areaCode, int phoneNumber )
 
{
 
{
   for( int i = 0; i < array .length; i++ ) // This will loop through every item in the array
+
   areaCode = areaCode * 10000000;    
   {
+
    
      array [i] = i+2040000000; // add 2040000000 to each element to give it an area code
+
   return (areaCode + phoneNumber);
  }
+
   return array; // Return the changed array
+
 
}
 
}
 
</pre>  
 
</pre>  
  
<BR>Now put it all together:<BR>
 
<pre>
 
  
 +
|Solution Code=Final Solution...
 +
<BR>
 +
<pre>
 
public static void main()
 
public static void main()
 
{
 
{
   int[] array = {4889123, 4889124, 4889125};
+
   int temp;
  int[] tempArray;
+
  
   tempArray = returnArray(array);
+
   temp = combineNumber(204, 4881234);
  
   for( int i = 0; i < tempArray.length; i++ ) // This will loop through every item in the array
+
   System.out.print(temp);
  {     
+
      System.out.print( tempArray[i] + "/" ); // Print the changed value
+
  }
+
 
}
 
}
  
public static int[] returnArray( int[] n )
+
public static int combineNumber( int areaCode, int phoneNumber )
 
{
 
{
   for( int i = 0; i < array .length; i++ ) // This will loop through every item in the array
+
   areaCode = areaCode * 10000000;    
   {
+
    
      array [i] = i+2040000000; // add 2040000000 to each element to give it an area code
+
   return (areaCode + phoneNumber);
  }
+
   return array; // Return the changed array
+
 
}
 
}
 
</pre>
 
</pre>
 
 
}}
 
}}

Latest revision as of 12:32, 1 April 2010

Back to the Program-A-Day homepage

Problem

Write a method which will take a three digit area code and a phone number and combine these into one value. Print this value in main.

Example:
combineNumber(204, 4881234);

Output:
2044881234

 

SideSectionTitle

float
Taken from http://www.flickr.com/photos/daniello/565304023/

An image or By Students section

Solution

The solution... To solve this problem we are going to create a skeleton structure. We know that we will have two methods: main & combineNumber. We know that combineNumber will accept two integers and return an integer:

public static void main()
{
}
public static int returnArray( int areaCode, int phoneNumber  )
{
}


Now that we have the skeleton we can start filling in main. We know that main will pass an area code and a number to combineNumber. combineNumber will return a integer which we will store in a temporary place holder and print:

public static void main()
{
   int temp;

   temp = combineNumber(204, 4881234); // instead of storing in temp you could also just print this directly

   System.out.print(temp);
}


Now we will fill in combineNumber. We know that it will be passed two numbers and we will need to combine them into one number. We can't just add them together though. We will need to change the area code to be area code + 0000000. This will cause it to be added onto the start of the phone number:

public static int combineNumber( int areaCode, int phoneNumber )
{
   areaCode = areaCode * 10000000;      
   
   return (areaCode + phoneNumber);
}

Code

SolutionCode goes here. Please DO NOT put your code in <pre> tags!

Back to the Program-A-Day homepage