Difference between revisions of "Return Array"

From CompSciWiki
Jump to: navigation, search
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:
 
<BR>
 
<BR>
int[] array = {4889123, 4889124, 4889125};
+
combineNumber(204, 4881234);
<BR>
+
returnArray(array);
+
 
<BR><BR>
 
<BR><BR>
 
Output:
 
Output:
 
<BR>
 
<BR>
2044889123
+
2044881234
<BR>
+
 
2044889124
+
<BR>
+
2044889125
+
  
  
Line 26: 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 32: 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 methodreturnArray will return an array which we will store in a temporary array and print:
+
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 combineNumbercombineNumber will return a integer which we will store in a temporary place holderand 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);
  
   for( int i = 0; i < tempArray.length; i++ ) // This will loop through every item in the array
+
   System.out.print(temp);
  {     
+
      System.out.println( tempArray[i] ); // Print the changed value
+
  }
+
 
}
 
}
 
</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
+
   int returnValue;
   {
+
 
      array [i] += 2040000000; // add 2040000000 to each element to give it an area code
+
   areaCode = areaCode * 10000000
   }
+
 
   return array; // Return the changed array
+
  returnValue = areaCode + phoneNumber;
 +
    
 +
   return returnValue; // Return the changed combined values
 
}
 
}
 
</pre>  
 
</pre>  
Line 72: Line 65:
 
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.println( 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
+
   int returnValue;
   {
+
 
      array [i] += 2040000000; // add 2040000000 to each element to give it an area code
+
   areaCode = areaCode * 10000000
   }
+
 
   return array; // Return the changed array
+
  returnValue = areaCode + phoneNumber;
 +
    
 +
   return returnValue; // Return the changed combined values
 
}
 
}
 
</pre>
 
</pre>
  
 
}}
 
}}

Revision as of 12:22, 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 holderand print:

public static void main()
{
   int temp;

   temp = combineNumber(204, 4881234);

   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 )
{
   int returnValue;

   areaCode = areaCode * 10000000 
   
   returnValue = areaCode + phoneNumber;
   
   return returnValue; // Return the changed combined values
}


Now put it all together:


public static void main()
{
   int temp;

   temp = combineNumber(204, 4881234);

   System.out.print(temp);
}

public static int combineNumber( int areaCode, int phoneNumber )
{
   int returnValue;

   areaCode = areaCode * 10000000 
   
   returnValue = areaCode + phoneNumber;
   
   return returnValue; // Return the changed combined values
}

Code

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

Back to the Program-A-Day homepage